SMU Winter 2023 Round #5 (Div.2)
int main(){ int t,a[6]; cin>>t; while(t--){ scanf("%1d%1d%1d%1d%1d%1d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5]); if(a[0]+a[1]+a[2]==a[3]+a[4]+a[5])cout<<"YES\n"; else cout<<"NO\n"; } return 0; }Equal Candies 思路:找出最小的数
int main(){ int t,n,a[55]; cin>>t; while(t--){ int mi=N; cin>>n; for(int i=0;i<n;++i){ cin>>a[i]; if(a[i]<mi)mi=a[i]; } long long s=0; for(int i=0;i<n;++i){ s+=(a[i]-mi); } cout<<s<<'\n'; } return 0; }
思路:枚举每一对
#include<bits/stdc++.h> using namespace std; const int N=1e7+10; int main(){ int t,n,m; string a[55]; cin>>t; while(t--){ cin>>n>>m; for(int i=0;i<n;++i)cin>>a[i]; int mi=N; for(int i=0;i<n-1;++i) for(int j=i+1;j<n;++j){ int s=0; for(int k=0;k<m;++k){ s+=(a[i][k]+a[j][k]-2*min(a[i][k],a[j][k])); } mi=min(mi,s); } cout<<mi<<'\n'; } return 0; }
X-Sum 思路:枚举每个点的对角线和
#include<bits/stdc++.h> using namespace std; const int N=1e7+10; int main(){ int t,n,m; int a[205][205]; int dx[4]={-1,-1,1,1},dy[4]={-1,1,-1,1}; cin>>t; while(t--){ cin>>n>>m; int res=0; for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) cin>>a[i][j]; for(int i=1;i<=n;++i) for(int j=1;j<=m;++j){ int s=0; s+=a[i][j]; for(int k=1;k<=max(n,m);++k){ for(int u=0;u<4;++u){ int xx=k*dx[u]+i,yy=k*dy[u]+j; if(xx>0&&xx<=n&&yy>0&&yy<=m) s+=a[xx][yy]; } } res=max(res,s); } cout<<res<<'\n'; } return 0; }
思路:对所有数降序排序后求前缀和,前缀和为升序,二分查找x
#include<bits/stdc++.h> using namespace std; const int N=150005; typedef long long ll; bool cmp(int a,int b){ return a>b; } int main(){ int t,n,q,a[N]; ll x; cin>>t; while(t--){ cin>>n>>q; for(int i=0;i<n;++i)cin>>a[i]; sort(a,a+n,cmp); for(int i=1;i<n;++i)a[i]+=a[i-1]; while(q--){ cin>>x; if(x>a[n-1])cout<<-1<<'\n'; else if(x==a[n-1])cout<<n<<'\n'; else{ ll s=lower_bound(a,a+n,x)-a; if(s<n)cout<<s+1<<'\n'; else cout<<-1<<'\n'; } } } return 0; }
Longest Strike 思路:用map存储每个数个数,刚好得到排列后的序列,查找个数都大于等于k的连续区间
#include<bits/stdc++.h> using namespace std; const int N=1e6+10; typedef long long ll; int t,n,k,l,r,maxl,mi,ma,len,last,a[N]; map<int,int>mp; int main(){ cin>>t; while(t--){ cin>>n>>k; mp.clear(),maxl=0,mi=0x3f3f3f3f,ma=0,len=0; for(int i=1;i<=n;++i){ cin>>a[i]; mp[a[i]]++; mi=min(mi,a[i]),ma=max(ma,a[i]); } last=mi-1; for(auto v:mp){ int i=v.first; if(i==ma&&v.second>=k&&i==last+1&&len+1>maxl)maxl=len+1,l=i-len,r=i; if(v.second>=k&&i==last+1)len++; else{ if(len>maxl)maxl=len,l=last+1-len,r=last; if(v.second>=k)len=1; else len=0; } last=i; } if(maxl!=0)cout<<l<<' '<<r<<'\n'; else if(mp[ma]>=k)cout<<ma<<' '<<ma<<'\n'; else cout<<"-1\n"; } return 0; }
标签:last,int,mi,cin,len,maxl,week3 From: https://www.cnblogs.com/bible-/p/17056812.html