Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】...

本文解析了一道Codeforces竞赛题目,探讨如何在多种科目中选择参赛者以最大化团队总技能水平。介绍了动态规划和贪心算法的应用,以及如何通过排序和前缀和优化解决方案。

传送门:http://codeforces.com/contest/1082/problem/C

C. Multi-Subject Competition
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

Input

The first line contains two integers nn and mm (1n1051≤n≤105, 1m1051≤m≤105) — the number of candidates and the number of subjects.

The next nn lines contains two integers per line: sisi and riri (1sim1≤si≤m, 104ri104−104≤ri≤104) — the subject of specialization and the skill level of the ii-th candidate.

Output

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.

Examples
input
Copy
6 3
2 6
3 6
2 5
3 5
1 9
3 1
output
Copy
22
input
Copy
5 3
2 6
3 6
2 5
3 5
1 11
output
Copy
23
input
Copy
5 2
1 -1
1 -5
2 -1
2 -1
1 -10
output
Copy
0
Note

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\n", ans);                //初始化
70         for(int i = 0; i < M; i++)
71             vv[i].clear();
72 
73     }
74     return 0;
75 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值