UVA10020用最少的线段去覆盖给定的线段
2016-03-13 22:29
302 查看
这题是个贪心,先把每条线段按照左端点进行排序,然后贪心,怎么贪呢,
每次都找到最长的线段,记录下坐标,(如果某个坐标为负值,初始的为负值,即表示下面有一段断掉了,直接return 0)更新起点值,具体的看代码:
每次都找到最长的线段,记录下坐标,(如果某个坐标为负值,初始的为负值,即表示下面有一段断掉了,直接return 0)更新起点值,具体的看代码:
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<string> #include<set> #include<map> #include<iostream> #include<algorithm> #include<vector> #include<cctype> #include<queue> #define LL long long using namespace std; const int maxn=1e5+10; const double eps=1e-8; const int Max=2147483647; struct line { double a,b; }lin[maxn]; int m; int l; int t[maxn]; int re; int cmp(line x,line y) { if(x.a==y.a) return x.b<y.b; return x.a<y.a; } int solve() { double t1=0; int tem=0; while(t1<m) { int tt=-1; double ma=0; for(;tem<l;tem++) { if(lin[tem].a<=t1) { if(lin[tem].b-t1>ma) { ma=lin[tem].b-t1; tt=tem; } } else break; } if(tt<0) return 0; t1=lin[tt].b; t[re++]=tt; } return re; } int main() { int T; cin>>T; while(T--) { cin>>m; l=0; while(scanf("%lf%lf",&lin[l].a,&lin[l].b)) { if(lin[l].a==0&&lin[l].b==0) break; l++; } re=0; // for(int i=0;i<l;i++) // cout<<lin[i].a<<' '<<lin[i].b<<endl; sort(lin,lin+l,cmp); int ans=solve(); if(!ans) cout<<0<<endl; else { cout<<ans<<endl; for(int i=0;i<ans;i++) cout<<lin[t[i]].a<<' '<<lin[t[i]].b<<endl; } if(T) cout<<endl; } }
相关文章推荐
- 引用路径 ../ ./ 说明
- BOM
- 【机器学习详解】线性回归、梯度下降、最小二乘的几何和概率解释
- swift 之xib自定义view可视化到storyboard
- 123
- UI自定义进度条控件
- 从start_kernel到init
- Java微信支付之公众号支付、扫码支付
- 文件存储格式转换(ASCII <-> UTF-8)
- Windows静态库和动态库的创建和使用
- Linux内核分析第三周
- C++第一次使用
- 顺序表的几种实现
- 粗谈《Java与模式》一书
- Unity3D 5 官方教程:标准着色器:内容和环境;金属与镜面工作流
- 设计模式(三)
- OpenGL+VS2010+Win7配置简记
- JVM字节码指令集简介
- java day02 交换数据
- TortoiseSVN 图文使用教程