hdu 5763 The All-purpose Zero (贪心/数据结构+dp)


The All-purpose Zero

                            Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                          Total Submission(s): 935    Accepted Submission(s): 451

Problem Description
?? gets an sequence S with n intergers(0 < n <= 100000,0<= S[i] <= 1000000).?? has a magic so that he can change 0 to any interger(He does not need to change all 0 to the same interger).?? wants you to help him to find out the length of the longest increasing (strictly) subsequence he can get.
 
Input
The first line contains an interger T,denoting the number of the test cases.(T <= 10)
For each case,the first line contains an interger n,which is the length of the array s.
The next line contains n intergers separated by a single space, denote each number in S.
 
Output
For each test case, output one line containing “Case #x: y”(without quotes), where x is the test case number(starting from 1) and y is the length of the longest increasing subsequence he can get.
 
Sample Input
  
  
2 7 2 0 2 1 2 0 5 6 1 2 3 3 0 0
 
Sample Output
  
  
Case #1: 5 Case #2: 5
Hint
In the first case,you can change the second 0 to 3.So the longest increasing subsequence is 0 1 2 3 5.
 
Author
FZU
 
Source
 
         题意:给了一个序列,其中0可以变成任意一个整数,求最长上升子序列长度。

         思路:贪心+dp:看着题解,到现在还不是理解的很彻底,先引用一下题解,抽时间再理解理解。

       题解:0可以转化成任意整数,包括负数,显然求LIS时尽量把0都放进去必定是正确的。因此我们可以把0拿出来,对剩下的做O(nlogn)的LIS,统计结果的时候再算上0的数量。为了保证严格递增,我们可以将每个权值S[i]减去i前面0的个数,再做LIS,就能保证结果是严格递增的。

       线段树+dp:还是数据结构好,虽然代码多,但好理解,而且复杂度和贪心+dp的一样。

        dp[i]表示长度为i的上升序列结尾最小是多少。当A[i] == 0 时,其实对于dp数组就是整体加1后右移,也就是for each i ,dp[i+1]=dp[i]+1 ,这里可以拿线段树来实现,区间更新(加1),左边界左移。不过有一点值得注意:dp数组要初始化为 -INF(或小于-1000000的任一个值)。不然1 0 2 0 1 0 4 0 5 0运行出来可能是6,答案是7,有的程序这个数据不对也能过了,总之数据比较水。具体可以看代码。


        听说拿树状数组或二分+单调栈也可以实现。不过这题数据比较水,当时队友拿n^2的直接78ms就过了,他在A[i]==0的时候拿循环更新的。


详细见代码:

          贪心+dp:

                

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1e5+100;
#define INF 0x3f3f3f3f
int dp[maxn];
int A[maxn];
int main()
{
    int t,case1=0;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        int i,j;
        printf("Case #%d: ",++case1);
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&A[i]);
            dp[i]=INF;
        }
        dp[0]=INF;
        int ans=0;
        for(i=1;i<=n;i++)
        {    
            if(A[i]==0)
                ans++;
            else
                *lower_bound(dp,dp+n,A[i]-ans)=A[i]-ans;
        }
        printf("%d\n",(int)(lower_bound(dp,dp+n+1,INF)-dp+ans));
    }
    return 0;
}
        


          线段树+dp:

        

//时间202ms
//用线段树B求最长上升子序列长度
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn=1e5+100;
int A[maxn];
int B[maxn<<2];
int cnt[maxn<<2];
int n;
inline void push_down(int rt)  //区间更新的延迟更新
{
    if(cnt[rt])
    {
        cnt[rt<<1]+=cnt[rt];
        cnt[rt<<1|1]+=cnt[rt];
        B[rt<<1]+=cnt[rt];
        B[rt<<1|1]+=cnt[rt];
        cnt[rt]=0;
    }
}
void update(int L,int R,int l,int r,int rt)//区间更新,整体+1
{
    if(L<=l&&R>=r)
    {
        B[rt]++;
        cnt[rt]++;
        return ;
    }
    int mid=(l+r)>>1;
    push_down(rt);
    if(L<=mid) update(L,R,lson);
    if(R>mid) update(L,R,rson);
    B[rt]=max(B[rt<<1],B[rt<<1|1]);
}
void change(int pos,int val,int l,int r,int rt) //改变pos位置的值
{
    if(l==r)
    {
        B[rt]=val;
        return ;
    }
    int mid=(l+r)>>1;
    push_down(rt);
    if(pos<=mid) change(pos,val,lson);
    else change(pos,val,rson);
    B[rt]=max(B[rt<<1],B[rt<<1|1]);
}
void work(int val,int l,int r,int rt)  //LIS把长度为i的结尾的值改为更小的值val,直接利用线段树的结构来更新
{
    if(l==r)
    {
        B[rt]=val;
        return;
    }
    int mid=(l+r)>>1;
    push_down(rt);
    if(B[rt<<1]>=val) work(val,lson);
    else work(val,rson);
    B[rt]=max(B[rt<<1],B[rt<<1|1]);
}
void solve()    
{
    int ll=n;
    int rr=n;
    int i;
    for(i=1;i<=n;i++)//优化一下dp数组刚开始时的范围,因为后面要左边界左移,优化一下,避免开更大的内存
    {
        if(A[i]!=0)
            rr--;
    }
    ll=rr+1;
    for(i=1;i<=n;i++)
    {
        if(A[i]==0)
        {
            if(rr>=ll)
            {
                update(ll,rr,1,n,1); //整体加1
            }
            ll--;//左边界左移
        }
        else
        {//这里就是LIS的内容了,B是线段树的数组,也就是dp数组吧
            if(B[1]<A[i]) 
            {
                rr++;
                change(rr,A[i],1,n,1);
            }
            else
            {
                work(A[i],1,n,1);
            }
        }
    }
    printf("%d\n",rr-ll+1);
}
int main()
{
    int t,case1=0;
    scanf("%d",&t);
    while(t--)
    {
        printf("Case #%d: ",++case1);
        scanf("%d",&n);
        int i,j;
        for(i=1;i<(maxn<<2);i++)  //线段树初始为-INF
        B[i]=-INF;
        memset(cnt,0,sizeof cnt);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&A[i]);
        }
        solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值