HDU 5773 The All-purpose Zero(贪心LIS)

来源:互联网 发布:全境封锁购买 知乎 编辑:程序博客网 时间:2024/05/16 10:20

http://acm.hdu.edu.cn/showproblem.php?pid=5773

给你一串数字,里面0可以变成其他数字,问你变化之后LIS的长度


这题首先要发现,选0肯定比选别的更优,所以我们可以把0都去掉,在没有0的情况下,考虑选几个数字,然后如果对于ai和aj,他们俩要选,他们的差值应该大于他们俩之间0的个数,所以我们可以记录sumi表示到i为止,0的个数,然后对于没有0的数组,要选上升的话必须满足aiaj>sumisumj,所以就是aisumi>ajsumj,所以把每个元素变化一下,然后求LIS,然后加上0的个数

这个姿势很不错,以后碰到LIS需要想到


代码

#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <string>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <sstream>#include <cstdlib>#include <iostream>#include <algorithm>#pragma comment(linker,"/STACK:102400000,102400000")using namespace std;#define   MAX           100005#define   MAXN          1000005#define   maxnode       205#define   sigma_size    26#define   lson          l,m,rt<<1#define   rson          m+1,r,rt<<1|1#define   lrt           rt<<1#define   rrt           rt<<1|1#define   middle        int m=(r+l)>>1#define   LL            long long#define   ull           unsigned long long#define   mem(x,v)      memset(x,v,sizeof(x))#define   lowbit(x)     (x&-x)#define   pii           pair<int,int>#define   bits(a)       __builtin_popcount(a)#define   mk            make_pair#define   limit         10000//const int    prime = 999983;const int    INF   = 0x3f3f3f3f;const LL     INFF  = 0x3f3f;const double pi    = acos(-1.0);const double inf   = 1e18;const double eps   = 1e-4;const LL    mod    = 1e9+7;const ull    mx    = 133333331;/*****************************************************/inline void RI(int &x) {      char c;      while((c=getchar())<'0' || c>'9');      x=c-'0';      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; }/*****************************************************/vector<int> v;int st[MAX];int main(){    //freopen("in.txt","r",stdin);    int t,kase=0;    scanf("%d",&t);    while(t--){        int n;        cin>>n;        v.clear();        int num=0;        for(int i=0;i<n;i++){            int a;            scanf("%d",&a);            if(a==0) num++;            else v.push_back(a-num);        }        int top=0;        for(int i=0;i<v.size();i++){            if(top==0||v[i]>st[top-1]) st[top++]=v[i];            else{                int pos=lower_bound(st,st+top,v[i])-st;                st[pos]=v[i];            }        }        kase++;        printf("Case #%d: %d\n",kase,top+num);    }    return 0;}
0 0