vector的二维用法+前缀和
题目链接:https://codeforces.com/contest/1082/problem/C(C. Multi-Subject Competition)
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 (≤n≤≤n≤, ≤m≤≤m≤) — the number of candidates and the number of subjects. The next nn lines contains two integers per line: sisi and riri (≤si≤m1≤si≤m, −≤ri≤−≤ri≤) — 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 if every valid non-empty delegation has negative sum. Examples
input output input output input -
-
-
-
-
output 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.
题目
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.
题目意思:
给定n(备选人数),m(科目数目)。
后n行输入,每行两个整数,x和y
第i行输入,第i个学生 第x科目的y等级
输出等级之和最好情况(唯一要求是 参赛的科目中人数是相等的)

盲点:
对于vector 类型来说,存在下列参数 greater<int>() 和less<int>()
- sort(v[i].begin(),v[i].end(),greater<int>()); //默认从大到小
- sort(v[i].begin(),v[i].end(),less<int>()); //默认从小到大
- 当然也可以手写cmp,传入cmp。
AC代码:
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>a[];
int cmp(const int a,const int b){
return a>b;
}
int sum[];
int main()
{
int n,m;
while (cin>>n>>m){
vector <int>::iterator it;
int x,y;
memset(sum,,sizeof(sum));
for (int i=;i<n;i++){
cin>>x>>y;
a[x].push_back(y);
}
for (int i=;i<=n;i++){
sort(a[i].begin(),a[i].end(),cmp);
}
for (int i=;i<=m;i++){
for (int j=;j<a[i].size();j++){
a[i][j]+=a[i][j-];
}
}
int ans=;
for (int i=;i<=m;i++){
for (int j=;j<a[i].size();j++){
if (a[i][j]>){
sum[j]+=a[i][j];
// printf("sum[%d]=%d\n",j,sum[j]);
ans=max(ans,sum[j]);
}
}
a[i].clear();
}
printf("%d\n",ans);
}
return ;
}
vector的二维用法+前缀和的更多相关文章
- 洛谷 P2701 [USACO5.3]巨大的牛棚Big Barn Label:二维数组前缀和 你够了 这次我用DP
题目背景 (USACO 5.3.4) 题目描述 农夫约翰想要在他的正方形农场上建造一座正方形大牛棚.他讨厌在他的农场中砍树,想找一个能够让他在空旷无树的地方修建牛棚的地方.我们假定,他的农场划分成 N ...
- python排序之二冒泡排序法
python排序之二冒泡排序法 如果你理解之前的插入排序法那冒泡排序法就很容易理解,冒泡排序是两个两个以向后位移的方式比较大小在互换的过程好了不多了先上代码吧如下: 首先还是一个无序列表lis,老规矩 ...
- 二值法方法综述及matlab程序
在某些图像处理当中一个关键步是二值法,二值化一方面能够去除冗余信息,另一方面也会使有效信息丢失.所以有效的二值化算法是后续的处理的基础.比如对于想要最大限度的保留下面图的中文字,以便后续的定位处理. ...
- 智课雅思词汇---二十、前缀syn-sym-syl是什么意思
智课雅思词汇---二十.前缀syn-sym-syl是什么意思 一.总结 一句话总结:l,m,n是可以互换 前缀:sy-, syn-, sym-, syl- [词根含义]:共同,同时 [词根来源]:(s ...
- Codeforces 519D A and B and Interesting Substrings(二维map+前缀和)
题目链接:http://codeforces.com/problemset/problem/519/D 题目大意:给你一串字符串s仅由小写字母组成,并且对于'a'~'z'都给了一个值.求子串t满足t的 ...
- [LeetCode] Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- C++ vector 实现二维数组时, 在类的头文件中定义时遇到"应输入类型符"的问题?
见下,当我在类的声明文件中定义二维vector时,提示我应输入类型说明符; 但是相同的格式定义,在类中将二维vector修改为在源文件中定义就可以顺利通过,并顺利执行打印 打印结果如下: 望大神来解惑 ...
- vector创建二位数组
默认初始化vector vector<vevtor<int> > arr(row, vector<int>(col, 0)); //指定行大小为row,列为col, ...
- vector作为二维数组
vector本来就是可以用来代替一维数组的,vector提供了operator[]函数,可以像数组一样的操作,而且还有边界检查,动态改变大小. 这里只介绍用它来代替二维的数组,二维以上的可以依此类推. ...
随机推荐
- Java的Stream流
yi.控制台输入输出流, 1.读取控制台输入 Java的控制台输入由System.in完成.为了获得一个绑定到控制台的字符流,可以把System.in包装在一个BufferedReader对象中来创建 ...
- 【[SDOI2011]拦截导弹】
这道题是真的蛇皮 方案数要开\(double\)真的蛇皮 首先\(dp\)是非常容易看出来的 设\(dp[i]\)表示以\(i\)结尾的最长子序列 显然转移方程为 \[dp[i]=max(dp[j]+ ...
- urllib库基本使用
#导入urllib库 import urllib.request #打开网址 file=urllib.request.urlopen("http://www.sohu.com/", ...
- 5、Android-跨程序共享数据--内容提供器
Android数据持久化技术:文件存储.SharedPreferences存储.数据库存储 使用这些持久化技术保存的数据只能再当前的应用程序中访问 但是对于不同应用之间的可以实现跨程序数据共享的功能 ...
- 4、Web Service-Jaxws(Eclipse版本)实现查看天气和手机归属地
1.前提概要 免费的官网:http://www.webxml.com.cn/zh_cn/web_services.aspx 官网提供了各种免费的webservice 我们使用的是:http://ws. ...
- 跳转到系统设置界面 iOS
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; if ([[UIApplication sharedApp ...
- poj 2253 Frogger 最小瓶颈路(变形的最小生成树 prim算法解决(需要很好的理解prim))
传送门: http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- 基于MySql数据库的单表与多表联合查询
这里以学生 班级 身份证 以及课程为例 1,启动MySql数据库 开启服务 2.1.0新建一张班级表 备注:CHARSET = UTF8 (指定编码格式为utf8 防止中文乱码) /*班级表*/ C ...
- 分布式消息通信ActiveMQ
消息中间件 消息中间件是指利用高效可靠的消息传递机制进行平台无关的数据交流,并且基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,可以在分布式架构下扩展进程之间的通信. 消息中间件能 ...
- 批量kill杀死某些会话session的PL/SQL
原文:http://blog.itpub.net/9240380/viewspace-666622/ SQL> declare 2 v_sid v$session.sid%type; --定义如 ...