思路
这题是经典的贪心,思路很好想。
安排的朋友可以随意分配害羞等级,直接都分配成0就好了,那就意味着我们只需要从害羞等级低的人向害羞等级高的人去贪心就好了。
在贪心的过程中记一个 cnt表示当前已经站起来的人数,如果小于当前的害羞等级,就更新 ans+=当前的害羞等级- cnt。最后按输出方式输出就好了。
多组数据记得清零
代码
#include<bits/stdc++.h>
using namespace std;
int T,S,cnt,ans;
string s;
int main()
{
std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>T;
for(int t=1;t<=T;++t)//用t做for循环而不用while(T--)是为了更方便输出
{
ans=0;
cin>>S>>s;
cnt=s[0]-'0';
for(int i=1;i<s.size();++i)
{
if(i>cnt)//特判,如果当前有人不站起来,就分配几个朋友让他站起来
{
ans+=i-cnt;
cnt+=i-cnt;
}
cnt+=s[i]-'0';
}
cout<<"Case #"<<t<<": "<<ans<<"\n";
}
return 0;
}