题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4977

今天讲的贪心题,真神奇啊;

首先,要得到尽量多选队友的解;

把队友按 a[i] 从小到大排序,敌人按 b[i] 从小到大排序,然后对于每个队友,选择能攻击的、收益最多的敌人;

如果没有能攻击的敌人,就把之前最小的一个队友踢掉代替,能使答案更优;

但尽量多选队友不一定是最终的最优答案,因为有些价值很小(为负)的敌人不如不选;

所以需要调整,很妙的做法,就是直接踢掉最小的队友和收益最小的敌人,那么剩下的队友和敌人也一定可以配对,过程中取最优解;

于是去写了,然而秒WA,自己码力好弱啊,打击...

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<queue>
  6. using namespace std;
  7. typedef long long ll;
  8. int const maxn=1e5+,inf=1e9;
  9. int n,m,a[maxn];
  10. ll ans;
  11. bool vis[maxn],out[maxn];
  12. struct N{
  13. int b,c,bh;
  14. bool operator < (const N &y) const
  15. {
  16. return c-b<y.c-y.b;//priority_queue大根堆
  17. }
  18. }p[maxn],t[maxn];
  19. priority_queue<N>qen;
  20. priority_queue<int>q2;
  21. priority_queue<int>q;
  22. bool cmp(N x,N y){return x.b<y.b;}
  23. int main()
  24. {
  25. scanf("%d%d",&n,&m);
  26. for(int i=;i<=n;i++)scanf("%d",&a[i]);
  27. for(int i=;i<=m;i++)scanf("%d%d",&p[i].b,&p[i].c);
  28. sort(a+,a+n+); sort(p+,p+m+,cmp);
  29. for(int i=;i<=m;i++)p[i].bh=i;
  30. int j=,pr=;
  31. for(int i=;i<=n;i++)
  32. {
  33. while(p[j].b<=a[i]) {qen.push(p[j]); j++;}
  34. if(!qen.size()) {t[i]=t[pr]; out[pr]=; pr++;}
  35. else {t[i]=qen.top(); vis[t[i].bh]=; qen.pop();}
  36. }
  37. for(int i=;i<=n;i++)
  38. if(!out[i])ans+=a[i]-t[i].b+t[i].c;
  39. for(int i=;i<=m;i++) if(vis[i])q2.push(p[i].b-p[i].c);
  40. for(int i=;i<=n;i++) if(!out[i])q.push(-a[i]);//
  41. ll tmp=ans;
  42. while(q.size()&&q2.size())
  43. {
  44. int x=-q.top(); q.pop();
  45. int y=-q2.top(); q2.pop();
  46. tmp-=x; tmp-=y;
  47. ans=max(ans,tmp);
  48. }
  49. printf("%lld\n",ans);
  50. return ;
  51. }

看看人家写的简洁优美AC代码:https://blog.csdn.net/dream_lolita/article/details/79302382

然后就模仿着写了;练习码力...

代码如下:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<queue>
  6. using namespace std;
  7. typedef long long ll;
  8. int const maxn=1e5+;
  9. int n,m,w[maxn],hd,tl;
  10. ll ans,sum;
  11. struct N{
  12. int x,y; bool tp;
  13. bool operator < (const N &b) const
  14. {
  15. if(x==b.x)return tp<b.tp;
  16. else return x<b.x;
  17. }
  18. N(int x=,int y=,bool t=):x(x),y(y),tp(t) {}
  19. }t[maxn<<];
  20. priority_queue<int>q;
  21. priority_queue<int,vector<int>,greater<int> >p;
  22. int main()
  23. {
  24. scanf("%d%d",&n,&m);
  25. for(int i=,x;i<=n;i++)scanf("%d",&t[i].x);
  26. for(int i=,x,y;i<=m;i++)
  27. {
  28. n++;
  29. scanf("%d%d",&x,&y);
  30. t[n]=N(x,y-x,);
  31. }
  32. sort(t+,t+n+);
  33. hd=;
  34. for(int i=;i<=n;i++)
  35. {
  36. if(t[i].tp)q.push(t[i].y);
  37. else
  38. {
  39. if(!q.size())
  40. {
  41. if(hd<=tl)
  42. {
  43. int tmp=w[hd]; hd++;
  44. sum+=t[i].x-tmp;
  45. w[++tl]=t[i].x;
  46. }
  47. }
  48. else
  49. {
  50. int tmp=q.top(); q.pop();
  51. sum+=t[i].x+tmp;
  52. p.push(tmp);//tmp被使用过
  53. w[++tl]=t[i].x;
  54. }
  55. }
  56. }
  57. ans=sum;
  58. for(int i=hd;i<=tl;i++)
  59. {
  60. sum-=w[i]+p.top(); p.pop();
  61. ans=max(ans,sum);
  62. }
  63. printf("%lld\n",ans);
  64. return ;
  65. }

