A - Print a Pedestal (Codeforces logo?)
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int>
#define vi vector<int>
void solve()
{
int n;
cin>>n;
int a=0,b=0,c=0;
if(n%3==0){
cout<<n/3<<" "<<n/3+1<<" "<<n/3-1<<endl;
}
else if (n%3==1){
cout<<n/3<<" "<<n/3+2<<" "<<n/3-1<<endl;
}
else {
cout<<n/3+1<<" "<<n/3+2<<" "<<n/3-1<<endl;
}
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}
B - Array Decrements
主要看ai 于bi之间的差和ai 与0之间的差大小,第二种情况不能大于第一种情况。
#include "bits/stdc++.h"
using namespace std;
#pragma GCC optimize(2)
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int>
#define vi vector<int>
void solve()
{
int n;
cin>>n;
vi a(n);
vi b(n);
for (int i=0;i<n;i++){
cin>>a[i];
}
for (int i=0;i<n;i++){
cin>>b[i];
}
int x=0,y=1e9;
for (int i=0;i<n;i++){
if(b[i]==0){
x=max(x,a[i]);
}
else {
x=max(x,a[i]-b[i]);
y=min(a[i]-b[i],y);
}
}
if(x<=y){
cout<<"YES"<<endl;
}
else {
cout<<"no"<<endl;
}
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}
C - Restoring the Duration of Tasks
可以理解为多个区段的首尾判断,枚举一次,每次更新下一区段的开始位置即可。
#include "bits/stdc++.h"
using namespace std;
#pragma GCC optimize(2)
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int>
#define vi vector<int>
void solve()
{
int n;
cin>>n;
vi a(n);
vi b(n);
for (int i=0;i<n;i++){
cin>>a[i];
}
for (int i=0;i<n;i++){
cin>>b[i];
}
vi c;
int last,begin;
for (int i=0;i<n;i++){
if(i==0){
c.push_back(b[i]-a[i]);
last=b[i];
}else {
if(a[i]<=last){
begin=last;
}
else {
begin=a[i];
}
c.push_back(b[i]-begin);
last=b[i];
}
}
for (int i=0;i<n;i++){
cout<<c[i]<<" ";
}
cout<<endl;
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}
D - Black and White Stripe
可以通过前缀和记录w的数量,来查找k的区间中,w的最小数量。
#include "bits/stdc++.h"
using namespace std;
#pragma GCC optimize(2)
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int>
#define vi vector<int>
void solve()
{
int n,k;
cin>>n>>k;
string s;
cin>>s;
vi a(n+1);
for (int i=0;i<n;i++){
a[i+1]=a[i]+(s[i]=='W');
}
int ans=k;
for (int i=0;i<=n-k;i++){
ans=min(ans,a[i+k]-a[i]);
}
cout<<ans<<endl;
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}
E - Price Maximization
本来以为是找一对尽量和可以被k整除的数,后来改为先记录每一个物品除 k 的商,每个数取余数
在使用双指针从头尾扫刚好大于等于k的数,更新总和。
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0);
#define all(x) x.begin(),x.end()
#define pi pair<int,int>
#define vi vector<int>
void solve()
{
int n,k;
cin>>n>>k;
vi a(n);
int sum=0;
for (int i=0;i<n;i++){
cin>>a[i];
sum+=a[i]/k;
a[i]%=k;
}
sort(a.begin(),a.end(),greater());
for (int l=0,r=n-1;l<r;l++,r--){
while(a[l]+a[r]<k && l<r) r--;
if(l==r){
break;
}
sum++;
}
cout<<sum<<endl;
}
signed main()
{
IOS
int t;
cin>>t;
while(t--){
solve();
}
}