【UVA - 10020 】Minimal coverage (贪心,区间覆盖问题)
生活随笔
收集整理的這篇文章主要介紹了
【UVA - 10020 】Minimal coverage (贪心,区间覆盖问题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題干:(Uva題不給題干了)
? ? ?t組樣例,每組首先給出一個M,然后給出一些線段(0 0結束),然后問怎么取能使得最少的線段覆蓋區間[0, M]。
Sample Input
2
1
-1 0
-5 -3
2 5
0 0
1
-1 0
0 1
0 0
Sample Output
0
1
0 1?? ?
解題報告:
? ?就是個貪心啊
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; struct Node {int st,ed;Node(){}Node(int st,int ed):st(st),ed(ed){}bool operator<(const Node & b) const{if(st != b.st) return st < b.st;return ed > b.ed;} } node[MAX]; int tot,n,m,cnt,nb; int ans[MAX]; int main() {int t;int a,b,maxx,minn;cin>>t;while(t--) {tot=cnt=0;maxx = 0,minn = 0x3f3f3f3f;scanf("%d",&m);while(scanf("%d%d",&a,&b)) {if(a==0 && b==0) break;if(b<=0) continue;if(a>=m) continue;node[++tot] = Node(a,b);maxx = max(maxx,b);minn = min(minn,a);}sort(node+1,node+tot+1);int cure,curs;curs=cure=0; // if(minn >= m || maxx <= 0) { // puts("0"); // if(t) puts(""); // continue; // }int flag = 0;for(int i = 1; i<=tot; ) {if(node[i].st > curs) {break; }while(i<=tot && node[i].st<=curs) {if(node[i].ed > cure) {cure = node[i].ed;nb = i;}i++;}//if(i!=tot) i--;ans[++cnt] = nb;curs = cure;if(curs >= m) {flag=1;break;}}if(flag == 0) puts("0");else {printf("%d\n",cnt);for(int i = 1; i<=cnt; i++) printf("%d %d\n",node[ans[i]].st,node[ans[i]].ed);}if(t) puts("");}return 0 ;}總結: 那兩句讀數據的時候的if去掉好像也可以AC,,反正注釋掉的那一部分加上也可以AC。。
總結
以上是生活随笔為你收集整理的【UVA - 10020 】Minimal coverage (贪心,区间覆盖问题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 海通国际拆了一台比亚迪 写出87页天花板
- 下一篇: 【CodeForces - 289D】P