题解:P13211 [GCJ 2015 Qualification] Standing Ovation

· · 题解

思路

简单贪心。

首先明确一点,为了让站起来鼓掌的人数越多,邀请的朋友的害羞等级肯定要设为 0。即邀请的朋友会立即站起来鼓掌。

我们按害羞程度由低到高处理观众,维护当前站起来鼓掌的人数 tot。如果 tot 小于当前观众的害羞程度 i,就邀请 i-tot 个朋友,然后将 tot 更新为 i 并将 tot 加上害羞程度为 i 的观众的数量。

时间复杂度 O(S_{max})。

代码

#include<bits/stdc++.h>
using namespace std;
int T,maxs,tot,ans;
string s; 
int main(){
    cin>>T;
    for(int t=1;t<=T;t++){
        cin>>maxs>>s;
        tot=ans=0;
        for(int i=0;i<=maxs;i++){
            if(tot<i){
                ans+=i-tot;
                tot=i;
            }
            tot+=s[i]-'0';
        }

        cout<<"Case #"<<t<<": "<<ans<<endl;
    }
    return 0;
}