bzoj4977 跳伞求生——贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4977
今天讲的贪心题,真神奇啊;
首先,要得到尽量多选队友的解;
把队友按 a[i] 从小到大排序,敌人按 b[i] 从小到大排序,然后对于每个队友,选择能攻击的、收益最多的敌人;
如果没有能攻击的敌人,就把之前最小的一个队友踢掉代替,能使答案更优;
但尽量多选队友不一定是最终的最优答案,因为有些价值很小(为负)的敌人不如不选;
所以需要调整,很妙的做法,就是直接踢掉最小的队友和收益最小的敌人,那么剩下的队友和敌人也一定可以配对,过程中取最优解;
于是去写了,然而秒WA,自己码力好弱啊,打击...
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<queue>
- using namespace std;
- typedef long long ll;
- int const maxn=1e5+,inf=1e9;
- int n,m,a[maxn];
- ll ans;
- bool vis[maxn],out[maxn];
- struct N{
- int b,c,bh;
- bool operator < (const N &y) const
- {
- return c-b<y.c-y.b;//priority_queue大根堆
- }
- }p[maxn],t[maxn];
- priority_queue<N>qen;
- priority_queue<int>q2;
- priority_queue<int>q;
- bool cmp(N x,N y){return x.b<y.b;}
- int main()
- {
- scanf("%d%d",&n,&m);
- for(int i=;i<=n;i++)scanf("%d",&a[i]);
- for(int i=;i<=m;i++)scanf("%d%d",&p[i].b,&p[i].c);
- sort(a+,a+n+); sort(p+,p+m+,cmp);
- for(int i=;i<=m;i++)p[i].bh=i;
- int j=,pr=;
- for(int i=;i<=n;i++)
- {
- while(p[j].b<=a[i]) {qen.push(p[j]); j++;}
- if(!qen.size()) {t[i]=t[pr]; out[pr]=; pr++;}
- else {t[i]=qen.top(); vis[t[i].bh]=; qen.pop();}
- }
- for(int i=;i<=n;i++)
- if(!out[i])ans+=a[i]-t[i].b+t[i].c;
- for(int i=;i<=m;i++) if(vis[i])q2.push(p[i].b-p[i].c);
- for(int i=;i<=n;i++) if(!out[i])q.push(-a[i]);//
- ll tmp=ans;
- while(q.size()&&q2.size())
- {
- int x=-q.top(); q.pop();
- int y=-q2.top(); q2.pop();
- tmp-=x; tmp-=y;
- ans=max(ans,tmp);
- }
- printf("%lld\n",ans);
- return ;
- }
囧
看看人家写的简洁优美AC代码:https://blog.csdn.net/dream_lolita/article/details/79302382
然后就模仿着写了;练习码力...
代码如下:
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<queue>
- using namespace std;
- typedef long long ll;
- int const maxn=1e5+;
- int n,m,w[maxn],hd,tl;
- ll ans,sum;
- struct N{
- int x,y; bool tp;
- bool operator < (const N &b) const
- {
- if(x==b.x)return tp<b.tp;
- else return x<b.x;
- }
- N(int x=,int y=,bool t=):x(x),y(y),tp(t) {}
- }t[maxn<<];
- priority_queue<int>q;
- priority_queue<int,vector<int>,greater<int> >p;
- int main()
- {
- scanf("%d%d",&n,&m);
- for(int i=,x;i<=n;i++)scanf("%d",&t[i].x);
- for(int i=,x,y;i<=m;i++)
- {
- n++;
- scanf("%d%d",&x,&y);
- t[n]=N(x,y-x,);
- }
- sort(t+,t+n+);
- hd=;
- for(int i=;i<=n;i++)
- {
- if(t[i].tp)q.push(t[i].y);
- else
- {
- if(!q.size())
- {
- if(hd<=tl)
- {
- int tmp=w[hd]; hd++;
- sum+=t[i].x-tmp;
- w[++tl]=t[i].x;
- }
- }
- else
- {
- int tmp=q.top(); q.pop();
- sum+=t[i].x+tmp;
- p.push(tmp);//tmp被使用过
- w[++tl]=t[i].x;
- }
- }
- }
- ans=sum;
- for(int i=hd;i<=tl;i++)
- {
- sum-=w[i]+p.top(); p.pop();
- ans=max(ans,sum);
- }
- printf("%lld\n",ans);
- return ;
- }
bzoj4977 跳伞求生——贪心的更多相关文章
- [bzoj4977]跳伞求生<贪心>
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4977 这是八月月赛的一道题,月赛的时候和同学讨论了一下,最后由一位叫二哥的大佬率先AC,用 ...
- BZOJ4977[Lydsy1708月赛]跳伞求生——贪心+堆+模拟费用流
题目链接: 跳伞求生 可以将题目转化成数轴上有$n$个人和$m$个房子,坐标分别为$a_{i}$和$b_{i}$,每个人可以进一个他左边的房子,每个房子只能进一个人.每个房子有一个收益$c_{i}$, ...
- BZOJ4977 跳伞求生(贪心)
如果现在选定了一些要求消灭的敌人而不考虑积分,显然应该让每个敌人被刚好能消灭他的人消灭.再考虑最大化积分,显然我们应该优先消灭ci-bi大的敌人,所选用的a也应尽量大.于是按ci-bi从大到小排序,用 ...
- BZOJ4977: [[Lydsy1708月赛]跳伞求生(不错的贪心)
4977: [[Lydsy1708月赛]跳伞求生 Time Limit: 5 Sec Memory Limit: 256 MBSubmit: 446 Solved: 142[Submit][Sta ...
- 【刷题】BZOJ 4977 [Lydsy1708月赛]跳伞求生
Description 小Q最近沉迷于<跳伞求生>游戏.他组建了一支由n名玩家(包括他自己)组成的战队,编号依次为1到n.这个游 戏中,每局游戏开始时,所有玩家都会从飞机上跳伞,选择一个目 ...
- BZOJ4977 八月月赛 Problem G 跳伞求生 set 贪心
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4977 - 八月月赛 Problem G 题意 小明组建了一支由n名玩家组成的战队,编号依次为1到n ...
- 2018.09.24 bzoj4977: [[Lydsy1708月赛]跳伞求生(贪心+线段树)
传送门 线段树好题. 这题一看我就想贪心. 先把a,b数组排序. 然后我们选择a数组中最大的b个数(不足b个就选a个数),分别贪心出在b数组中可以获得的最大贡献. 这时可以用线段树优化. 然后交上去只 ...
- BZOJ4977: [[Lydsy1708月赛]跳伞求生
传送门 直接贪心 考虑到 \(n\) 个人的贡献都是 \(a_i\),另外 \(m\) 个人的贡献都是 \(c_i-b_i\) 首先 \(a_i>b_j\) 的限制不好做,所以将 \(a,b\) ...
- bzoj 1034 [ZJOI2008]泡泡堂BNB——贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1034 原来觉得和 bzoj4977跳伞求生 有点像(虽然还没做). 所以对于a[ ]从小到大 ...
随机推荐
- Java_Web三大框架之Hibernate操作数据库(三)
使用Hibernate操作数据库需要七个步骤: (1)读取并解析配置文件 Configuration conf = newConfiguration().configure(); (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 ...
- .mm c++ oc 混编
When you create a static library you don't link in the dependent libraries. As a result, when you re ...
- illumina测序原理
一些常用基本概念的介绍: flowcell流动池 是指Illumina测序时,测序反应发生的位置,1个flowcell含有8条lane lane通道 每一个flowcell上都有8条泳道,用于测序反应 ...
- 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 ...
- Xcode 插件因为UUID原因不能使用解决办法
Xcode 经常因为一些原因不能使用,需要重新在 ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins目录下对每一个插件包下的p ...
- 使用IDM下载软件下载百度云网盘里的资源,以Chrome浏览器为例
1.下载安装最新版的Chrome浏览器,我是在腾讯软件下载中心下载的,使用普通下载,下载地址:https://dl.softmgr.qq.com/original/Browser/72.0.3626. ...
- 面向对象:__getitem__、__setitem__、__delitem__
item系列 class Person(object): def __init__(self, name): self.name = name def __getitem__(self, item): ...
- Golang之路
目录 Golang之路 Golang之路 Golang(一) - 开篇必须吹牛逼 Golang(二) - 第一个go程序和基本语法 Golang(三) - 函数 Golang(四) - 流程控制 Go ...
- hdu2009 求数列的和【C++】
求数列的和 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...