1. 程式人生 > >Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 預處理優化】

Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 預處理優化】

long long i++ ins pat nds won not total mathjax

傳送門: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 }

Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 預處理優化】