CF 1082C. Multi-Subject Competition

15 篇文章 0 订阅
10 篇文章 0 订阅

C. Multi-Subject Competition
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
A multi-subject competition is coming! The competition has m different subjects participants can choose from. That’s why Alex (the coach) should form a competition delegation among his students.

He has n candidates. For the i-th person he knows subject si the candidate specializes in and ri — 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 n and m (1≤n≤105, 1≤m≤105) — the number of candidates and the number of subjects.

The next n lines contains two integers per line: si and ri (1≤si≤m, −104≤ri≤104) — the subject of specialization and the skill level of the i-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
inputCopy
6 3
2 6
3 6
2 5
3 5
1 9
3 1
outputCopy
22
inputCopy
5 3
2 6
3 6
2 5
3 5
1 11
outputCopy
23
inputCopy
5 2
1 -1
1 -5
2 -1
2 -1
1 -10
outputCopy
0
Note
In the first example it’s optimal to choose candidates 1, 2, 3, 4, so two of them specialize in the 2-nd subject and other two in the 3-rd. The total sum is 6+6+5+5=22.

In the second example it’s optimal to choose candidates 1, 2 and 5. One person in each subject and the total sum is 6+6+11=23.

In the third example it’s impossible to obtain a non-negative sum.

题意:给你m个不同的学科,和n个学生,每个学生都掌握了一个学科,且具有一个技能分val
现在要你安排他们去比赛,要求每个学科去的人数必须相同,即如果有三个学科,第一个学科只去了一个人,那么剩下两个学科也只能去一个人,现在问你技能分的总和最大是多少,不是所有的学科都必须去人
思路:看懂题意就很简单了,先按学科技能分大到小排序,然后只要维护一个前缀和数组,分别代表去1个人时候的和,去2个人的时候的最大和,…,最后再用一个数组记录n个人的时候的最大和。

AC代码:

#include<bits/stdc++.h>
#define LL long long
#define Max 100005
const LL mod=1e9+7;
const LL LL_MAX=9223372036854775807;
using namespace std;
bool cmp(int a,int b){
    return a>b;
}
vector<int>a[Max],sum[Max];
int ans[Max];
set<int>s;
int n,m;
int main()
{
    scanf("%d%d",&n,&m);
    int x,y;
    for(int i=0;i<n;i++){
         scanf("%d%d",&x,&y);
         a[x].push_back(y);
         s.insert(x);//题目只说了m个学科,没说编号从什么开始,所以用set记录一下
    }
    for(set<int>::iterator it=s.begin();it!=s.end();it++){//每个学科排序
        sort(a[*it].begin(),a[*it].end(),cmp);
    }
    for(set<int>::iterator it=s.begin();it!=s.end();it++){//计算前缀和
            sum[*it].push_back(a[*it][0]);
                ans[0]+=sum[*it][0]>0?sum[*it][0]:0;
        for(int j=1;j<a[*it].size();j++){
            sum[*it].push_back(sum[*it][j-1]+a[*it][j]);
            ans[j]+=sum[*it][j]>0?sum[*it][j]:0;//统计最大分数,如果是负数肯定就不用加进去了
        }
    }
    int res=*max_element(ans,ans+Max);//找到最大值输出
    printf("%d\n",res<0?0:res);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值