A 逆序数:https://www.nowcoder.com/acm/contest/77/A
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <bits/stdc++.h> using namespace std; int main() { int n; while(~scanf("%d",&n)&&n) { int arr[n+5],maxa=0,sign[100005],cnt[100005]; long long sum=0; memset(sign,0,sizeof(sign)); memset(cnt,0,sizeof(cnt)); for(int i=1;i<=n;i++) { scanf("%d",&arr[i]); maxa=max(maxa,arr[i]); sign[arr[i]]++; for(int j=arr[i]+1;j<=maxa;j++) cnt[arr[i]]+=sign[j]; } for(int i=0;i<=maxa;i++) sum+=cnt[i]; printf("%lld\n",sum); } return 0; }
同一个思路 别人的代码真是精简
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <bits/stdc++.h> using namespace std; int main() { int n,k; scanf("%d",&n); long long sum=0; int a[100005]; memset(a,0,sizeof(a)); while(n--) { scanf("%d",&k); sum+=a[k]; for(int i=0;i<k;i++) a[i]++; } printf("%lld\n",sum); return 0; }
B Big Water Problem:https://www.nowcoder.com/acm/contest/77/B
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <bits/stdc++.h> using namespace std; int main() { int n,m; while(~scanf("%d%d",&n,&m)&&n&&m) { long long b[n+5],num; memset(b,0,sizeof(b)); for(int i=1;i<=n;i++) { scanf("%lld",&num); if(i==1) b[i]=num; else b[i]=b[i-1]+num; } while(m--) { int sel,x,y; scanf("%d%d%d",&sel,&x,&y); if(sel==1) { for(int i=x;i<=n;i++) b[i]+=y; } else printf("%lld\n",b[y]-b[x-1]); } } return 0; }
C 字符串的问题:https://www.nowcoder.com/acm/contest/77/C
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <bits/stdc++.h> using namespace std; char ch[1000005],ans[1000005],lans[1000005]; int flag,lch; void midjudge(int len) { int k=0,j; for(j=1;j<lch-1;j++) { if(ch[j]==ans[k]) k++; else k=0; if(k==len+1) break; } if(j<lch-1) { strcpy(lans,ans); flag=1; } return ; } void judge(int len) { int k=0,j; for(j=lch-1-len;j<lch;j++) if(ans[k++]==ch[j]) continue; if(j==lch) midjudge(len); return ; } int main() { while(gets(ch)) { memset(ans,0,sizeof(ans)); flag=0,lch=strlen(ch); int j=0; for(int i=0;i<lch-1;i++) { ans[j++]=ch[i]; if(ch[i]==ch[lch-1]) judge(i); } if(flag) puts(lans); else printf("Just a legend\n"); } return 0; }
D 集合问题:https://www.nowcoder.com/acm/contest/77/D
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<bits/stdc++.h> using namespace std; int a,b,n,ans[100005],p[100005]; map<int,int>m; bool dfs(int num,int c) { if(c==2) { if(ans[m[b-num]]==-1) { ans[m[b-num]]=ans[m[num]]=1; return 1; } if(ans[m[a-num]]==-1) { ans[m[a-num]]=ans[m[num]]=0; return 1; } if(ans[m[a-num]]==1) { ans[m[a-num]]=ans[m[num]]=0; return dfs(b-a+num,1); } return 0; } else { if(ans[m[a-num]]==-1 || ans[m[a-num]]==0) { ans[m[a-num]]=ans[m[num]]=0; return 1; } if(ans[m[a-num]]==1) { ans[m[a-num]]=ans[m[num]]=0; return dfs(b-a+num,1); } return 0; } } int main() { scanf("%d%d%d",&n,&a,&b); for(int i=1;i<=n;i++) { scanf("%d",&p[i]); m[p[i]]=i; ans[i]=-1; } for(int i=1;i<=n;i++) if(ans[i]==-1) { if(dfs(p[i],2)); else { printf("NO\n"); return 0;} } printf("YES\n"); for(int i=1;i<n;i++) printf("%d ",ans[i]); printf("%d\n",ans[n]); return 0; }
E 情人节的灯泡:https://www.nowcoder.com/acm/contest/77/E
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <bits/stdc++.h> int main() { int n,m; while(~scanf("%d%d",&n,&m)&&n&&m) { int arr[n+5][n+5],sel; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&arr[i][j]); while(m--) { scanf("%d",&sel); if(sel==1) { int x,y; scanf("%d%d",&x,&y); arr[x][y]= arr[x][y] ? 0:1 ; } else { int x1,y1,x2,y2,sum=0; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); for(int i=x1;i<=x2;i++) for(int j=y1;j<=y2;j++) sum+=arr[i][j]; printf("%d\n",sum); } } } return 0; }
F The Biggest Water Problem:https://www.nowcoder.com/acm/contest/77/F
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <bits/stdc++.h> using namespace std; int main() { long long n; while(~scanf("%lld",&n)&&n>0) { long long ans=0; while(n>9) { ans=0; while(n) { ans+=n%10; n/=10; } n=ans; } printf("%lld\n",ans); } return 0; }
G 送分啦-QAQ:https://www.nowcoder.com/acm/contest/77/G
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<bits/stdc++.h> using namespace std; int main() { long long n,i,j; long long a[1005]; a[1]=1,a[2]=2; for(i=3;;i++) { a[i]=a[i-1]+a[i-2]; if(a[i]>1e9) break; } while(scanf("%lld",&n)!=EOF) { for(j=1;j<=i;j++) if(a[j]>=n) break; if(a[j]==n) printf("Sha\n"); else printf("Xian\n"); } return 0; }
H Tree Recovery:https://www.nowcoder.com/acm/contest/77/H
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <bits/stdc++.h> using namespace std; int n,m; long long arr[100005]; void opsC() { int a,b,c; scanf("%d%d%d",&a,&b,&c); for(int i=a;i<=b;i++) arr[i]+=c; } void opsQ() { int a,b; long long ans=0; scanf("%d%d",&a,&b); for(int i=a;i<=b;i++) ans+=arr[i]; printf("%lld\n",ans); } int main() { while(~scanf("%d%d",&n,&m)&&n&&m) { memset(arr,0,sizeof(arr)); for(int i=1;i<=n;i++) scanf("%lld",&arr[i]); while(m--) { char sel; getchar(); scanf("%c",&sel); if(sel=='Q') opsQ(); else opsC(); } } return 0; }