bzoj4977 跳伞求生——贪心的更多相关文章

  1. [bzoj4977]跳伞求生<贪心>

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4977 这是八月月赛的一道题,月赛的时候和同学讨论了一下,最后由一位叫二哥的大佬率先AC,用 ...

  2. BZOJ4977[Lydsy1708月赛]跳伞求生——贪心+堆+模拟费用流

    题目链接: 跳伞求生 可以将题目转化成数轴上有$n$个人和$m$个房子,坐标分别为$a_{i}$和$b_{i}$,每个人可以进一个他左边的房子,每个房子只能进一个人.每个房子有一个收益$c_{i}$, ...

  3. BZOJ4977 跳伞求生(贪心)

    如果现在选定了一些要求消灭的敌人而不考虑积分,显然应该让每个敌人被刚好能消灭他的人消灭.再考虑最大化积分,显然我们应该优先消灭ci-bi大的敌人,所选用的a也应尽量大.于是按ci-bi从大到小排序,用 ...

  4. BZOJ4977: [[Lydsy1708月赛]跳伞求生(不错的贪心)

    4977: [[Lydsy1708月赛]跳伞求生 Time Limit: 5 Sec  Memory Limit: 256 MBSubmit: 446  Solved: 142[Submit][Sta ...

  5. 【刷题】BZOJ 4977 [Lydsy1708月赛]跳伞求生

    Description 小Q最近沉迷于<跳伞求生>游戏.他组建了一支由n名玩家(包括他自己)组成的战队,编号依次为1到n.这个游 戏中,每局游戏开始时,所有玩家都会从飞机上跳伞,选择一个目 ...

  6. BZOJ4977 八月月赛 Problem G 跳伞求生 set 贪心

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4977 - 八月月赛 Problem G 题意 小明组建了一支由n名玩家组成的战队,编号依次为1到n ...

  7. 2018.09.24 bzoj4977: [[Lydsy1708月赛]跳伞求生(贪心+线段树)

    传送门 线段树好题. 这题一看我就想贪心. 先把a,b数组排序. 然后我们选择a数组中最大的b个数(不足b个就选a个数),分别贪心出在b数组中可以获得的最大贡献. 这时可以用线段树优化. 然后交上去只 ...

  8. BZOJ4977: [[Lydsy1708月赛]跳伞求生

    传送门 直接贪心 考虑到 \(n\) 个人的贡献都是 \(a_i\),另外 \(m\) 个人的贡献都是 \(c_i-b_i\) 首先 \(a_i>b_j\) 的限制不好做,所以将 \(a,b\) ...

  9. bzoj 1034 [ZJOI2008]泡泡堂BNB——贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1034 原来觉得和 bzoj4977跳伞求生 有点像(虽然还没做). 所以对于a[ ]从小到大 ...

随机推荐

  1. Java_Web三大框架之Hibernate操作数据库(三)

    使用Hibernate操作数据库需要七个步骤: (1)读取并解析配置文件 Configuration conf = newConfiguration().configure(); (2)读取并解析映射 ...

  2. 【sqli-labs】 less55 GET -Challenge -Union -14 queries allowed -Variation1 (GET型 挑战 联合查询 只允许14次查询 变化2)

    http://192.168.136.128/sqli-labs-master/Less-55/?id=1' 试了几次,整型带括号正常了 http://192.168.136.128/sqli-lab ...

  3. .mm c++ oc 混编

    When you create a static library you don't link in the dependent libraries. As a result, when you re ...

  4. illumina测序原理

    一些常用基本概念的介绍: flowcell流动池 是指Illumina测序时,测序反应发生的位置,1个flowcell含有8条lane lane通道 每一个flowcell上都有8条泳道,用于测序反应 ...

  5. CAD插入图块前修改图块文字

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  6. Xcode 插件因为UUID原因不能使用解决办法

    Xcode 经常因为一些原因不能使用,需要重新在  ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins目录下对每一个插件包下的p ...

  7. 使用IDM下载软件下载百度云网盘里的资源,以Chrome浏览器为例

    1.下载安装最新版的Chrome浏览器,我是在腾讯软件下载中心下载的,使用普通下载,下载地址:https://dl.softmgr.qq.com/original/Browser/72.0.3626. ...

  8. 面向对象:__getitem__、__setitem__、__delitem__

    item系列 class Person(object): def __init__(self, name): self.name = name def __getitem__(self, item): ...

  9. Golang之路

    目录 Golang之路 Golang之路 Golang(一) - 开篇必须吹牛逼 Golang(二) - 第一个go程序和基本语法 Golang(三) - 函数 Golang(四) - 流程控制 Go ...

  10. hdu2009 求数列的和【C++】

    求数列的和 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...