Codeforces 1082C Multi-Subject Competition

https://vjudge.net/problem/CodeForces-1082C

题目:

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 teamparticipating 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 nnand mm (1≤n≤105, 1≤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≤m, −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 0 if every valid non-empty delegation has negative sum.

Examples

Input1

6 3
2 6
3 6
2 5
3 5
1 9
3 1

Output1

22

Input2

5 3
2 6
3 6
2 5
3 5
1 11

Output2


Input3

5 2
1 -1
1 -5
2 -1
2 -1
1 -10

Output3

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.

分析:

题目不复杂,很轻松就打出来了,wa了几次调了调bug,然后就TLE到死
本来以为前缀和绝对绝对会超时,所以简单暴力了,,,,
结果标答就是前缀和,真香

TLE代码:

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <time.h>
#include <queue>
#include <string.h>
#include <list>
#define sf scanf
#define pf printf
#define lf double
#define ll long long
#define p123 printf("123\n");
#define pn printf("\n");
#define pk printf(" ");
#define p(n) printf("%d",n);
#define pln(n) printf("%d\n",n);
#define s(n) scanf("%d",&n);
#define ss(n) scanf("%s",n);
#define ps(n) printf("%s",n);
#define sld(n) scanf("%lld",&n);
#define pld(n) printf("%lld",n);
#define slf(n) scanf("%lf",&n);
#define plf(n) printf("%lf",n);
#define sc(n) scanf("%c",&n);
#define pc(n) printf("%c",n);
#define gc getchar();
#define re(n,a) memset(n,a,sizeof(n));
#define len(a) strlen(a)
#define f(i,n) for(int i = 0; i < n; i ++)
#define LL long long
#define eps (1e-6)
using namespace std;
struct A{
int s;
int t; }a[];
bool cmp(A a,A b){
if(a.s < b.s)return true;
if(a.s == b.s && a.t > b.t)return true;
return false;
}
int kind[];
int main(){
re(kind,);
int n,m;
s(n) s(m)
int kinds = ;
f(i,n){
s(a[i].s) s(a[i].t)
kind[a[i].s] ++;
if(kinds < kind[a[i].s]){
kinds = kind[a[i].s];
}
}
//p(kinds)
if(kinds >= n- && n > ){
int sum00 = ;
f(i,n){
if(a[i].t >= ){
sum00 += a[i].t;
}
}
p(sum00) pn return ;
}
sort(a,a+n,cmp);
int maxi = ;
for(int i = kinds; i >= ; i --){
int num = ;
while(kind[num] < i){
num ++;
}
int count0 = i;
int sum = ;
int sum0 = ;
f(j,n){
if(a[j].s == num && count0 != ){
//p(a[j].s) pk p(a[j].t) pn
if(a[j].t < && count0 == i){
num ++;
while(kind[num] < i){
num ++;
}
continue;
}
sum0 += a[j].t;
count0 --;
}
if(count0 == ){
if(sum0 >= ){
sum += sum0;
}
sum0 = ;
num ++;
while(kind[num] < i){
num ++;
}
count0 = i;
}
}
//p(sum) pn if(maxi < sum){
maxi = sum;
} //pn
}
p(maxi) pn
return ;
}

1082C - Multi-Subject Competition

At first, it's optimal to take candidates with maximal levels for a fixed subject.

At second, if we fix number of participants in each subject for some delegation, then it's always optimal to choose all subjects with positive sum of levels.

It leads us to a following solution. Let's divide all candidates by it's sisi and sort each group in non-increasing order.

In result we can just iterate over all prefix sums for each group and update global answer of current length with current sum if it has a positive value.

 #include<bits/stdc++.h>
using namespace std; #define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define sz(a) int((a).size()) int n, m;
vector<int> s, r; inline bool read() {
if(!(cin >> n >> m))
return false;
s.assign(n, );
r.assign(n, ); fore(i, , n) {
assert(cin >> s[i] >> r[i]);
s[i]--;
}
return true;
} vector< vector<int> > subs; inline void solve() {
subs.assign(m + , vector<int>()); fore(i, , n)
subs[s[i]].push_back(r[i]); fore(id, , sz(subs)) {
sort(subs[id].begin(), subs[id].end());
reverse(subs[id].begin(), subs[id].end());
} vector<int> mx(n + , );
fore(id, , sz(subs)) {
int curSum = ;
fore(i, , sz(subs[id])) {
curSum += subs[id][i];
if(curSum < )
break; mx[i + ] += curSum;
}
} cout << *max_element(mx.begin(), mx.end()) << endl;
} int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
int tt = clock();
#endif
cout << fixed << setprecision(); if(read()) {
solve(); #ifdef _DEBUG
cerr << "TIME = " << clock() - tt << endl;
tt = clock();
#endif
}
return ;
}

