跳过正文
  1. Posts/

hdu 1950 Bridging signals (LIS)

·1 分钟

题目链接 题意:有两跟柱子并排竖直放置,每根柱子有n个结点,从上往下标号1..n, 两根柱子间的结点间要连线,给出计划连接的情况。a[i]表示左边结点i连接右边结点a[i].但是要求连线不能交叉,所以计划可能不能全部执行。现在问最多能连接多少条线。 思路:由于不能交叉,而左边的结点是按照顺序给出的,所以右边连接的结点只能是越来越大。其实就是求a[i]的最长上升子序列。感觉算是LIS的一个比较巧妙的应用? 由于n还是很大。所以必须nlogn的做法。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年04月01日 星期五 20时43分24秒
 4File Name :code/hdu/1950.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=4E4+7;
34int n;
35int a[N];
36int dp[N];
37int g[N];
38
39int main()
40{
41	#ifndef  ONLINE_JUDGE
42	freopen("code/in.txt","r",stdin);
43  #endif
44	ios::sync_with_stdio(false);
45	int T;
46	cin>>T;
47	while (T--)
48	{
49	    cin>>n;
50	    for ( int i = 1 ; i <= n ; i++) cin>>a[i];
51	    ms(g,0x3f);
52	    int ans = 0 ;
53	    for ( int i = 1 ; i <= n ; i++)
54	    {
55		int k = lower_bound(g+1,g+n+1,a[i])-g;
56		dp[i] = k;
57		g[k] = a[i];
58		ans = max(ans,dp[i]);
59	    }
60
61	    cout<<ans<<endl;
62	}
63
64
65
66  #ifndef ONLINE_JUDGE
67  fclose(stdin);
68  #endif
69    return 0;
70}

相关文章

最长上升子序列nlogn解法

·2 分钟
首先回顾一下n^2的做法。 状态转移方程为dp[i] =max(1,dp[j]) (1=<j<=i-1&&a[i]>a[j]) 1for ( int i = 1 ; i <= n ; i++) cin>>a[i]; 2 3 for ( int i = 1 ; i <= n ; i++) 4 { 5 dp[i] = 1; 6 for ( int j = 1 ; j < i ; j++) 7 { 8 if (a[i]>a[j]) 9 dp[i] = max(dp[i],dp[j]+1); 10 } 11 ans = max(ans,dp[i]); 12 } 然后我们发现,使得dp[i]得到同一个值的dp[j]可能有多个,那么选择哪个呢? 假设 x<y<i,a[x]<a[y],dp[x]==dp[y],那么我们选择x好还是y好呢? 显然是x好。为什么?因为选择x潜力大。因为可能在x,y之间存在一个z,满足a[x]<a[z]<a[y],如果选择a[y],就没有办法选择可能使长度更长的a[z]了。通俗得说。。我们要求的是最长上升子序列。。你一开始就弄那么大。。。后面还上哪上升去啊。。。长度小啊。。。

bzoj 1602: [Usaco2008 Oct]牧场行走 (bfs,优先队列)

·2 分钟
Description N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草。 这n块土地被n-1条边连接。 奶牛可以在边上行走,第i条边连接第Ai,Bi块牧场,第i条边的长度是Li(1<=Li<=10000)。 这些边被安排成任意两头奶牛都可以通过这些边到达的情况,所以说这是一棵树。 这些奶牛是非常喜欢交际的,经常会去互相访问,他们想让你去帮助他们计算Q(1<=q<=1000)对奶牛之间的距离。