Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】...

传送门:http://codeforces.com/contest/1082/problem/C

C. Multi-Subject Competition
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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).

Input

The first line contains two integers nn and mm (1n1051≤n≤105, 1m1051≤m≤105) — the number of candidates and the number of subjects.

The next nn lines contains two integers per line: sisi and riri (1sim1≤si≤m, 104ri104−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 00 if every valid non-empty delegation has negative sum.

Examples
input
Copy
6 3
2 6
3 6
2 5
3 5
1 9
3 1
output
Copy
22
input
Copy
5 3
2 6
3 6
2 5
3 5
1 11
output
Copy
23
input
Copy
5 2
1 -1
1 -5
2 -1
2 -1
1 -10
output
Copy
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.

 

 

题意概括:

有 M 种物品,有 N 条信息。

每条信息 no val 说明了 第 no 种物品能带来的收益(可累加)

要求选取的物品里,每种物品的数量要一样,求最大收益 。

 

解题思路:

N M 的范围开不了静态二维数组,需要使用 stl 里的 vector 开一个二维数组,用于记录没每种物品的收益。

首先对每种物品的收益按照 降序排序,这样就能贪心取到 k 个该种物品了。

但这样还不够,我们求一个收益的前缀和,这样第 k 个值就是 取得 k 个该种物品的最大收益了。

其次对每种物品的数量也进行降序排序,后面枚举物品数量时就可以剪枝了。

 

AC code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <vector>
 7 #include <map>
 8 #define INF 0x3f3f3f3f
 9 #define LL long long
10 using namespace std;
11 const int MAXN = 1e5+10;
12 vector<vector<int> >vv(MAXN);       //动态二维数组
13 int N, M;
14 
15 bool cmp(vector<int>a, vector<int>b)   //自定义对一维数组的排序规则
16 {
17     return a.size() > b.size();
18 }
19 
20 int solve(int len)
21 {
22     int res = 0;
23 //    int tmp = 0;
24     for(int i = 0; i < M; i++){
25         if(vv[i].size() < len) return res;
26 //        tmp = 0;
27 //        for(int k = 0; k < len; k++)      //未预处理前缀和导致超时
28 //            tmp += vv[i][k];
29 //        if(tmp > 0) res+=tmp;
30         if(vv[i][len-1] > 0) res+=vv[i][len-1];
31     }
32     return res;
33 }
34 
35 int main()
36 {
37     int no, x, slen, ans;
38     while(~scanf("%d%d", &N, &M)){
39         slen = 0;
40         for(int i = 1; i <= N; i++){
41             scanf("%d%d", &no, &x);
42             no--;                       //注意因为数组下标从 0 开始
43             vv[no].push_back(x);
44             if(vv[no].size() > slen) slen = vv[no].size();
45         }
46         for(int i = 0; i < M; i++){
47             sort(vv[i].begin(), vv[i].end(), std::greater<int>());  //降序排序
48         }
49 
50         for(int i = 0; i < M; i++){             //预处理前缀和
51             if(vv[i].size() == 1) continue;
52             for(int k = 1; k < vv[i].size(); k++)
53                 vv[i][k] = vv[i][k-1] + vv[i][k];
54         }
55 
56         sort(vv.begin(), vv.end(), cmp);        //用于剪枝
57 
58 //        for(int i = 0; i < M; i++){
59 //            printf("%d:", i+1);
60 //            for(int k = 0; k < vv[i].size(); k++)
61 //                printf(" %d", vv[i][k]);
62 //            puts("");
63 //        }
64 
65         ans = 0;
66         for(int li = 1; li <= slen; li++){  //枚举物品数量
67             ans = max(ans, solve(li));
68         }
69         printf("%d\n", ans);                //初始化
70         for(int i = 0; i < M; i++)
71             vv[i].clear();
72 
73     }
74     return 0;
75 }

 

转载于:https://www.cnblogs.com/ymzjj/p/10066526.html

CSDN海神之光上传的代码均可运行,亲测可用,直接替换数据即可,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b或2023b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描博客文章底部QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作 功率谱估计: 故障诊断分析: 雷达通信:雷达LFM、MIMO、成像、定位、干扰、检测、信号分析、脉冲压缩 滤波估计:SOC估计 目标定位:WSN定位、滤波跟踪、目标定位 生物电信号:肌电信号EMG、脑电信号EEG、心电信号ECG 通信系统:DOA估计、编码译码、变分模态分解、管道泄漏、滤波器、数字信号处理+传输+分析+去噪(CEEMDAN)、数字信号调制、误码率、信号估计、DTMF、信号检测识别融合、LEACH协议、信号检测、水声通信 1. EMD(经验模态分解,Empirical Mode Decomposition) 2. TVF-EMD(时变滤波的经验模态分解,Time-Varying Filtered Empirical Mode Decomposition) 3. EEMD(集成经验模态分解,Ensemble Empirical Mode Decomposition) 4. VMD(变分模态分解,Variational Mode Decomposition) 5. CEEMDAN(完全自适应噪声集合经验模态分解,Complementary Ensemble Empirical Mode Decomposition with Adaptive Noise) 6. LMD(局部均值分解,Local Mean Decomposition) 7. RLMD(鲁棒局部均值分解, Robust Local Mean Decomposition) 8. ITD(固有时间尺度分解,Intrinsic Time Decomposition) 9. SVMD(逐次变分模态分解,Sequential Variational Mode Decomposition) 10. ICEEMDAN(改进的完全自适应噪声集合经验模态分解,Improved Complementary Ensemble Empirical Mode Decomposition with Adaptive Noise) 11. FMD(特征模式分解,Feature Mode Decomposition) 12. REMD(鲁棒经验模态分解,Robust Empirical Mode Decomposition) 13. SGMD(辛几何模态分解,Spectral-Grouping-based Mode Decomposition) 14. RLMD(鲁棒局部均值分解,Robust Intrinsic Time Decomposition) 15. ESMD(极点对称模态分解, extreme-point symmetric mode decomposition) 16. CEEMD(互补集合经验模态分解,Complementary Ensemble Empirical Mode Decomposition) 17. SSA(奇异谱分析,Singular Spectrum Analysis) 18. SWD(群分解,Swarm Decomposition) 19. RPSEMD(再生相移正弦辅助经验模态分解,Regenerated Phase-shifted Sinusoids assisted Empirical Mode Decomposition) 20. EWT(经验小波变换,Empirical Wavelet Transform) 21. DWT(离散小波变换,Discraete wavelet transform) 22. TDD(时域分解,Time Domain Decomposition) 23. MODWT(最大重叠离散小波变换,Maximal Overlap Discrete Wavelet Transform) 24. MEMD(多元经验模态分解,Multivariate Empirical Mode Decomposition) 25. MVMD(多元变分模态分解,Multivariate Variational Mode Decomposition
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值