C 维护前缀和

题意

每一个id给一个权值序列,从每个id选出数量相同的权值,对他们进行求和,使得他们的和最大

题解

注意负数对结果没有贡献,直接跳过。

当时写的比较挫,连排序都写错了!cf的编译器比较的严谨,虽然本地的编译器过了样例,但实际上是有问题的。结果就是交上去疯狂RE。

题解就直接看代码,比较的直观

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 2e5+10;
  4. int n, m, k;
  5. int a[maxn];
  6. struct Node{
  7. int id, val;
  8. bool operator < (const Node &b) const{
  9. if(id!=b.id) return id<b.id;
  10. else return val>b.val;
  11. }
  12. }node[maxn];
  13. int main(){
  14. ios::sync_with_stdio(false);
  15. cin>>n>>m;
  16. int u, v;
  17. for(int i=1; i<=n; i++){
  18. cin>>node[i].id>>node[i].val;
  19. }
  20. sort(node+1, node+1+n);
  21. int pre=-1;
  22. int c;
  23. int sum = 0;
  24. int ans = -1e9-10;
  25. for(int i=1; i<=n; i++){
  26. if(node[i].id!=pre){
  27. pre = node[i].id, c=0, sum = 0;
  28. }
  29. c++;
  30. sum +=node[i].val;
  31. if(sum>0) {
  32. a[c]+=sum;
  33. }
  34. ans = max(ans, a[c]);
  35. }
  36. if(ans == -1e9-10) cout<<0<<endl;
  37. else cout<<ans<<endl;
  38. return 0;
  39. }

D 构造题

题意

给n个点,每个点限制一个度数\(a_i\),你要构造一个无向图,使得每个点的度数不超过限制,并且最长链最长。

题解

度数大于等于2的点先构成一个最长链,然后度数为1的先加在两头(否则会使答案错误),之后的点在中间插就行了。

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef unsigned long long ull;
  5. typedef pair<int, int> pii;
  6. #define pb(x) push_back(x)
  7. #define cls(x, val) memset(x, val, sizeof(x))
  8. #define fi first
  9. #define se second
  10. #define mp(x, y) make_pair(x, y)
  11. #define inc(i, l, r) for(int i=l; i<=r; i++)
  12. const int inf = 0x3f3f3f3f;
  13. const int maxn = 500+10;
  14. int readint()
  15. {
  16. int x=0,f=1;char ch=getchar();
  17. while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
  18. while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
  19. return x*f;
  20. }
  21. int n, m;
  22. int a[maxn];
  23. vector<int> lone;
  24. vector<int> two;
  25. int main()
  26. {
  27. ios::sync_with_stdio(false);
  28. cin>>n;
  29. int tot = 0;
  30. int one = 0;
  31. int sum = 0;
  32. int u, v;
  33. for(int i=1; i<=n; i++) {
  34. cin>>a[i];
  35. if(a[i]>1) tot += 1, two.push_back(i);
  36. else one+=1, lone.push_back(i);
  37. sum += a[i];
  38. }
  39. if(sum<2*n-2){
  40. cout<<"NO"<<endl;
  41. return 0;
  42. }
  43. {
  44. if(one>=2)
  45. cout<<"YES "<<tot+1<<endl<<n-1<<endl;
  46. else cout<<"YES "<<tot-1+one<<endl<<n-1<<endl;
  47. for(int i=0; i<two.size(); i++){
  48. if(i+1<two.size()){
  49. u = two[i], v = two[i+1];
  50. a[u]--, a[v]--;
  51. cout<<u<<" "<<v<<endl;
  52. }
  53. }
  54. int idx = 0;
  55. if(lone.size()>=2){
  56. u=lone[0], v=lone[1];
  57. cout<<u<<" "<<two[0]<<endl;
  58. a[two[0]]--;
  59. cout<<v<<" "<<two[two.size()-1]<<endl;
  60. a[two[two.size()-1]]--;
  61. idx=2;
  62. }
  63. for(int i=0; i<two.size(); i++){
  64. int &deg = a[two[i]];
  65. if(idx >= int(lone.size())) break;
  66. for(int j=idx; j<min(int(lone.size()), deg+idx); j++){
  67. cout<<lone[j]<<" "<<two[i]<<endl;
  68. }
  69. idx+=deg;
  70. deg = 0;
  71. }
  72. }
  73. return 0;
  74. }

E Increasing Frequency--思维+贪心

题意

给一个长度为n的序列,并且确定一个数c。\(你可以任选一个区间[l, r], 对该区间+k,k可以为负数\),使得最后的n个数中,等于c的数字的个数最多。问最多有多少个这样的数?

