hdu5489 Removed Interval

针对移除连续子序列后的最长上升子序列问题,通过记录两个函数f[i]和g[i],并使用线段树进行维护,实现了高效的算法解决方案。


Problem Description
Given a sequence of numbers A=a1,a2,,aN, a subsequence b1,b2,,bk of A is referred as increasing if b1<b2<<bk. LY has just learned how to find the longest increasing subsequence (LIS).
Now that he has to select L consecutive numbers and remove them from A for some mysterious reasons. He can choose arbitrary starting position of the selected interval so that the length of the LIS of the remaining numbers is maximized. Can you help him with this problem?
 

Input
The first line of input contains a number T indicating the number of test cases (T100).
For each test case, the first line consists of two numbers N and L as described above (1N100000,0LN). The second line consists of N integers indicating the sequence. The absolute value of the numbers is no greater than 109.
The sum of N over all test cases will not exceed 500000.
 

Output
For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from 1. Y is the maximum length of LIS after removing the interval.
 

Sample Input
2 5 2 1 2 3 4 5 5 3 5 4 3 2 1
 

Sample Output
Case #1: 3

Case #2: 1

这题想了很长时间,题意是求切去长度为l的连续子序列后,剩下的序列的最长上升子序列,切的起始位置随意定。

可以记录两个函数

f[i]:以a[i]为尾端的lis的最大长度。

g[i]:以a[i]为起始点的lis的最大长度。两者都包含a[i]。

这样对于每一个点,我们可以根据i, 找到 [0,i−L−1]之间的一个值,使得这个值小于a[i],且长度最大,可以用线段树来维护。然后用maxx=max(maxx,ans+g[i]),即前半段加上后半段就行了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 100060
int f[maxn],g[maxn],pos[maxn],a[maxn],c[maxn],a1[maxn],h[maxn];
struct node{
    int l,r,maxnum;
}b[4*maxn];

void build(int l,int r,int i)
{
    int mid;
    b[i].l=l;b[i].r=r;b[i].maxnum=0;
    if(l==r)return;
    mid=(l+r)/2;
    build(l,mid,i*2);
    build(mid+1,r,i*2+1);
}

void update(int idx,int num,int i)
{
    int mid;
    if(b[i].l==idx && b[i].r==idx){
        b[i].maxnum=max(b[i].maxnum,num);return;
    }
    mid=(b[i].l+b[i].r)/2;
    if(idx<=mid)update(idx,num,i*2);
    else update(idx,num,i*2+1);
    b[i].maxnum=max(b[i*2].maxnum,b[i*2+1].maxnum);
}

int question(int l,int r,int i)
{
    int mid;
    if(l>r)return 0;
    if(b[i].l==l && b[i].r==r){
        return b[i].maxnum;
    }
    mid=(b[i].l+b[i].r)/2;
    if(r<=mid) return question(l,r,i*2);
    else if(l>mid)return question(l,r,i*2+1);
    else{
        int lv=question(l,mid,i*2);
        int rv=question(mid+1,r,i*2+1);
        return max(lv,rv);
    }

}

int main()
{
    int n,m,i,j,T,l,tot,len,maxx,ans,num1=0,k;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&l);
        tot=0;
        for(i=1;i<=n;i++){
            scanf("%d",&a[i]);
            pos[i]=a[i];
        }
        sort(pos+1,pos+1+n);
        tot=unique(pos+1,pos+1+n)-pos-1;
        for(i=1;i<=n;i++)a[i]=lower_bound(pos+1,pos+1+tot,a[i])-pos;
        f[1]=1;len=1;c[1]=a[1];
        for(i=2;i<=n;i++){
            if(a[i]>c[len]){
                len++;f[i]=len;
                c[len]=a[i];continue;
            }
            j=lower_bound(c+1,c+1+len,a[i])-c;
            c[j]=a[i];
            f[i]=j; //这里很重要,是j,不是len,因为求的是以a[i]为结尾的最长长度,不是前i个的最长长度
        }

        for(i=1;i<=n;i++){
            a1[i]=-a[n+1-i];
        }

        g[1]=1;len=1;c[1]=a1[1];
        for(i=2;i<=n;i++){
            if(a1[i]>c[len]){
                len++;g[i]=len;
                c[len]=a1[i];continue;
            }
            j=lower_bound(c+1,c+1+len,a1[i])-c;
            c[j]=a1[i];
            g[i]=j;
        }
        reverse(g+1,g+1+n);

        build(1,100000,1);
        maxx=0;
        for(i=l+1;i<=n;i++){
            ans=question(1,a[i]-1,1);
            update(a[i-l],f[i-l],1);
            maxx=max(maxx,ans+g[i]);
        }
        if(n-l>=1){
            maxx=max(maxx,f[n-l]);
        }
        num1++;
        printf("Case #%d: %d\n",num1,maxx);
    }
    return 0;
}
/*
2
10 2
1 2 3 9 8 7 4 5 6 7
*/



