Scores of Final Examination
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll mod=10000; int n,m; int c[1006][66]; int p[1006]; int main() { while(scanf("%d%d",&m,&n)!=EOF) { if(!m&&!n)break; int mx=0; memset(p,0,sizeof(p)); for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ scanf("%d",&c[i][j]); p[j]+=c[i][j]; mx=max(mx,p[j]); } } printf("%d ",mx); } return 0; }
On-Screen Keyboard
#include <bits/stdc++.h> using namespace std; struct node { int x,y; friend bool operator <(node a,node b) { return a.x<b.x; } }; map<char,node> mm; string s; int n,m; int main() { while (~scanf("%d%d", &n, &m)!=EOF) { mm.clear(); int ans=0;getchar(); if (n==0&&m==0) break; for(int i=1;i<=n;++i) { getline(cin,s); //cout<<s<<endl; for(int j=0;s[j];++j) { if(s[j]=='_') continue; node t; t.x=i; t.y=j+1; mm[s[j]]=t; } } getline(cin,s); // cout<<s<<endl; ans+=mm[s[0]].x+mm[s[0]].y-1; for(int i=1;s[i];++i) { ans+=abs(mm[s[i]].x-mm[s[i-1]].x)+abs(mm[s[i]].y-mm[s[i-1]].y)+1; } printf("%d ", ans); } }
Tally Counters
#pragma GCC optimize(3,"Ofast","inline") #include <bits/stdc++.h> using namespace std; void read(int &x) { int f=1;x=0;char s=getchar(); while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();} while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();} x*=f; } int n,m; int ans; const int maxn=1e3+5; int a[maxn]; int x[maxn]; priority_queue<int>q; int main() { while(1) { read(n); read(m); if(!n&&!m) break; ans=0; while(!q.empty()) q.pop(); for(register int i=1;i<=n;++i) read(a[i]); for(register int i=1;i<=n;++i) { int t; read(t); if(t>=a[i]) x[i]=t-a[i]; else x[i]=m-a[i]+t; } for(register int i=1;i<=n;++i) { if(x[i]>x[i-1]) { int t=x[i]-x[i-1]; ans+=t; if(!q.empty()) { int s=q.top(); if(m-s<t) { ans=ans-t+m-s; q.pop(); q.push(m-t); } } } else if(x[i]<x[i-1]) { q.push(x[i-1]-x[i]); } } printf("%d ",ans); } return 0; }
Balance Scale
#pragma GCC optimize(3,"Ofast","inline") #include <bits/stdc++.h> using namespace std; typedef long long ll; unordered_map<ll,bool> m1; int n,m,cnt; const int maxn=6e4; ll a[maxn],tot1,tot2,ans; ll w[105],b[105]; inline void dfs(int now,ll sum) { if(now>n) { m1[sum]=1; return; } dfs(now+1,sum); dfs(now+1,sum+w[now]); dfs(now+1,sum-w[now]); } inline ll read() { ll res=0,f=1; char ch=getchar(); while (!isdigit(ch)) { if (ch=='-') { f=-f; } ch=getchar(); } while (isdigit(ch)) { res=(res<<3)+(res<<1)+ch-'0'; ch=getchar(); } return f*res; } int main() { while (1) { m=read();n=read(); if (n == 0 && m == 0) break; m1.clear(); cnt = tot1 = tot2 = 0; ans = 0x3f3f3f3f3f3f3f3f; for (int i = 1; i <= m; ++i) a[i]=read(); for (int i = 1; i <= n; ++i) w[i]=read(); dfs(1, 0); unordered_map<ll, bool>::iterator it; for (int i = 1; i <= m; ++i) { if (m1[a[i]]) cnt++; else { b[tot2++] = a[i]; m1.erase(a[i]); } } for (it = m1.begin(); it != m1.end(); ++it) a[tot1++] = it->first; if (cnt == m) { printf("0 "); continue; } for (int i = 0; i < tot1; ++i) { cnt = 1; ll x = abs(a[i] - b[0]); for (int j = 1; j < tot2; ++j) { if (m1.count(b[j] - x) || m1.count(b[j] + x)) { cnt++; } } if (cnt == tot2) { ans = min(ans, x); } } if (ans == 0x3f3f3f3f3f3f3f3f) printf("-1 "); else printf("%lld ", ans); } }