Codeforces 1082C Multi-Subject Competition 前缀和 A的更多相关文章

  1. Codeforces 1082C Multi-Subject Competition(前缀+思维)

    题目链接:Multi-Subject Competition 题意:给定n名选手,每名选手都有唯一选择的科目si和对应的能力水平.并且给定科目数量为m.求选定若干个科目,并且每个科目参与选手数量相同的 ...

  2. Codeforces 1132C - Painting the Fence - [前缀和优化]

    题目链接:https://codeforces.com/contest/1132/problem/C 题意: 栅栏有 $n$ 个节,有 $q$ 个人可以雇佣来涂栅栏,第 $i$ 个人可以涂第 $l_i ...

  3. Codeforces 853B Jury Meeting (差分+前缀和)

    <题目链接> 题目大意: 有$ n(n<=1e5)$个城市和一个首都(0号城市),现在每个城市有一个人,总共有$ m (m<=1e5)$次航班,每个航班要么从首都起飞,要么飞到 ...

  4. CodeForces 838A Binary Blocks(前缀和)题解

    题意:给你个n*m的矩阵,要求你找到一个k,k > 1,使得矩阵可以分为很多k * k的小正方形,然后进行操作把每个小正方形都变为0或1,问你怎样使操作数最小. 思路:随便暴力不可取,显然你每次 ...

  5. Educational Codeforces Round 30 B【前缀和+思维/经典原题】

    B. Balanced Substring time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. codeforces 938F(dp+高维前缀和)

    题意: 给一个长度为n的字符串,定义$k=\floor{log_2 n}$ 一共k轮操作,第i次操作要删除当前字符串恰好长度为$2^{i-1}$的子串 问最后剩余的字符串字典序最小是多少? 分析: 首 ...

  7. Codeforces 106D Treasure Island 预处理前缀+暴力(水

    主题链接:点击打开链接 意甲冠军: 特定n*m矩阵 # 是墙 . 和字母是平地 最多有26个字母(不反复出现) 以下k个指令. 每一个指令代表移动的方向和步数. 若以某个字母为起点,依次运行全部的指令 ...

  8. Codeforces 948 数论推导 融雪前缀和二分check 01字典树带删除

    A. 全部空的放狗 B. 先O(NLOGNLOGN)处理出一个合数质因数中最大的质数是多少 因为p1 x1 x2的关系是 x2是p在x1之上的最小倍数 所以x1的范围是[x2-p+1,x2-1]要使最 ...

  9. C - Monitor CodeForces - 846D (二维前缀和 + 二分)

    Recently Luba bought a monitor. Monitor is a rectangular matrix of size n × m. But then she started ...

随机推荐

  1. day2模块初识别

    sys_mod.py import sys print(sys.argv) #['C:/Users/Administrator/desktop/s17/day2/sys_mod.py'] 打印脚本的绝 ...

  2. django 多线程下载图片

    example1: from multiprocessing.dummy import Pool as ThreadPool #多线程 import time import urllib2 urls ...

  3. storj白皮书v3最全面解读,Docker创始人的加入能否扳倒AWS S3

    Storj新发了白皮书v3,地址是:https://storj.io/storjv3.pdf. 这次白皮书一共有90页,看完还真要费不少时间.如果你没有时间看,可以看一下我这篇快速技术解读. 上次St ...

  4. springboot学习目录

    1.spring boot 简单示例 一个简单的springboot 例子  https://www.cnblogs.com/shoshana-kong/p/9641696.html 2. sprin ...

  5. 面向对象开发C++快速入门视频教程 C++基础加实战视频教程

    课程目录: ├<C++面向对象高级开发(上)> │ ├1.C++编程简介.mp4 │ ├2.头文件与类的声明.mp4 │ ├3.构造函数.mp4 │ ├4.参数传递与返回值.mp4 │ ├ ...

  6. spring boot 日志介绍 以及 logback配置示例

    https://www.cnblogs.com/flying607/p/7827460.html 以下是springboot的一个局部依赖关系: 可以看到,java util logging(jul) ...

  7. OS模块的介绍

    os,语义为操作系统,模块提供了访问多个操作系统服务的功能,可以处理文件和目录这些我们日常手动需要做的操作.os和它的子模块os.path还包括一些用于检查.构造.删除目录和文件的函数,以及一些处理路 ...

  8. [maven] "Dynamic Web Module 3.0 requires Java 1.6 or newer." OR "JAX-RS (REST Web Services) 2.0 requires Java 1.6 or newer."

    在网上下载的开源工程,用maven构建的时候报错: Dynamic Web Module 3.0 requires Java 1.6 or newer. JAX-RS (REST Web Servic ...

  9. python中发送post请求时,报错“Unrecognized token 'xxxx': was expecting ('true', 'false' or 'null')”

    解决办法: 如请求参数为 data={“user”=“aaa”,“pwd”=“123456”,sign=“00000000000000”} 需要将参数data先做处理,调用函数datas=datajs ...

  10. cacti监控服务器

    1.Cacti基本概念详解 Cacti是用php语言实现的一个软件,它的主要功能是用snmp服务获取数据,然后用rrdtool储存和更新数据,当用户需要查看数据的时候用rrdtool生成图表呈现给用户 ...