Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】
传送门:http://codeforces.com/contest/1082/problem/C
2 seconds
256 megabytes
standard input
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).
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.
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.
6 3
2 6
3 6
2 5
3 5
1 9
3 1
22
5 3
2 6
3 6
2 5
3 5
1 11
23
5 2
1 -1
1 -5
2 -1
2 -1
1 -10
0
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 预处理优化】的更多相关文章
- Educational Codeforces Round 55 (Rated for Div. 2):C. Multi-Subject Competition
C. Multi-Subject Competition 题目链接:https://codeforces.com/contest/1082/problem/C 题意: 给出n个信息,每个信息包含专业编 ...
- 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-& ...
- 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 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency
E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...
- Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph
D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...
- Educational Codeforces Round 55 (Rated for Div. 2)E
题:https://codeforces.com/contest/1082/problem/E 题意:给出n个数和一个数c,只能操作一次将[L,R]之间的数+任意数,问最后该序列中能存在最多多少个c ...
- Educational Codeforces Round 55 (Rated for Div. 2)
D. Maximum Diameter Graph 题意 给出每个点的最大度,构造直径尽可能长的树 思路 让度数大于$1$的点构成链,考虑是否能在链的两端加度为$1$的点 代码 #include &l ...
随机推荐
- js验证营业执照号码是否合规
需求:最近要做实名验证的功能,但是验证我们要验证严谨一点,参考了网上关于营业执照号码规则和一些大侠的代码的代码,总结一下. 营业执照号码规则:规则 代码: //方法一:function checkLi ...
- 在 Web 应用中创建 Node.js 应用程序
本分步指南将通过 Azure Web 应用帮助您启动并运行示例 Node.JS 应用程序.除 Node.JS 外,Azure Web 应用还支持其他语言,如 PHP..NET.Node.JS.Pyth ...
- C Primer Plus note2
warning: 'mmin' is used uninitialized in this function [-Wuninitialized]| 编译器出现如上图的警告,是因为变量‘mmin’没有初 ...
- Web前端面试指导(十五):CSS样式-display有哪些作用?
题目点评 其实就是要你说清楚该属性有哪些值,每个值都有什么作用,这个题目可以答得很简单,但要答全也并非是一件容易的事情. 元素默认的display值的情况如下(这个一般很少人注意这一点) block( ...
- 织梦后台添加友情链接的方法(flink标签)
标记名称:flink[标签简介][功能说明]:用于获取友情链接,其对应后台文件为"includetaglibflink.lib.php".[适用范围]:全局标记,适用V55,V56 ...
- linux多线程编程——读者优先、写者优先问题
读者优先描述 如果读者来: 1) 无读者.写着,新读者可以读: 2) 无写者等待,但有其他读者正在读,新读者可以读: 3) 有写者等待,但有其他读者正在读,新读者可以读: 4) 有写者写,新读者等 如 ...
- 12_Redis缓存穿透
[何为缓存穿透] 缓存穿透是查询一个一定不存在的数据,这样的请求都要到存储层MySql去查询,失去了缓存的意义,在流量大时,可能MySql就挂掉了,要是有人利用不存在的key频繁攻击我们的应用,这就是 ...
- Python爬虫教程-14-爬虫使用filecookiejar保存cookie文件(人人网)
Python爬虫教程-14-爬虫使用filecookiejar保存cookie文件(人人网) 上一篇介绍了利用CookieJar访问人人网,本篇将使用filecookiejar将cookie以文件形式 ...
- Unity使Text 文字逐个出现
Text tex; string s="Unity使Text 文字逐个出现"; //字符出现间隔 waitTime = 0.3f; // float speed=0; //方法一 ...
- c windows控制台输出颜色文字
#include <windows.h> //设置文字颜色void SetColor(int ForgC){ WORD wColor; //We will need this handle ...