[Educational Codeforces Round 55 (Rated for Div. 2)][C. Multi-Subject Competition][时间复杂度]

https://codeforc.es/contest/1082/problem/C

题目大意:有m个类型,n个人,每个人有一个所属类型k和一个能力v,要求所选的类型的人个数相等并且使v总和最大(n,m<=1e5)

题解:用vector存下每种类型的各个v并且每种类型根据v按从大到小排序,然后处理出每种类型的前缀和,然后扫每种类型的所有前缀和,如果该类型在i处的前缀和大于0,则相应的ans[i]加上这个类型在i处的前缀和,最后求出max(ans[i])(1<=i<=n)即可.

注意:这题的外层循环应该是遍历m个类型,然后扫每种类型的前缀和来更新ans[i],这样子因为一共n个人,所以最多n次扫描过程复杂度为O(n),这种方式由于是根据每个类型的size来遍历的会减少很多无用的遍历,而如果外层循环是长度n,然后根据长度来更新ans[i],这样子会无视类型长度,进行很多次无用的遍历,复杂度是O(m*n),显然会T掉

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #include<stack>
 6 #include<vector>
 7 #include<algorithm>
 8 using namespace std;
 9 typedef long long ll;
10 const int mod = 1e9+7;
11 vector<int>vc[100005];
12 vector<ll>sum[100005];
13 ll ans[100005];
14 int main()
15 {
16   // freopen("in.txt","r",stdin);
17     int n,m;
18     cin>>n>>m;
19     int aa,bb;
20     int maxx=1;
21     for(int i=0;i<n;i++){
22         scanf("%d%d",&aa,&bb);
23         vc[aa].push_back(bb);
24         maxx=max(maxx,aa);
25     }
26     int minn=0;//
27     for(int i=1;i<=maxx;i++){
28         minn=max(minn,(int)vc[i].size());
29         sort( vc[i].begin(),vc[i].end(),greater<int>() );
30         for(int j=0;j<vc[i].size();j++){
31
32             if(j)sum[i].push_back(sum[i][j-1]+vc[i][j]);
33             else {sum[i].push_back(vc[i][j]);}
34         }
35     }
36     /*for(int i=1;i<=minn;i++){
37         for(int j=1;j<=maxx;j++){
38             if(vc[j].size()>=i&&sum[j][i-1]>=0){
39                 ans[i]+=sum[j][i-1];
40             }
41         }
42     }*///错误的扫描方式,会T
43     for(int i=1;i<=maxx;i++){
44         for(int j=0;j<vc[i].size();j++){
45             if(sum[i][j]>=0){
46                 ans[j+1]+=sum[i][j];
47             }
48         }
49     }//正确的扫描方式
50     ll aans=0;
51     for(int i=1;i<=minn;i++){
52         aans=max(ans[i],aans);
53     }
54     cout << aans<<endl;
55     return 0;
56 }

原文地址:https://www.cnblogs.com/MekakuCityActor/p/10046727.html

时间: 2024-11-08 03:23:58

[Educational Codeforces Round 55 (Rated for Div. 2)][C. Multi-Subject Competition][时间复杂度]的相关文章

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm

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 

Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))

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.

Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vasya is reading a e-book. The file of the book consists of nn pages, numbered from 11 to nn. The screen is currently disp

Codeforces 1082 D. Maximum Diameter Graph-树的直径-最长链-构造题 (Educational Codeforces Round 55 (Rated for Div. 2))

D. Maximum Diameter Graph time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Graph constructive problems are back! This time the graph you are asked to build should match the following proper

codeforces Educational Codeforces Round 55 (Rated for Div. 2) C题 C. Multi-Subject Competition

这道题比赛时候没做出来,下来一看才发现是排序傻逼题. 把每个偏好的人做成一个vector,从大到小排序,做一个前缀和.然后将每种人数做一个桶,在桶里装每种科目选择人数为i的时候分数总和. 遍历每一维vector,把各个位置上面的vector加到sum数组中,最后sum数组里面挑出最大值. #include<bits/stdc++.h> using namespace std; typedef long long ll; vector<int> vec[100010];//vecto

Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies

传送门 https://www.cnblogs.com/violet-acmer/p/10035971.html 题意: Vova有n个奖杯,这n个奖杯全部是金奖或银奖,Vova将所有奖杯排成一排,你最多可以交换其中两个奖杯,求最大的连续的金奖杯个数. 题解: 思路: 求出连续的金奖杯位置,找出每两个相邻的连续的金奖杯所能够形成的最大的连续的金奖杯的个数,输出最大值. 相关变量解释: 1 int n; 2 char trophy[maxn]; 3 struct Node 4 { 5 int l,

Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition (实现,贪心,排序)

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'

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