题解

Let $ cnt(l,r,x) $ be a number of occurrences of number x in subsegment$ [l,r] $.

The given task is equivalent to choosing \([l,r]\) and value d, such that $ans=cnt(1,l−1,c)+cnt(l,r,d)+cnt(r+1,n,c) $is maximum possible. But with some transformations \(ans=cnt(1,n,c)+(cnt(l,r,d)−cnt(l,r,c))\), so we need to maximize \(cnt(l,r,d)−cnt(l,r,c)\).

代码的精髓就是求解maximize\(cnt(l, r, d)-cnt(l, r, c)\)

代码

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<iostream>
  5. #include<queue>
  6. #include<cmath>
  7. #include<map>
  8. #include<stack>
  9. #include<set>
  10. #include<bitset>
  11. using namespace std;
  12. typedef long long ll;
  13. typedef unsigned long long ull;
  14. typedef pair<int, int> pii;
  15. #define pb(x) push_back(x)
  16. #define cls(x, val) memset(x, val, sizeof(x))
  17. #define fi first
  18. #define se second
  19. #define mp(x, y) make_pair(x, y)
  20. #define inc(i, l, r) for(int i=l; i<=r; i++)
  21. const int inf = 0x3f3f3f3f;
  22. const int maxn = 5e5+10;
  23. int readint()
  24. {
  25. int x=0,f=1;char ch=getchar();
  26. while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
  27. while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
  28. return x*f;
  29. }
  30. int cnt[maxn];
  31. int n, c;
  32. int mi[maxn];
  33. int main()
  34. {
  35. ios::sync_with_stdio(false);
  36. cin>>n>>c;
  37. int temp;
  38. int ans = 0;
  39. for(int i=1; i<=n; i++){
  40. cin>>temp;
  41. mi[temp] = min(mi[temp], cnt[temp]-cnt[c]);
  42. cnt[temp]++;
  43. ans = max(ans, cnt[temp]-cnt[c]-mi[temp]);
  44. }
  45. cout<<ans+cnt[c]<<endl;
  46. return 0;
  47. }

F trie+dp

G 最大密度子图

题意

给定一个无向图,给定边权和点权,选择若干的点,使得他们的边权和-点权和最大

题解

codeforces contest1082的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. EffectiveC++笔记 目录

    Charpter 1. 让自己习惯C++   条款01: 视C++为一个语言联邦 条款02: 尽量以const,enum,inline替换#define 条款03: 尽可能使用const 条款04: ...

  2. EF提交插入数据catch捕获具体异常方法

    try { db.SaveChanges(); } catch (DbEntityValidationException ex) { StringBuilder errors = new String ...

  3. python复习2

    在操作字符串时,我们经常遇到str和bytes的互相转换.为了避免乱码问题,应当始终坚持使用UTF-8编码对str和bytes进行转换.

  4. 【尚学堂·Hadoop学习】MapReduce案例2--好友推荐

    案例描述 根据好友列表,推荐好友的好友 数据集 tom hello hadoop cat world hadoop hello hive cat tom hive mr hive hello hive ...

  5. 2018-2019-2 20165234 《网络对抗技术》 Exp2 后门原理与实践

    实验二 后门原理与实践 实验内容 (1)使用netcat获取主机操作Shell,cron启动 (2)使用socat获取主机操作Shell, 任务计划启动 (3)使用MSF meterpreter(或其 ...

  6. git工具——对比文件的不同

    对比工作区和某个版本中文件的不同: (1)继续编辑文件code.txt,在其中添加一行内容: (2)现在要对比工作区中code.txt和head版本中code.txt的不同,使用如下命令: 前面没有出 ...

  7. 论文笔记:Mask R-CNN

    之前在一次组会上,师弟诉苦说他用 UNet 处理一个病灶分割的任务,但效果极差,我看了他的数据后发现,那些病灶区域比起整张图而言非常的小,而 UNet 采用的损失函数通常是逐像素的分类损失,如此一来, ...

  8. 【译】索引进阶(七):SQL SERVER中的过滤索引

    原文链接:传送门. To be continued...

  9. CocoaLumberjack——带颜色的Log

    CocoaLumberjack可以带颜色Log,具体的好处嘛,谁用谁知道,:] 具体步骤如下: 1. 安装XcodeColors插件 下载地址:https://github.com/robbiehan ...

  10. git rejected - non-fast-forward

    di第一次提交时可能提示此错误,应该是.gitignore冲突,建议码云创建时不要初始化,如果已经出现了.可以从git  repostitory里合并. 参考:https://blog.csdn.ne ...