bzoj3410 [Usaco2009 Dec]Selfish Grazing 自私的食草者
来源:互联网 发布:蓝桥杯软件大赛 编辑:程序博客网 时间:2024/06/06 07:19
题目
贪心经典题目,用一些不相交的线段覆盖区间。
按l排序,记录目前最远的rmx,对于新的线段,如果l>=rmx,就加入,更新rmx。否则判断当前r是否
#include<bits/stdc++.h>#define N 50000 using namespace std;int n,cnt,tmp;struct cow{ int l,r; bool operator <(const cow &A)const { return l<A.l; }}A[N+5];inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline int read(){ int x=0,b=1; char c=nc(); for(;!(c<='9'&&c>='0');c=nc())if(c=='-')b=-1; for(;c<='9'&&c>='0';c=nc())x=x*10+c-'0'; return x*b;}inline void write(int x){ if(x==0)putchar('0'); else { char buf[15]; int len=0; if(x<0)putchar('-'),x=-x; while(x)buf[++len]=x%10+'0',x/=10; for(int i=len;i>=1;i--)putchar(buf[i]); } putchar(' ');}int main(){ freopen("in.txt","r",stdin); n=read(); for(int i=1;i<=n;i++)A[i].l=read(),A[i].r=read(); sort(A+1,A+n+1); cnt=1,tmp=A[1].r; for(int i=2;i<=n;i++) if(A[i].l>=tmp)cnt++,tmp=A[i].r; else if(A[i].r<tmp)tmp=A[i].r; cout<<cnt; return 0;}
速度很快,排到第一了,但是空间比较大(不知为何),所以榜上第二。
阅读全文
0 0
- bzoj3410 [Usaco2009 Dec]Selfish Grazing 自私的食草者
- bzoj3410 [Usaco2009 Dec]Selfish Grazing 自私的食草者
- 【贪心】BZOJ3410(Usaco2009 Dec)[Selfish Grazing 自私的食草者]题解
- 3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者
- [贪心] BZOJ 3410 [Usaco2009 Dec]Selfish Grazing 自私的食草者
- BZOJ 3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者 离散化dp
- [贪心]BZOJ 3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者
- BZOJ 3410 [Usaco2009 Dec]Selfish Grazing 自私的食草者 贪心
- 【USACO2009Decsilver T1 自私的食草者
- toj3479 Selfish Grazing
- [Usaco09Dec] Selfish Grazing
- bzoj1774【Usaco2009 Dec】Toll 过路费
- bzoj1774 [Usaco2009 Dec]Toll 过路费
- bzoj3412 [Usaco2009 Dec]Music Notes乐谱
- 【BZOJ 1774】 [Usaco2009 Dec]Toll 过路费
- Bzoj1775 [Usaco2009 Dec]Vidgame 电视游戏问题
- Bzoj 1774 [Usaco2009 Dec]Toll 过路费
- BZOJ 1774 [Usaco2009 Dec] Toll 过路费
- 图的邻接矩阵数据结构代码实现
- CSP 之 权限查询 java
- 判断程序集是debug还是release版本
- 菜鸟学java第二天
- 丑数
- bzoj3410 [Usaco2009 Dec]Selfish Grazing 自私的食草者
- the selection cannot be run on any server 原因
- 第二章 数据类型和运算符
- 算法第六周Merge k Sorted Lists[hard]
- Android网络编程(六)OkHttp3用法全解析
- 系统安全类命令补充
- 启动Tomcat服务器报错
- Android环境配置
- 含运算放大器电阻电路