C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outputadmin管理员组文章数量:1530048
A multi-subject competition is coming! The competition has mm different subjects participants can choose from. That's why Alex (the coach) should form a competition delegation among his students.
He has nn candidates. For the ii-th person he knows subject sisi the candidate specializes in and riri — a skill level in his specialization (this level can be negative!).
The rules of the competition require each delegation to choose some subset of subjects they will participate in. The only restriction is that the number of students from the team participating in each of the chosen subjects should be the same.
Alex decided that each candidate would participate only in the subject he specializes in. Now Alex wonders whom he has to choose to maximize the total sum of skill levels of all delegates, or just skip the competition this year if every valid non-empty delegation has negative sum.
(Of course, Alex doesn't have any spare money so each delegate he chooses must participate in the competition).
InputThe first line contains two integers nn and mm (1≤n≤1051≤n≤105, 1≤m≤1051≤m≤105) — the number of candidates and the number of subjects.
The next nn lines contains two integers per line: sisi and riri (1≤si≤m1≤si≤m, −104≤ri≤104−104≤ri≤104) — the subject of specialization and the skill level of the ii-th candidate.
OutputPrint the single integer — the maximum total sum of skills of delegates who form a valid delegation (according to rules above) or 00 if every valid non-empty delegation has negative sum.
Examples input Copy6 3 2 6 3 6 2 5 3 5 1 9 3 1output Copy
22input Copy
5 3 2 6 3 6 2 5 3 5 1 11output Copy
23input Copy
5 2 1 -1 1 -5 2 -1 2 -1 1 -10output Copy
0Note
In the first example it's optimal to choose candidates 11, 22, 33, 44, so two of them specialize in the 22-nd subject and other two in the 33-rd. The total sum is 6+6+5+5=226+6+5+5=22.
In the second example it's optimal to choose candidates 11, 22 and 55. One person in each subject and the total sum is 6+6+11=236+6+11=23.
In the third example it's impossible to obtain a non-negative sum.
题意就是选科目,每科人数必须相同,总和尽量大。
有一个坑,可以往里面加入负数,只要该科总和>0就可以,具体代码。
代码:
1 //C 2 #include<bits/stdc++.h> 3 using namespace std; 4 typedef long long ll; 5 const int maxn=1e5+10; 6 const int inf=0x3f3f3f3f; 7 8 vector<int> ve[maxn]; 9 vector<ll> sum[maxn]; 10 int sz[maxn]; 11 ll ans[maxn]; 12 13 bool cmp(int a,int b) 14 { 15 return a>b; 16 } 17 18 int main() 19 { 20 int n,m; 21 cin>>n>>m; 22 for(int i=1;i<=n;i++){ 23 int s,r; 24 cin>>s>>r; 25 ve[s].push_back(r); 26 } 27 for(int i=1;i<=m;i++) 28 sort(ve[i].begin(),ve[i].end(),cmp); 29 for(int i=1;i<=m;i++){ 30 int pre=0; 31 for(int j=0;j<ve[i].size();j++){ 32 if(j==0) sum[i].push_back(ve[i][j]),pre=ve[i][j]; 33 else sum[i].push_back(pre+ve[i][j]),pre=sum[i][j]; 34 } 35 } 36 int maxx=0; 37 for(int i=1;i<=m;i++){ 38 maxx=max(maxx,(int)ve[i].size()); 39 for(int j=0;j<ve[i].size();j++){ 40 ans[j]=max(ans[j],ans[j]+sum[i][j]); 41 } 42 } 43 ll ret=0; 44 for(int i=0;i<maxx;i++) 45 ret=max(ret,ans[i]); 46 cout<<ret<<endl; 47 }
转载于:https://wwwblogs/ZERO-/p/10046655.html
本文标签: SubjectcompetitionMulticodeforcesDiv
版权声明:本文标题:Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2... 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1726694423a1081047.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论