传送门: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 (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.

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:

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 1e5+;
vector<vector<int> >vv(MAXN); //动态二维数组
int N, M; bool cmp(vector<int>a, vector<int>b) //自定义对一维数组的排序规则
{
return a.size() > b.size();
} int solve(int len)
{
int res = ;
// int tmp = 0;
for(int i = ; i < M; i++){
if(vv[i].size() < len) return res;
// tmp = 0;
// for(int k = 0; k < len; k++) //未预处理前缀和导致超时
// tmp += vv[i][k];
// if(tmp > 0) res+=tmp;
if(vv[i][len-] > ) res+=vv[i][len-];
}
return res;
} int main()
{
int no, x, slen, ans;
while(~scanf("%d%d", &N, &M)){
slen = ;
for(int i = ; i <= N; i++){
scanf("%d%d", &no, &x);
no--; //注意因为数组下标从 0 开始
vv[no].push_back(x);
if(vv[no].size() > slen) slen = vv[no].size();
}
for(int i = ; i < M; i++){
sort(vv[i].begin(), vv[i].end(), std::greater<int>()); //降序排序
} for(int i = ; i < M; i++){ //预处理前缀和
if(vv[i].size() == ) continue;
for(int k = ; k < vv[i].size(); k++)
vv[i][k] = vv[i][k-] + vv[i][k];
} sort(vv.begin(), vv.end(), cmp); //用于剪枝 // for(int i = 0; i < M; i++){
// printf("%d:", i+1);
// for(int k = 0; k < vv[i].size(); k++)
// printf(" %d", vv[i][k]);
// puts("");
// } ans = ;
for(int li = ; li <= slen; li++){ //枚举物品数量
ans = max(ans, solve(li));
}
printf("%d\n", ans); //初始化
for(int i = ; i < M; i++)
vv[i].clear(); }
return ;
}

Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】的更多相关文章

  1. Educational Codeforces Round 55 (Rated for Div. 2):C. Multi-Subject Competition

    C. Multi-Subject Competition 题目链接:https://codeforces.com/contest/1082/problem/C 题意: 给出n个信息,每个信息包含专业编 ...

  2. Educational Codeforces Round 55 (Rated for Div. 2) A/B/C/D

    http://codeforces.com/contest/1082/problem/A WA数发,因为默认为x<y = = 分情况讨论,直达 or x->1->y  or  x-& ...

  3. Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 【贪心 】

    传送门:http://codeforces.com/contest/1082/problem/B B. Vova and Trophies time limit per test 2 seconds ...

  4. Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))

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

  5. Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

    A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency

    E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...

  7. Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph

    D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...

  8. Educational Codeforces Round 55 (Rated for Div. 2)E

    题:https://codeforces.com/contest/1082/problem/E 题意:给出n个数和一个数c,只能操作一次将[L,R]之间的数+任意数,问最后该序列中能存在最多多少个c ...

  9. Educational Codeforces Round 55 (Rated for Div. 2)

    D. Maximum Diameter Graph 题意 给出每个点的最大度,构造直径尽可能长的树 思路 让度数大于$1$的点构成链,考虑是否能在链的两端加度为$1$的点 代码 #include &l ...

随机推荐

  1. maven配置编译器的版本

    发现每次启动idea时,项目里很多红色错误,都是的编译器版本不对,每次都要手动修改. 其实在pom里把默认编译器版本加进去就好了. <build> <plugins> < ...

  2. ServiceHelper

    public class ServiceHelper { private static string _baseUrl = $@"http://{Config.Instance.MesSer ...

  3. centos6.x硬件信息统计脚本

    #!/bin/bash Line='===========' #linux发行版名称 if [[ -f /usr/bin/lsb_release ]]; then OS=$(/usr/bin/lsb_ ...

  4. 在ASP.NET CORE中启用favicon.ico

    在静态页面中添加网站标志只需在<head>标签中添加<link rel="shortcut icon" href="favicon.ico" ...

  5. css样式margin padding border

  6. Oracle数据库查看已添加的索引和创建索引

    /** *查看目标表中已添加的索引 * */ --在数据库中查找表名 select * from user_tables where table_name like 'tablename%'; --查 ...

  7. Safari无痕模式下,storage被禁用问题

    前言 Safari开启无痕模式后,localStorage和sessionStorage为空,对其进行set操作也会报错,也就是说这种情况下,storage是被禁止使用了.接下来说一下解决方法. 解决 ...

  8. node定时任务

    var schedule = require('node-schedule') require('shelljs/global'); function scheduleCronstyle(){ sch ...

  9. 规范的web前端代码

    web前端的代码规范主要针对的是HTML,CSS和javaScript代码. 前端代码规范在不同场合会有差异,但是规范的前端代码应该具有以下特征: 1.符合标准 所谓的标准是指W3C指定的web标准, ...

  10. ueditor默认字体和字号修改

    ueditor编辑器默认字号是16号,默认字体为sans-serif,显得有些难看,所以决定修改默认值.配置文件ueditor.config.js可以修改整个编辑器配置项,里面有配置项fontfami ...