HDU3564 线段树+dp

来源:互联网 发布:mac ps教程 编辑:程序博客网 时间:2024/06/06 08:41

Problem Description

There is a sequence firstly empty. We begin to add number from 1 to N to the sequence, and every time we just add a single number to the sequence at a specific position. Now, we want to know length of the LIS (Longest Increasing Subsequence) after every time’s add.

Input

An integer T (T <= 10), indicating there are T test cases.
For every test case, an integer N (1 <= N <= 100000) comes first, then there are N numbers, the k-th number Xk means that we add number k at position Xk (0 <= Xk <= k-1).See hint for more details.

Output

For the k-th test case, first output “Case #k:” in a separate line, then followed N lines indicating the answer. Output a blank line after every test case.

Sample Input

1
3
0 0 2

Sample Output

Case #1:
1
1
2

Hint

In the sample, we add three numbers to the sequence, and form three sequences.
a. 1
b. 2 1
c. 2 1 3

这个题初看就是最傻逼的那种lis
但是那种是n^2lgn的复杂度对于10W数据打死都过不去的
所以想了想绝逼是没法在线做的…
采用一种离线的方式
先建出这个所有命令结束以后的数列
再插进去
因为新插进去的永远是最大的
所以只要按照建图顺序找出前面的dp里最大的那个+1就可以转移了
这个过程我想到最后好像只有线段树能实现

然后说下细节问题
我一开始处理这个建图是很傻比的
我用的map企图建立一个重复的标记
让新的图建立在标记次数+输入位置..这样的一个下标上
但是实际上就是很傻比
0 1 2 3 2 2
这组数据的图就建不起来,覆盖了
才想到如果重复次数+输入下标之前被占用了
就会打出一波gg

所以建图也要用线段树来实现..
一开始设置有n个空位的线段树
插入的点只往第x个空位上面插入
插一个减掉一个

但是你看这个题目就给了32MB
就很惨
线段树上面用完了清理清理接着用好了…
不然估计会mle

#include<iostream>#include<algorithm>#include<cstdio>#include<memory.h>using namespace std;int shur[100001], tu[100001], dp[100001],sr[100001];struct p{    int z, y, s;};p shu[600000];void jianshu(int gen, int zuo, int you){    shu[gen].z = zuo;    shu[gen].y = you;    if (zuo == you)return;    int mid = (zuo + you) / 2;    jianshu(2 * gen, zuo, mid);    jianshu(2 * gen + 1, mid + 1, you);}void jianshu1(int gen, int zuo, int you){    shu[gen].z = zuo;    shu[gen].y = you;    shu[gen].s = you - zuo + 1;    if (zuo == you)return;    int mid = (zuo + you) / 2;    jianshu1(2 * gen, zuo, mid);    jianshu1(2 * gen + 1, mid + 1, you);}int chaxun(int gen, int z, int y, int cc){    if (y < cc)return shu[gen].s;    if (z>cc)return 0;    if (y == z&&z == cc)return shu[gen].s;    int mid = (z + y) / 2;    int zz = chaxun(2 * gen, z, mid, cc);    int yy = chaxun(2 * gen + 1, mid + 1, y, cc);    return max(zz, yy);}int gengxin(int gen, int z, int y, int cc){    if (z > cc || y < cc)return shu[gen].s;    if (y == z&&z == cc)return shu[gen].s = dp[cc];    int mid = (z + y) / 2;    int zz = gengxin(2 * gen, z, mid, cc);    int yy = gengxin(2 * gen + 1, mid + 1, y, cc);    return shu[gen].s = max(zz, yy);}void chacha(int gen, int z, int y, int diji,int a){    shu[gen].s--;    if (z == y)    {        sr[a] = z;        tu[z] = a;        return;    }    int mid = (z + y) / 2;    if (shu[2 * gen].s >= diji)chacha(2 * gen, z, mid, diji, a);    else chacha(2 * gen + 1, mid + 1, y, diji - shu[2 * gen].s, a);}int main(){    int T, n;    cin >> T;    int u = 0;    while (T--)    {        memset(shur, 0, sizeof(shur));        memset(tu, 0, sizeof(tu));        memset(dp, 0, sizeof(dp));        memset(shu, 0, sizeof(shu));        memset(sr, 0, sizeof(sr));        scanf("%d", &n);        int y;        for (int a = 1;a <= n;a++)        {            scanf("%d", &y);            shur[a] = y + 1;        }        int xianzai;        jianshu1(1, 1, n);        for (int a = n;a >= 1;a--)chacha(1, 1, n, shur[a],a);    /*  for (int a = 1;a <= n;a++)cout << sr[a] <<" ";        cout << endl;        for (int a = 1;a <= n;a++)cout << tu[a] << " ";        cout << endl;*/        memset(shu, 0, sizeof(shu));        jianshu(1, 1, n);        printf("Case #%d:\n", ++u);        int zuida = 0;        for (int a = 1;a <= n;a++)        {            int yu = chaxun(1, 1, n, sr[a]);            dp[sr[a]] = yu + 1;            zuida = max(zuida, dp[sr[a]]);            gengxin(1, 1, n, sr[a]);            printf("%d\n", zuida);        }        cout << endl;    }    return 0;}
0 0
原创粉丝点击