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

    // // 摘要: // 通过使用默认的相等比较器对值进行比较生成两个序列的差集. // // 参数: // first: // 一个 System.Collections.Generic.IEnum ...

  2. 互联网轻量级框架SSM-查缺补漏第九天

    简言: 第九章 Spring Ioc的概念 IoC(Inversion of Control)控制反转:比如想喝橙汁,在没有饮品店的日子,最直观的做法是买果汁机.橙汁.这是你自己“主动”创造的过程,也 ...

  3. POJ 1185 炮兵阵地 经典的 状态压缩dp

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16619   Accepted: 6325 Description ...

  4. svn 创建本地仓库

    1. svnadmin create ~/repository 2. svnserve -d -r ~/repository 3. svn checkout file://~/repository $ ...

  5. zookeeper学习实践1-实现分布式锁

    引言 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提 ...

  6. PHP获取当前时间戳

    PHP提供了专门的获取当前时间戳的函数,那就是time()函数.   time()函数获取当前的UNIX时间戳,返回值为从时间戳纪元(格林威治时间1970年1月1日 00:00:00)到当前的秒数.  ...

  7. PHP支持多线程吗?

    https://zhidao.baidu.com/question/2053529640037778107.html

  8. 小白学flask之静态文件

    引入css的方式有两种 1 那在flask中,如何处理静态文件? 做法很简单,只要在你的包或模块旁边创建一个名为 static 的文件夹就行了. flask的静态文件是位于应用的 /static 中的

  9. js和jq获取宽度和高度

    Javascript: console.log(document.body.clientWidth); //网页可见区域宽(body) console.log(document.body.client ...

  10. jQuery和css3控制箭头丝滑旋转

    问题: 我们经常会遇见点击一个小三角使之丝滑的旋转180度上下旋转,怎么实现呢,需要css3搭配jq 来处理 如图:1.点击前 2.点击后(效果丝滑旋转)                 1.html ...