标签:dp
太强了 !!!!!!!!膜拜啊!!
摘:
链接:http://acm.hdu.edu.cn/forum/read.php?tid=15580&fpage=0&page=1然后应该发现一件事情了:在B中插入数据是有序的,而且是进行替换而不需要挪动——也就是说,我们可以使用二分查找,将每一个数字的插入时间优化到O(logN)~~~~~于是算法的时间复杂度就降低到了O(NlogN)~
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; struct node{ int p,r; }king[500001]; int cmp(node a,node b) { if(a.p!=b.p) return a.p<b.p; } int dp[500001]; int fun(int l,int t,int x) { int mid=(l+t)/2; while(l<=t) { mid=(l+t)/2; if(dp[mid]<x) l=mid+1; else t=mid-1; } return l; } int main() { int i,j,n,m; int sum=0; int cnt=1; while(scanf("%d",&n)!=EOF) { sum=1; for(i=1;i<=n;i++) scanf("%d%d",&king[i].p,&king[i].r); sort(king+1,king+n+1,cmp); memset(dp,0,sizeof(dp)); dp[1]=king[1].r; for(i=2;i<=n;i++) { if(dp[sum]>king[i].r) { int tot=fun(1,sum,king[i].r); dp[tot]=king[i].r; } else { dp[++sum]=king[i].r; } } printf("Case %d:\n",cnt++); if(sum==1) printf("My king, at most %d road can be built.\n\n",sum); else printf("My king, at most %d roads can be built.\n\n",sum); } return 0; }
HDU 1025 Constructing Roads In JGShining's Kingdom
标签:dp
原文地址:http://blog.csdn.net/l133236116/article/details/43153487