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[]函数,可以像数组一样的操作,而且还有边界检查,动态改变大小. 这里只介绍用它来代替二维的数组,二维以上的可以依此类推. ...
随机推荐
- Android Volley源码分析及扩展
转载请标明出处: http://www.cnblogs.com/why168888/p/6681232.html 本文出自:[Edwin博客园] Volley 介绍 Android系统中主要提供了两种 ...
- jersey之get,put,post,delete简单使用
要使用jersey首先要有相应的依赖包,获取方法有很多,本地下载依赖文件或maven获取,这里假设你的环境已经搭建好了.要使用jersey首先要初始化一个client客户端,下面是最简单的一个get请 ...
- BZOJ 3224 Tyvj 1728 普通平衡树模板
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 题目大意: 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以 ...
- Java基础知识强化之集合框架笔记78:ConcurrentHashMap之 ConcurrentHashMap、Hashtable、HashMap、TreeMap区别
1. Hashtable: (1)是一个包含单向链的二维数组,table数组中是Entry<K,V>存储,entry对象: (2)放入的value不能为空: (3)线程安全的,所有方法均用 ...
- [19/04/25-星期四] GOF23_结构型模式(适配器模式、代理模式)
一.引言 结构模式:核心作用就是从程序的结构上实现松耦合,从而扩大整体的类结构,用来解决更大的问题. 二.适配器模式(adapter) 生活中假设笔记本是标准的USB接口但是外置键盘是圆形接口,这时候 ...
- [运维笔记] Mysql单库备份脚本
工作中用到的Mysql单库备份Shell脚本,压缩备份,并在Crontab中添加计划任务,最多保存60天的备份 #!/bin/bash . /etc/profile USERNAME=zabbix P ...
- ERDAS IMAGINE 2014 32位 破解安装
1. 安装Install ERDAS Foundation 2014 2. 安装ERDAS IMAGINE 2014 32位 3. 安装Intergraph ...
- xmppframework 简介
XMPPFramework是一个OS X/iOS平台的开源项目,使用Objective-C实现了XMPP协议(RFC-3920),同时还提供了用于读写XML的工具,大大简化了基于XMPP的通信应用的开 ...
- DPDK测试用例(sample)编译
前言 要使用DPDK的测试用例,必须先进行编译,以此记录编译的操作,方便日后查找 编译用例 设置环境变量,将DPDK的目录路径添加到编译代码中,RTE_SDK指示DPDK目录路径: export RT ...
- 【JavaScript-基础-文件上传】
Upload 最原始方式 form表单提交 // html <form method="get" action="/test/upload"> &l ...