HDU 5773 The All-purpose Zero 脑洞LIS

给定一个序列,里面的0是可以任变的。问变化后最长的LIS的长度

首先,0全部选上是不亏的。这个不知道怎么说,YY一下吧。

最关键的就是解决2 0 0 3

这种问题了。

注意到这个序列的LIS应该是3

也就是你求LIS的时候,是不能包括0的,因为0是最后全部加上去的。这样你求到的LIS只能是1.

再来一组数据

2 0 0 3 0 0 4

这样的LIS是5,也就是你求到的LIS只能是1.

这样的话,只有2 1 0求到的LIS是1了。

也就是每个数减去它前面出现过多少个0,再求一次LIS.

关键要抓住0要全部用上,想到每个数减去前面有多少个0,比较难想到。抓住0要全部用上。列几组数据,慢慢推还可以。

(其实是我比较水,想不懂)

现在还有点难理解

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 100000 + 20;
int a[maxn];
int b[maxn];
bool pos[maxn];
int dp[maxn];
int dp_up (int a[],int lena)
{
    int begin=1;
    while (pos[begin]) begin++;
    b[1]=a[begin];
    int lenb=1;
    for (int i=begin+1;i<=lena;++i)
    {
        if (pos[i]) continue;
        if (a[i] > b[lenb])
        {
            b[++lenb] = a[i];
        }
        else
        {
            int pos = lower_bound(b+1,b+1+lenb,a[i]) - b;
            b[pos] = a[i];
        }
    }
    return lenb;
}
int f;

void work ()
{
    memset(pos,0,sizeof pos);
    memset(dp,0,sizeof dp);
    int n;
    scanf("%d",&n);
    int begin=-1;
    for (int i=1;i<=n;++i)
    {
        scanf("%d",&a[i]);
        if (a[i]==0) dp[i] = dp[i-1]+1;
        else dp[i]=dp[i-1];
        if (a[i]==0) pos[i]=1;
    }
    if(dp[n]==n)
    {
        printf ("Case #%d: %d\n",++f,n);
        return ;
    }
    for (int i=1;i<=n;++i)
    {
        if (pos[i]) continue;
        else a[i] -= dp[i];
    }
    int ans = dp_up(a,n);
    printf ("Case #%d: %d\n",++f,ans+dp[n]);
    return ;
}
int main()
{
#ifdef LOCAL
    freopen("data.txt","r",stdin);
#endif
    int t;
    scanf("%d",&t);
    while (t--) work();
    return 0;
}

时间: 2024-08-07 08:40:08

HDU 5773 The All-purpose Zero 脑洞LIS的相关文章

hdu 5773 The All-purpose Zero 最长上升子序列+树状数组

题目链接:hdu 5773 The All-purpose Zero 官方题解:0可以转化成任意整数,包括负数,显然求LIS时尽量把0都放进去必定是正确的. 因此我们可以把0拿出来,对剩下的做O(nlogn)的LIS,统计结果的时候再算上0的数量. 为了保证严格递增,我们可以将每个权值S[i]减去i前面0的个数,再做LIS,就能保证结果是严格递增的. 个人看法:对于显然把所以0放进去部分我解释一下: 如果0位于最长上升子序列两边,这两个零要加进去是显然的 如果有一个0夹于最长上升子序列之间,那么

HDU 5773 The All-purpose Zero(O(nlgn)求LIS)

http://acm.hdu.edu.cn/showproblem.php?pid=5773 题意: 求LIS,其中的0可以看做任何数. 思路: 因为0可以看做任何数,所以我们可以先不管0,先求一遍LIS,最后再加上0的个数就可以了.当然,每个数需要减去它前面0的个数. 还有这题如果用dp求LIS是要超时的,从别人那里学习了更快的求LIS的方法. 假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5.n 下面一步一步试着找出它. 我们定义一个序列B

HDU 5773 The All-purpose Zero 求LIS

求最长上升子序列长度: 单纯的dp时间复杂度是O(n*n)的 dp[i] = max(dp[j]+1); (0=<j<=i-1 && a[i]>a[j]) 用二分可以减少查找的时间:时间复杂度:O(n*log(n)) 模板: #define maxn 100010 int a[maxn], b[maxn]; // 二分在b[] 数组里找第一个比num 大的数的位置. int search_(int num, int low, int high) { int mid; wh

【动态规划】【最长上升子序列】HDU 5773 The All-purpose Zero

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何自然数) 题目思路: [动态规划][二分][最长上升子序列] 按最长上升子序列做,遇到0的时候更新所有长度的最优解.(这种暴力解法都能过?而且还比标解快?) 1 // 2 //by coolxxx 3 // 4 #include<iostream> 5 #include<algorithm&

HDU 1160(两个值的LIS,需dfs输出路径)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1160 FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20100    Accepted Submission(s): 8909Special Judge Problem Description FatMou

HDU 5773 The All-purpose Zero(DP)

题目链接:点击打开链接 思路: 首先一点:我们把所有0都用上是肯定不亏的. 接下来:我们把剩下的非0数求LIS, 但是, 这些0有可能不能全部插入LIS中, 那么势必要在LIS中剔出一些数. 一个很简单的方法, 我们把每一个数减去他前面的0的个数, 这样, 相当于先为0留出了空间, 因为是全都减小, 相对大小是不变的. 细节参见代码: #include <cstdio> #include <cstring> #include <algorithm> #include &

hdu 5773 最长递增子序列 (nlogn)+贪心

The All-purpose Zero Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 947    Accepted Submission(s): 453 Problem Description ?? gets an sequence S with n intergers(0 < n <= 100000,0<= S[i] &l

hdu 1087 Super Jumping! Jumping! Jumping! (LIS)

Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24549    Accepted Submission(s): 10838 Problem Description Nowadays, a kind of chess game called "Super Jumping!

HDU 5748 BestCoder Round #84 Bellovin (LIS)

Bellovin Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 540    Accepted Submission(s): 254 Problem Description Peter has a sequence a1,a2,...,an and he define a function on the sequence -- F