传送门: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. JavaScript中变量声明以及数据类型

    JavaScript变量 变量名必须以字母或下划线("_")开头 变量也能以 $ 和 _ 符号开头(不过我们不推荐这么做) 变量名称对大小写敏感(y 和 Y 是不同的变量) 变量可 ...

  2. .Net程序员玩转Android系列之一~Java快速入门

    前言 前段时间受公司业务发展需要,探索性进入Android开发领域.一切从零开始,java基础,Java进阶,Android框架学习,Eclipse熟悉,最终到第一个即时通讯App完成,历经一个月的时 ...

  3. C# 委托,事件, 异步

    委托 ​ 委托可以说是方法的集合,里面放着方法列表,当调用的时候就会调用全部的方法列表 ​ 个人理解 : 当声明和创建时委托时, 它是个 对象 当使用委托,调用方法时,它是个 方法 声明委托类型 de ...

  4. golang学习之regexp

    regexp是golang标准库自带的正则校验包,使用: re, _ := regexp.Compile(`(\d+)年(\d+)月`) //判断是否匹配category类别搜索 ismatch := ...

  5. 第十三章.MySQL数据库与JDBC编程(下)

    JDBC的典型用法: JDBC4.2常用接口和类简介: DriverManager:用于管理JDBC驱动的服务类,程序中使用该类的主要功能是获取Connection对象,该类包含如下方法: publi ...

  6. CentOS新增硬盘,重新扫描总线

    Centos 新增硬盘以后,系统不能自动进行识别. 1. 由于不知道新增硬盘挂载的位置,可以先查看现有硬盘挂载的适配器. [root@localhost ~]# ls -l /sys/block/sd ...

  7. Java中线程的实现

    在Java中要想实现多线程代码有两种方法,一种是继承 Thread 类,另一种就是实现 Runnable 接口 一.继承 Thread 类 Thread 类是在 java.lang 包中定义的,一个类 ...

  8. MySQL数据库(4)----生成统计信息

    MySQL最有用的一项功能就是,能够对大量原始数据进行归纳统计. 1.在一组值里把各个唯一的值找出来,这是一项典型的统计工作,可以使用DISTINCT 关键字清楚查询结果里重复出现的行.例如,下面的查 ...

  9. Element和vue框架报错提示

    上面报错提示Error in render function: "TypeError:Cannot read property '$options' of undefined" 就 ...

  10. SVN常用功能介绍(二)

    说明 上一章节主要描述了SVN的简介.安装搭建,和项目管理人员对SVN的常用操作. 这章主要讲解,SVN对应角色组员,在实际运用中的常用操作. 将SVN服务器项目导入到开发组员的本地电脑里 方式一: ...