GCJ 2015-Qualification-A Standing Ovation 难度:0
https://code.google.com/codejam/contest/6224486/dashboard#s=p0
肯定把朋友们都设置在第0位,在第i位前面必须至少有i个人鼓掌,所以答案加上i-sum[i-1]即可(sum是从第0位开始到第i-1位结束包括朋友的总人数)
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=1e4; int n; int sum[maxn]; char buf[maxn]; int main(){ freopen("data.in","r",stdin); freopen("data.out","w",stdout); int T; scanf("%d",&T); for(int ti=1;ti<=T;ti++){ scanf("%d%s",&n,buf); memset(sum,0,sizeof(sum)); int ans=0; sum[0]=buf[0]-'0'; for(int i=1;i<=n;i++){ if(sum[i-1]<i){ ans+=i-sum[i-1]; sum[i-1]=i; } sum[i]=sum[i-1]+buf[i]-'0'; } printf("Case #%d: %d\n",ti,ans); } return 0; }