hdu5773 --2016多校第四场1010

来源:互联网 发布:pymongo 遍历大量数据 编辑:程序博客网 时间:2024/06/01 12:23

The All-purpose Zero

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


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
272 0 2 1 2 0 561 2 3 3 0 0
 

Sample Output
Case #1: 5Case #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.

题意:有一个序列,含有n个数,序列中的0可以变成任意数,问你变换完后最长上升子序列的长度是多少

显然在最后面的0可以作为最长上升子序列的元素,那么前面的0怎么变换呢,思路是这样的:把0去掉,做LIS为了保证是添上0变换的数字后是上升的,把每个非0数减去前面包含的0的个数,然后len加上0的个数即可

样例一 2 0 2 1 2 0 5 -> 2 (0) 1 0 1 (0) 3 新序列是 2 1 0 1 3 去掉了两个0,新序列LIS=3

那么原序列LIS=3+2 =5;

#include <stdio.h>#include <iostream>#include <stdlib.h>using namespace std;const int inf=0x3f3f3f3f,maxn=1e5+10;int a[maxn],c[maxn],len;int find(int R,int x){    int L=1,mid;    while(L<=R)    {        mid=(L+R)/2;        if(c[mid]==x)return mid;        else if(c[mid]<=x)            L=mid+1;        else R=mid-1;    }    return L;}int main(){    int n,m,T=1,t;    int sum,j,zero,tot;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        sum=0;        tot=0;        int x;        for(int i=1;i<=n;i++)        {            scanf("%d",&x);            if(x==0)                sum++;            else                a[tot++]=x-sum;        }        if(tot==0)        {             printf("Case #%d: %d\n",T++,n);             continue;        }        len=0;        c[0]=-inf;        for(int i=0;i<tot;i++)        {            if(a[i]>c[len])                j=++len;            else            j=find(len,a[i]);            c[j]=a[i];        }        /*for(int i=1;i<=len;i++)            cout<<c[i]<<" ";        cout<<endl;*/        printf("Case #%d: %d\n",T++,len+sum);    }}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 驾驶证照片泡水不清楚怎么办 驾驶证过了180天怎么办 天津有公司执照怎么办落户 温州车在上海年审手续怎么办 广东小高考考了d怎么办 深圳开摩托抓到怎么办 深圳车卖了车牌怎么办 a1驾照时期过了怎么办 b1驾照扣了12分怎么办 北京的驾照换证怎么办 b2汽车驾驶证年审过期几天怎么办 上海驾照到期人在外地怎么办 交警开的罚单交不了怎么办 珠海交警微信交罚单扣分怎么办 驾驶证违法罚款单子没有了怎么办 转账密码输错3次怎么办 汽车违章扣6分怎么办 汽车扣了72分年检怎么办 汽车扣了50分怎么办 汽车扣了15分怎么办 汽车扣了27分怎么办 汽车扣了40分怎么办 行驶证掉了怎么办 异地 高速上没带驾驶证行驶证怎么办 身份证驾驶证行驶证都丢了怎么办 驾驶证年审过期一个月怎么办 柴油车辆年检尾气不合格怎么办 驾驶证过期了5天怎么办 过了审车时间怎么办 骑车没带行驶证怎么办 轿车行驶证丢了怎么办 车子没年检被扣怎么办 上海车辆年检过期了怎么办 行驶证过期十天怎么办 行驶证盖章满了怎么办 驾照c证扣12分怎么办 两年小车忘年审怎么办 4年车检过期了怎么办 车检过期了1周怎么办 超过检车几天了怎么办 驾驶证扣两个6分怎么办