题解:P13312 [GCJ 2012 Qualification] Dancing With the Googlers

· · 题解

题意

给出 Googler 的人数 n,令人惊讶的三元组数量 s,要求的最佳成绩 p,分别给出每位 Googler 的总分。求最多有多少个 Googler 的最佳成绩能达到 p。

思路

看到最多,想到贪心。

贪心方法:

逐个枚举,若这个 Googler 的总分能达到 3\times p-2,则答案加一。若这个 Googler 的总分能达到 3\times p-4 且 s>0 ,则答案加一, s 减一。

贪心证明

若这个 Googler 的总分能达到 3\times p-2,则这个 Googler 分数最坏一定能是 (p,p-1,p-1),满足最佳成绩能达到 p。

若这个 Googler 的总分能达到 3\times p-4 且 s>0,则这个 Googler 分数最坏一定能是 (p,p-2,p-2),满足最佳成绩能达到 p,是一个令人惊讶的三元组,且将任何一个总分能达到 3\times p-4 的 Googler 的分数作为令人惊讶的三元组都只能增加一个最佳成绩能达到 p 的 Googler。

若这个 Googler 的总分不能达到 3\times p-4,则无论如何这个 Googler 的最佳成绩都不能能达到 p。

代码

易错点:当 p=1 时,3\times p-1<0,而总分不能小于 0,故需特判。

#include<bits/stdc++.h>
using namespace std;
int t,s,p,n,a[100001];
int main(){
    cin>>t;
    for(int k=1;k<=t;k++){
        memset(a,0,sizeof(int));
        long long ans=0;
        cin>>n>>s>>p;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        for(int i=1;i<=n;i++){
            if(a[i]>=p+p+p-2)//若这个 Googler 的总分能达到 3*p-2 
                ans++;
            else if(a[i]>=max(p+p+p-4,1)&&s>0){//若这个 Googler 的总分能达到 3*p-4 且 s>0
                ans++;
                s--;
            }
        }
        cout<<"Case #"<<k<<": "<<ans<<endl;
    }
}

这题真是黄题吗?