Problem Description
Given a sequence of numbers A=a1,a2,,aN, a subsequence b1,b2,,bk of A is referred as increasing if b1<b2<<bk. LY has just learned how to find the longest increasing subsequence (LIS).
Now that he has to select L consecutive numbers and remove them from A for some mysterious reasons. He can choose arbitrary starting position of the selected interval so that the length of the LIS of the remaining numbers is maximized. Can you help him with this problem?
 

Input
The first line of input contains a number T indicating the number of test cases (T100).
For each test case, the first line consists of two numbers N and L as described above (1N100000,0LN). The second line consists of N integers indicating the sequence. The absolute value of the numbers is no greater than 109.
The sum of N over all test cases will not exceed 500000.
 

Output
For each test case, output a single line consisting of “Case #X: Y”. X is the test case number starting from 1. Y is the maximum length of LIS after removing the interval.
 

Sample Input
2 5 2 1 2 3 4 5 5 3 5 4 3 2 1
 

Sample Output
Case #1: 3

Case #2: 1

这题想了很长时间,题意是求切去长度为l的连续子序列后,剩下的序列的最长上升子序列,切的起始位置随意定。

可以记录两个函数

f[i]:以a[i]为尾端的lis的最大长度。

g[i]:以a[i]为起始点的lis的最大长度。两者都包含a[i]。

这样对于每一个点,我们可以根据i, 找到 [0,i−L−1]之间的一个值,使得这个值小于a[i],且长度最大,可以用线段树来维护。然后用maxx=max(maxx,ans+g[i]),即前半段加上后半段就行了。

基于模拟退火的计算器 在线运行 访问run.bcjh.xyz。 先展示下效果 https://pan.quark.cn/s/cc95c98c3760 参见此仓库。 使用方法(本地安装包) 前往Releases · hjenryin/BCJH-Metropolis下载最新 ,解压后输入游戏内校验码即可使用。 配置厨具 已在2.0.0弃用。 直接使用白菜菊花代码,保留高级厨具,新手池厨具可变。 更改迭代次数 如有需要,可以更改 中39行的数字来设置迭代次数。 本地编译 如果在windows平台,需要使用MSBuild编译,并将 改为ANSI编码。 如有条件,强烈建议这种本地运行(运行可加速、可多次重复)。 在 下运行 ,是游戏中的白菜菊花校验码。 编译、运行: - 在根目录新建 文件夹并 至build - - 使用 (linux) 或 (windows) 运行。 最后在命令行就可以得到输出结果了! (注意顺序)(得到厨师-技法,表示对应新手池厨具) 注:linux下不支持多任务选择 云端编译已在2.0.0弃用。 局限性 已知的问题: - 无法得到最优解! 只能得到一个比较好的解,有助于开阔思路。 - 无法选择菜品数量(默认拉满)。 可能有一定门槛。 (这可能有助于防止这类辅助工具的滥用导致分数膨胀? )(你问我为什么不用其他语言写? python一个晚上就写好了,结果因为有涉及json读写很多类型没法推断,jit用不了,算这个太慢了,所以就用c++写了) 工作原理 采用两层模拟退火来最大化总能量。 第一层为三个厨师,其能量用第二层模拟退火来估计。 也就是说,这套方法理论上也能算厨神(只要能够在非常快的时间内,算出一个厨神面板的得分),但是加上厨神的食材限制工作量有点大……以后再说吧。 (...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值