6008 1300 6000 2100 500 2000 1000 4000 1100 3000 6000 2000 8000 1400 6000 1200 2000 1900
4 4 5 9 7
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int index[1005]; int dp[1005]; int ans[1005]; struct node { int a; int b; int pos; }nod[1005]; int cmp(node p1,node p2) { if(p1.a<p2.a) return 1; //if(p1.b>p2.b) return 1; return 0; } void debug() { int i; cout<<"******"<<endl; for(i=0;i<9;i++) cout<<nod[i].a<<" "<<nod[i].b<<" "<<nod[i].pos<<endl; cout<<"#######"<<endl; } int main() { int n=0; int p1,p2; int i,j; while(cin>>p1>>p2) { nod[n].a=p1; nod[n].b=p2; nod[n].pos=n+1; n++; } sort(nod,nod+n,cmp); //memset(dp,1,sizeof(dp)); //debug(); for(i=0;i<=1000;i++) { dp[i]=1; index[i]=0; } int tmp,ma=1; for(i=0;i<n;i++) for(j=i+1;j<n;j++) { if(nod[i].a<nod[j].a&&nod[i].b>nod[j].b) { if(dp[i]+1>dp[j]) { dp[j]=dp[i]+1; if(dp[j]>ma) { ma=dp[j]; tmp=j; } index[j]=i; } } } //cout<<ma<<endl; int k=0; while(tmp) { ans[k++]=nod[tmp].pos; tmp=index[tmp]; } cout<<k<<endl; for(i=k-1;i>=0;i--) cout<<ans[i]<<endl; return 0; } /* 6008 1300 6000 2100 500 2000 1000 4000 1100 3000 6000 2000 8000 1400 6000 1200 2000 1900 */
2 1 2 2 1 3 1 2 2 3 3 1
Case 1: My king, at most 1 road can be built. Case 2: My king, at most 2 roads can be built.HintHuge input, scanf is recommended.
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; struct node { int x; int y; }nod[500005]; int ans[500005]; int cmp(node a,node b) { return a.x<b.x; } int main() { int cas=0; int n,i; while(cin>>n) { for(i=0;i<n;i++) scanf("%d%d",&nod[i].x,&nod[i].y); sort(nod,nod+n,cmp); for(i=1;i<=5e5;i++) ans[i]=1e6; for(i=0;i<n;i++) { int x=nod[i].y; int l=0,r=5e5,mid; while(r>l+1) { mid=(l+r)>>1; if(ans[mid]<x) l=mid; else r=mid; } ans[l+1]=x; } int res; for(i=5e5;i>=1;i--) if(ans[i]<1e6) { res=i; break; } printf("Case %d:\n",++cas); if(res>1) printf("My king, at most %d roads can be built.\n\n",res); else printf("My king, at most %d road can be built.\n\n",res); } return 0; } /* 2 1 2 2 1 3 1 2 2 3 3 1 5 2 2 3 3 4 1 1 4 5 5 */
hdu 1025&hdu 1025 LIS(O(n*n)和O(n*log(n)))两种解法
原文:http://blog.csdn.net/coraline_m/article/details/18744627