ABC323_c题解

· · 题解

题意

有 n 个选手和 m 道题,每题有不同的分值,现给出各题目分值和各选手每题的通过情况,求每一位选手要超过目前的最高分选手至少还需要通过几道题。数据保证没有人全部通过。每个人有基础分,为自己的编号。

思路分析

简简单单贪心,把题目分数排一遍序然后依次选取,如果已经做过就跳过该题,直至超过目前最高分。然后输出答案即可。

Code

#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read(){
    int x=0,w=1;
    char ch=0;
    while(ch<'0'||ch>'9'){
        if(ch=='-')w=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+(ch-'0');
        ch=getchar();
    }
    return x*w;
}
void write(int x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    static int sta[35];
    int top=0;
    do{
        sta[top++]=x%10,x/=10;
    }while(x);
    while(top)putchar(sta[--top]+'0');
}//快读快写不解释
struct node{
    int sum,num,ans;//选手分数,编号,需要通过的题目数
    string s;
};
struct problem{
    int point;//分值
    map<int,bool>mp; //储存选手是否已经通过此题
}pro[105];
node a[105];
int n,m;
bool cmp1(node x,node y){
    return x.sum>y.sum;;
}//排序找出最高分
bool cmp2(problem x,problem y){
    return x.point>y.point;
}//排序题目分值
bool cmp3(node x,node y){
    return x.num<y.num;
}
int main(){
    n=read();m=read();
    for(int i=1;i<=m;i++){
        pro[i].point=read();
    }//输入
    for(int i=1;i<=n;i++){
        cin>>a[i].s;
        a[i].num=i;
        a[i].sum=i;//初始分
        for(int j=0;j<a[i].s.size();j++){
            if(a[i].s[j]=='o'){
                a[i].sum+=pro[j+1].point;
                pro[j+1].mp[a[i].num]=true;
            }//累加分数,标记已经通过此题
        }
    }
    sort(a+1,a+1+n,cmp1);//对选手排序
    sort(pro+1,pro+1+m,cmp2);//对题目排序
    for(int i=2;i<=n;i++){//第一个已经是最高分所以从第二个开始
        int anss=0;//储存需要通过的题目数
        for(int j=1;j<=m;j++){//选取题目
            if(a[i].sum>=a[1].sum)break;//如果超过,退出
            if(pro[j].mp[a[i].num]==false){//如果没有做过此题,选取
                a[i].sum+=pro[j].point;
                anss++;//累加题目数
            }
        }
        a[i].ans=anss;//将通过题目数转移到选手结构体里面
    }
    sort(a+1,a+1+n,cmp3);//因为刚刚我们按分数排了一遍序,现在的顺序是乱的,所以按照编号排回去
    for(int i=1;i<=n;i++){
        write(a[i].ans);puts("");//输出答案
    }
    return 0;
}

但是我们发现,这个代码还是过于复杂,对选手排序只是为了找出最大值,而且排了两遍序,如果 n 再大一些可能会超时,我们可以优化此代码,去掉排序选手的步骤。

Code

#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read(){
    int x=0,w=1;
    char ch=0;
    while(ch<'0'||ch>'9'){
        if(ch=='-')w=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+(ch-'0');
        ch=getchar();
    }
    return x*w;
}
void write(int x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    static int sta[35];
    int top=0;
    do{
        sta[top++]=x%10,x/=10;
    }while(x);
    while(top)putchar(sta[--top]+'0');
}//快读快写不解释
struct node{
    int sum,num;//选手分数,编号
    string s;
};
struct problem{
    int point;//分值
    map<int,bool>mp; //储存选手是否已经通过此题
}pro[105];
node a[105];
int n,m,maxx=-1;
bool cmp(problem x,problem y){
    return x.point>y.point;
}//排序题目分值
int main(){
    n=read();m=read();
    for(int i=1;i<=m;i++){
        pro[i].point=read();
    }//输入
    for(int i=1;i<=n;i++){
        cin>>a[i].s;
        a[i].num=i;
        a[i].sum=i;//初始分
        for(int j=0;j<a[i].s.size();j++){
            if(a[i].s[j]=='o'){
                a[i].sum+=pro[j+1].point;
                pro[j+1].mp[a[i].num]=true;
            }//累加分数,标记已经通过此题
        }
        maxx=max(maxx,a[i].sum);//直接在线求出最大值
    }
    sort(pro+1,pro+1+m,cmp);//对题目排序
    for(int i=1;i<=n;i++){//循环从一开始
        if(a[i].sum==maxx){//如果该选手已经是最大值,那么输出 0 并且跳过
            puts("0");
            continue;
        }
        int anss=0;//储存需要通过的题目数
        for(int j=1;j<=m;j++){//选取题目
            if(a[i].sum>maxx)break;//如果超过,退出
            if(pro[j].mp[a[i].num]==false){//如果没有做过此题,选取
                a[i].sum+=pro[j].point;
                anss++;//累加题目数
            }
        }
        write(anss);//输出
        puts("");
    }
    return 0;
}
\text{The End}