2022.04.18(Codeforces Round #780 (Div. 3))
见代码:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> using namespace std; int main(){ int t; scanf("%d",&t); while(t--){ int n; int a,b; scanf("%d %d",&a,&b); if(a==0){ printf("1\n"); }else if(b==0){ printf("%d\n",a+1); }else{ if(a==1&b==1){ printf("4\n"); }else{ printf("%d\n",a+1+b*2); } } } }
说明:只有最大和次大的值会影响最后的结果,见代码
wa掉的点:(1)数组不要越界
代码:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> using namespace std; const int maxx=2e5+10; int main(){ int t; scanf("%d",&t); while(t--){//1 3 3 3 3 int n; int a[maxx]; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } sort(a+1,a+n+1); if(abs(a[n]-a[n-1])<=1){ printf("YES\n"); }else{ printf("NO\n"); } } }
说明:我们只需要把最近的点对找出来,然后用总数减去这个数即可
代码:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<map> using namespace std; const int maxx=2e5+10; int main(){ int t; scanf("%d",&t); while(t--){//1 3 3 3 3 char s[maxx]; scanf("%s",&s); int n=strlen(s); map<char,int> mp; int sum=0; for(int i=0;i<n;i++){ if(mp.count(s[i])){ sum+=2; mp[s[i]]=0; mp.clear(); }else{ mp[s[i]]=1; } } printf("%d\n",n-sum); } }