題目大意爲poor城市要搭建路去rich城市
路不能交叉,而且保證poor城市和rich城市不會有相同的,一開始這裏坑了一把
很明顯LIS最長子序列問題,但是,題目輸出很坑
如果只能搭建一條路的話,輸出時road不能加s
我一直wa,調程序的bug。
一定的記住要看清題意。
直接用二分來優化。
用到了LIS(nlogn)算法。
請點這題,很好理解上述算法
如果不想看就點這裏LIS(nlogn)算法
下面是代碼
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int a[500010];
int d[500010];
int n;
int main()
{
int kase = 1;
while(~scanf("%d",&n)){
for(int i=0,p,r;i<n;i++){
scanf("%d%d",&p,&r);
a[p] = r;
}
int len = 1;
memset(d,0,sizeof(d));
d[1] = a[1];
for(int i=2;i<=n;i++){
int w = lower_bound(d+1,d+len+1,a[i]) - (d+1);
if(w==len){
len++;
d[w+1] = a[i];
}else d[w+1] = a[i];
//printf("%d w %d len %d\n",w,len,d[w+1]);
}
printf("Case %d:\n",kase++);
if(len==1) printf("My king, at most %d road can be built.\n\n",len);
else printf("My king, at most %d roads can be built.\n\n",len);
}
return 0;
}
/*
5
1 4
2 3
3 5
4 1
5 2
7
1 1
2 7
3 4
4 6
5 5
6 2
7 3
*/
此題很水,但是自己還是粗心。