传送门: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. [转]OData/WebApi

    本文转自:https://github.com/OData/WebApi/tree/vNext OData Web API Introduction OData Web API (i.e., ASP. ...

  2. JS大写转小写小写转大写,JS字符串大小写互换

    Array.prototype.map.call(str,a=>a.toUpperCase(a)==a?a.toLowerCase():a.toUpperCase()).join(''); 效果 ...

  3. SpringCloud实战之初级入门(一)— eureka注册中心

    目录 写在前面 1.资料目录 2.环境介绍 3.eureka注册中心 3.1 创建工程 3.2 启动工程 5.eureka注册中心集群高可用 6.结语 7.一点点重要的事情 写在前面 我在软件行业浸泡 ...

  4. Ajax实现页面跳转与结果返回

    ajax实现页面局部跳转与结果返回 1.带有结果返回的提交过程 这里用一个提交按钮来演示,HTML代码为: <input type="button" class=" ...

  5. unity3d之实现各种滑动效果

    一. 点击滑动页面 新建了一个带mask的prefab,加上代码只需要将图片prefab.按钮prefab和所想添加的图片 拖进去会自动生成按钮,滑动速度可以随意调time,滑动效果用itween实现 ...

  6. 多版本python如何切换

    一.在命令行中 通过py -x 二.在py文件中 头部字段添加 #!python2 或 #!python3 即可调用相应版本解释器 命令行调用python:py helloworld.py

  7. csharp:Microsoft.Ink 手写识别(HandWriting Recognition)

    /* 下載: //Microsoft Windows XP Tablet PC Edition 2005 Recognizer Pack http://www.microsoft.com/zh-cn/ ...

  8. cnpm 安装

    国内npm 安装比较慢,可选择cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org

  9. #学习tips——写给自己的语录

    用编程证明自己的观点! "我以为我懂了?"-- 花1/3的时间去学去记,剩下的时间应用做记忆实战. 记录一个转变 2018.6.7 (英文字幕-->无字幕!)good job ...

  10. 微信小程序开发5-WXML

    1.HTML元素是构建网页的一种单位,是由HTML标签和HTML属性组成的,HTML元素也是网页中的一种基本单位.HTML与其他标记语言一样,HTML的关键,是标签(tag).HTML标签是HTML语 ...