贪心+线段树(优先队列)——51nod1191 消灭兔子
来源:互联网 发布:优酷视频for mac 编辑:程序博客网 时间:2024/05/22 10:02
题面:51nod1191
哈哈哈……脑回路清奇
首先我们可以很快想到贪心,我们把兔子的血量从大到小排序,箭按照伤害值从小到大排序,那么我们就可以先做血量多的兔子,这样就可以为血量少的兔子留出箭了
因为数据范围有5W,所以可以想到RMQ
其实只要一个堆就可以了,我清(zhi)奇(zhang)地想到了线段树。。。
#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>#include <iostream>#include <ctime>#include <map>#include <queue>#include <cstdlib>#include <string>#include <climits>#include <set>#include <vector>using namespace std;inline int read(){ int k=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();} return k*f;}int n,m,a[100010],t[400010],v[100010];struct ppap{int x,y;}b[100010];bool operator <(ppap a,ppap b){return a.x==b.x?a.y<b.y:a.x<b.x;}inline void build(int l,int r,int nod){ if(l==r){t[nod]=l;return;} int mid=l+r>>1; build(l,mid,nod*2);build(mid+1,r,nod*2+1); t[nod]=v[t[nod*2]]<v[t[nod*2+1]]?t[nod*2]:t[nod*2+1];}inline void xg(int l,int r,int x,int nod){ if(l==r)return; int mid=l+r>>1; if(x<=mid)xg(l,mid,x,nod*2);else xg(mid+1,r,x,nod*2+1); t[nod]=v[t[nod*2]]<v[t[nod*2+1]]?t[nod*2]:t[nod*2+1];}inline int smin(int l,int r,int x,int y,int nod){ if(l>=x&&r<=y)return t[nod]; int mid=l+r>>1; if(y<=mid)return smin(l,mid,x,y,nod*2); if(x>mid)return smin(mid+1,r,x,y,nod*2+1); int L=smin(l,mid,x,y,nod*2),R=smin(mid+1,r,x,y,nod*2+1); return v[L]<v[R]?L:R;}int main(){ n=read();m=read(); for(int i=1;i<=n;i++)a[i]=read(); for(int i=1;i<=m;i++)b[i].x=read(),b[i].y=read(); sort(b+1,b+m+1); for(int i=1;i<=m;i++)v[i]=b[i].y; build(1,m,1);sort(a+1,a+n+1);int ans=0; for(int i=n;i;i--){ int p=lower_bound(b+1,b+m+1,(ppap){a[i],0})-b; if(p>m)return puts("No Solution")&0; int q=smin(1,m,p,m,1); if(v[q]==1e9)return puts("No Solution")&0; ans+=v[q];v[q]=1e9;xg(1,m,q,1); } printf("%d",ans); return 0;}
阅读全文
1 0
- 贪心+线段树(优先队列)——51nod1191 消灭兔子
- 51NOD——1191 消灭兔子 (可用优先队列解决的贪心算法)
- 51nod消灭兔子 贪心+优先队列
- 51nod 1191 消灭兔子 (贪心+优先队列)
- hdu 4544 湫湫系列故事——消灭兔子(贪心,优先队列)
- HDU 4544 湫湫系列故事——消灭兔子 (贪心+优先队列)
- 51Nod-消灭兔子-(优先队列)
- 51nod 1191:消灭兔子 贪心+优先队列
- 51Nod 1191 消灭兔子 (贪心+优先队列)
- 51nod 1191 消灭兔子 贪心+优先队列
- hdu 4544 湫湫系列故事——消灭兔子(优先队列 + 贪心)
- HDU4544 湫湫系列故事——消灭兔子(贪心算法,使用优先队列优化)
- hdu 4544 湫湫系列故事——消灭兔子(贪心+优先队列)
- HDU4544 湫湫系列故事——消灭兔子(贪心+优先队列)
- hdu 4544 湫湫系列故事——消灭兔子(优先队列+贪心)
- HDU 4544 湫湫系列故事——消灭兔子(优先队列+贪心)
- hdu 4544 湫湫系列故事——消灭兔子 优先队列+贪心
- hdu 4544 湫湫系列故事——消灭兔子 贪心+优先队列 解题报告
- Ubuntu Python2.7 django1.3 安装使用过程部分笔记
- jquery 克隆input
- Java
- Paint、Canvas
- Java
- 贪心+线段树(优先队列)——51nod1191 消灭兔子
- unity三种图片格式
- struts2的常量配置
- jmeter服务器上安装
- vlc编译Windows
- PHP常用的四种排序方法及二种查找方法
- 安卓软键盘弹出,遮挡控件的问题
- select2控件动态更新option
- 爬虫入门二(urllib,urllib2)