Codeforces 1082C Multi-Subject Competition(前缀+思维)

题目链接:Multi-Subject Competition

题意:给定n名选手,每名选手都有唯一选择的科目si和对应的能力水平。并且给定科目数量为m。求选定若干个科目,并且每个科目参与选手数量相同的情况下的最大能力水平。

题解:每位选手扔到对应的科目里面从1-m遍历科目,能力值排序下,维护下能力值和,大于0就给到当前位置人数答案加上该值,否则跳出(给负价值是没有意义的),最后遍历一遍人数对应的价值,拿最大的即可。

 1 #include <vector>
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int N=1e5+10;
 8 int ans[N];
 9 vector <int> v[N];
10 
11 int main(){
12     int n,m,s,r,mx=0;
13     scanf("%d%d",&n,&m);
14     for(int i=1;i<=n;i++){
15         scanf("%d%d",&s,&r);
16         v[s].push_back(r);
17     }
18     for(int i=1;i<=m;i++){
19         int sum=0,sz=v[i].size();
20         sort(v[i].begin(),v[i].end());
21         mx=max(mx,sz);
22         for(int j=sz-1;j>=0;j--){
23             sum+=v[i][j];
24             if(sum>=0) ans[sz-j]+=sum;
25             else break;
26         }
27     }
28     int res=0;
29     for(int i=1;i<=mx;i++) res=max(res,ans[i]);
30     printf("%d\n",res);
31     return 0;
32 }
View Code

猜你喜欢

转载自www.cnblogs.com/ehanla/p/10039827.html