Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition vector 预处理优化
Posted ymzjj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition vector 预处理优化相关的知识,希望对你有一定的参考价值。
传送门:http://codeforces.com/contest/1082/problem/C
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).
The 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.
Print 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.
6 3 2 6 3 6 2 5 3 5 1 9 3 1
22
5 3 2 6 3 6 2 5 3 5 1 11
23
5 2 1 -1 1 -5 2 -1 2 -1 1 -10
0
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.
题意概括:
有 M 种物品,有 N 条信息。
每条信息 no val 说明了 第 no 种物品能带来的收益(可累加)
要求选取的物品里,每种物品的数量要一样,求最大收益 。
解题思路:
N M 的范围开不了静态二维数组,需要使用 stl 里的 vector 开一个二维数组,用于记录没每种物品的收益。
首先对每种物品的收益按照 降序排序,这样就能贪心取到 k 个该种物品了。
但这样还不够,我们求一个收益的前缀和,这样第 k 个值就是 取得 k 个该种物品的最大收益了。
其次对每种物品的数量也进行降序排序,后面枚举物品数量时就可以剪枝了。
AC code:
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <vector> 7 #include <map> 8 #define INF 0x3f3f3f3f 9 #define LL long long 10 using namespace std; 11 const int MAXN = 1e5+10; 12 vector<vector<int> >vv(MAXN); //动态二维数组 13 int N, M; 14 15 bool cmp(vector<int>a, vector<int>b) //自定义对一维数组的排序规则 16 { 17 return a.size() > b.size(); 18 } 19 20 int solve(int len) 21 { 22 int res = 0; 23 // int tmp = 0; 24 for(int i = 0; i < M; i++){ 25 if(vv[i].size() < len) return res; 26 // tmp = 0; 27 // for(int k = 0; k < len; k++) //未预处理前缀和导致超时 28 // tmp += vv[i][k]; 29 // if(tmp > 0) res+=tmp; 30 if(vv[i][len-1] > 0) res+=vv[i][len-1]; 31 } 32 return res; 33 } 34 35 int main() 36 { 37 int no, x, slen, ans; 38 while(~scanf("%d%d", &N, &M)){ 39 slen = 0; 40 for(int i = 1; i <= N; i++){ 41 scanf("%d%d", &no, &x); 42 no--; //注意因为数组下标从 0 开始 43 vv[no].push_back(x); 44 if(vv[no].size() > slen) slen = vv[no].size(); 45 } 46 for(int i = 0; i < M; i++){ 47 sort(vv[i].begin(), vv[i].end(), std::greater<int>()); //降序排序 48 } 49 50 for(int i = 0; i < M; i++){ //预处理前缀和 51 if(vv[i].size() == 1) continue; 52 for(int k = 1; k < vv[i].size(); k++) 53 vv[i][k] = vv[i][k-1] + vv[i][k]; 54 } 55 56 sort(vv.begin(), vv.end(), cmp); //用于剪枝 57 58 // for(int i = 0; i < M; i++){ 59 // printf("%d:", i+1); 60 // for(int k = 0; k < vv[i].size(); k++) 61 // printf(" %d", vv[i][k]); 62 // puts(""); 63 // } 64 65 ans = 0; 66 for(int li = 1; li <= slen; li++){ //枚举物品数量 67 ans = max(ans, solve(li)); 68 } 69 printf("%d ", ans); //初始化 70 for(int i = 0; i < M; i++) 71 vv[i].clear(); 72 73 } 74 return 0; 75 }
以上是关于Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition vector 预处理优化的主要内容,如果未能解决你的问题,请参考以下文章
Educational Codeforces Round 7 A
Educational Codeforces Round 7
Educational Codeforces Round 90
Educational Codeforces Round 33