The All-purpose Zero

来源:互联网 发布:有网络无法连接 编辑:程序博客网 时间:2024/05/29 11:59

The All-purpose Zero

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


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.
<p>用STL做:</p><p>题解:求最长递增子序列,把0换成你需要的那个数,是他成为最长的递增子序列</p><p>函数lower_bound()在first和last中的<strong>前闭后开</strong>区间进行二分查找,返回大于或等于val的<strong>第一个元素</strong>位置。如果所有元素都小于val,则返回<strong>last</strong>的位置</p><p>举例如下:</p><p>一个数组number序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,111.pos为要插入的位置的下标</p><p>则</p><p>pos = lower_bound( number, number + 8, 3) - number,pos = 0.即number数组的下标为0的位置。</p><p>pos = lower_bound( number, number + 8, 9) - number, pos = 1,即number数组的下标为1的位置(即10所在的位置)。</p><p>pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个元素)。</p><p>所以,要记住:函数lower_bound()在first和last中的<strong>前闭后开</strong>区间进行二分查找,返回大于或等于val的<strong>第一个元素</strong>位置。如果所有元素都小于val,则返回<strong>last</strong>的位置,且last的位置是越界的!!~</p>
#include<cstdio>#include<cstring>#include<cctype>#include<cmath>#include<set>#include<map>#include<list>#include<queue>#include<deque>#include<stack>#include<string>#include<vector>#include<iostream>#include<algorithm>#include<stdlib.h>using namespace std;typedef long long int LL;const int INF=2e9+1e8;const int MAXSIZE=1e6+10;int dp[MAXSIZE];int main(){    int ncase,cases=0;    cin>>ncase;    while(ncase--)    {        int n,ans=-1;        scanf("%d",&n);        for(int i=0; i<=n+1; i++)            dp[i]=INF;        for(int i=0; i<n; i++)        {            int we,k;            scanf("%d",&we);            if(we)            {                k=lower_bound(dp,dp+n,we)-dp;                dp[k]=we;                ans=max(ans,k);            }            else            {                ans++;                for(int j=ans;j-1>=0;j--)                    dp[j]=dp[j-1]+1;                dp[0]=0;            }        }        printf("Case #%d: %d\n",++cases,ans+1);    }    return 0;}

0 0
原创粉丝点击