Timus 1303
用动态规划超时了,悲剧,没想到可以用贪心O(n)就搞定了。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct node { int x,y; }vert[100010]; int res[100010]; int cmp(node a,node b) { return a.x < b.x; } int work(int n,int m) { int i,k=0,max=0,count=0,pre=0; if(n==0) return 0; for(i=0;i<n;i++) { if(vert[i].x<=pre) { if(max < vert[i].y) { max=vert[i].y; k=i; } } else { if(vert[i].x>max) return 0; pre=max; res[count++]=k; if(pre>=m) return count; max=vert[i].y; k=i; } } if(max<m) return 0; res[count++]=k; return count; } int main() { int i,n,s,t,count,r; while(scanf("%d",&n)!=EOF) { count=0; scanf("%d %d",&s,&t); while(s!=0 || t!=0) { if(!(t<=0 || s>=n)) { vert[count].x=s; vert[count].y=t; count++; } scanf("%d %d",&s,&t); } sort(vert,vert+count,cmp); r=work(count,n); if(r) { printf("%d\n",r); for(i=0;i<r;i++) printf("%d %d\n",vert[res[i]].x,vert[res[i]].y); } else { printf("No solution\n"); } } return 0; }