Codeforces Round 896 (Div. 2) A-D2
A. Make It Zero
没看懂这个 8 次的限制是什么意思。先用一次直接把所有数变成
点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int inf=1e18;
inline int read(){
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int a[100005];
void solve(){
int n=read();
for(int i=1;i<=n;i++)a[i]=read();
if(n%2==0){
printf("2\n1 %lld\n1 %lld\n",n,n);
}
else{
printf("4\n1 %lld\n1 %lld\n%lld %lld\n%lld %lld\n",n,n-1,n-1,n,n-1,n);
}
}
signed main(){
int T=read();
while(T--)solve();
return 0;
}
B. 2D Traveling
点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int inf=1e18;
inline int read(){
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int n,k,a,b,x[200005],y[200005];
int dist(int i,int j){
if(i<=k&&j<=k)return 0;
return abs(x[i]-x[j])+abs(y[i]-y[j]);
}
void solve(){
n=read(),k=read(),a=read(),b=read();
for(int i=1;i<=n;i++)x[i]=read(),y[i]=read();
int ans=dist(a,b),da=inf,db=inf;
for(int i=1;i<=k;i++)da=min(da,dist(a,i));
for(int i=1;i<=k;i++)db=min(db,dist(b,i));
ans=min(ans,da+db);
printf("%lld\n",ans);
}
signed main(){
int T=read();
while(T--)solve();
return 0;
}
C. Fill in the Matrix
当
特判一下
点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int inf=1e18;
inline int read(){
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
void solve(){
int n=read(),m=read();
if(n==1&&m==1){
printf("0\n");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("0 ");
}
puts("");
}
return;
}
if(m==1){
printf("0\n");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("0 ");
}
puts("");
}
return;
}
if(n==1){
printf("2\n");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%lld ",j);
}
puts("");
}
return;
}
if(n>=m-1){
printf("%lld\n",m);
for(int i=0;i<m-1;i++){
for(int j=0;j<m;j++){
printf("%lld ",(i+j)%m);
}
puts("");
}
for(int i=m-1;i<n;i++){
for(int j=0;j<m;j++){
printf("%lld ",(m-2+j)%m);
}
puts("");
}
return;
}
else{
int M=n+1;printf("%lld\n",M);
for(int i=0;i<n;i++){
for(int j=0;j<M;j++){
printf("%lld ",(i+j)%M);
}
for(int j=M;j<m;j++){
printf("%lld ",j);
}
puts("");
}
return;
}
}
signed main(){
int T=read();
while(T--)solve();
return 0;
}
D1. Candy Party (Easy Version)
首先平均数不是整数不行,每个数与平均数的差不能写成
点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int inf=1e18;
inline int read(){
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int a[200005],cnt[1005];
void solve(){
for(int i=0;i<=100;i++)cnt[i]=0;
int n=read(),s=0;
for(int i=1;i<=n;i++)a[i]=read(),s+=a[i];
if(s%n)return puts("No"),void();
s/=n;
for(int i=1;i<=n;i++){
if(s>=a[i]){
int o=s-a[i],p1=0,p2=0;
while(o&&o%2==0)o/=2,p1++;
p2=p1;while(o&&o%2==1)o/=2,p2++;
if(o)return puts("No"),void();
cnt[p1]++,cnt[p2]--;
}
else{
int o=a[i]-s,p1=0,p2=0;
while(o&&o%2==0)o/=2,p1++;
p2=p1;while(o&&o%2==1)o/=2,p2++;
if(o)return puts("No"),void();
cnt[p1]--,cnt[p2]++;
}
}
for(int i=0;i<=100;i++)if(cnt[i]!=0)return puts("No"),void();
puts("Yes");
}
signed main(){
int T=read();
while(T--)solve();
return 0;
}
D2. Candy Party (Hard Version)
考试的时候蠢了。这个题在上一个题的基础上多了一种选择:当差可以表示成
点击查看代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int inf=1e18;
inline int read(){
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int a[200005],cnt[1005],v[2][1005];
void solve(){
for(int i=0;i<=100;i++)cnt[i]=v[0][i]=v[1][i]=0;
int n=read(),s=0;
for(int i=1;i<=n;i++)a[i]=read(),s+=a[i];
if(s%n)return puts("No"),void();
s/=n;
for(int i=1;i<=n;i++){
if(s>a[i]){
int o=s-a[i],p1=0,p2=0;
while(o&&o%2==0)o/=2,p1++;
p2=p1;while(o&&o%2==1)o/=2,p2++;
if(o)return puts("No"),void();
if(p2!=p1+1)cnt[p1]++,cnt[p2]--;
else v[1][p1]++;
}
else if(s<a[i]){
int o=a[i]-s,p1=0,p2=0;
while(o&&o%2==0)o/=2,p1++;
p2=p1;while(o&&o%2==1)o/=2,p2++;
if(o)return puts("No"),void();
if(p2!=p1+1)cnt[p1]--,cnt[p2]++;
else v[0][p1]++;
}
}
for(int i=100;i>=1;i--){
int val=cnt[i]+(v[0][i]-v[1][i]);
if(val>0){
if(v[1][i-1]<val)return puts("No"),void();
v[1][i-1]-=val,cnt[i-1]+=val;
}
else if(val<0){
if(v[0][i-1]<-val)return puts("No"),void();
v[0][i-1]-=-val,cnt[i-1]-=-val;
}
}
if(cnt[0]+(v[0][0]-v[1][0])==0)puts("Yes");
else puts("No");
}
signed main(){
int T=read();
while(T--)solve();
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】