HDU 5773 Maximum increment subsequence (NLOGN) + greedy

Source: Internet
Author: User

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 Descriptionsequence S with n intergers (0 < n <= 100000,0<= S[i] <= 1000000).? have a magic so that he can Change 0 to any Interger (He does not need to change all 0 to the same interger).?? Wants him to find out the length of the longest increasing (strictly) subsequence he can get. 

InputThe first line contains an Interger t,denoting the number of the the test cases. (T <=)
For each case,the first line contains a Interger N,which is the length of the array s.
the next line contains n Intergers separated is a single space, and denote each number in S. 

Outputfor each test case, output one line containing ' case #x: Y ' (without quotes), where x is the ' test Case number (starting from 1) and Y are the length of the longest increasing subsequence he can get. 

Sample Input272 0 2 1 2 0 561 2 3 3 0 0 

Sample OutputCase #1:5 case #2:5 HintIn the first case,you can change the second 0 to 3.So of the longest increasing subsequence is 0 1 2 3 5.Test Instructions: give you n numbers, you can turn 0 of them into arbitrary numbers, and you'll finally get the longest strictly ascending subsequence,
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath > #include <stdlib.h>using namespace std;typedef long long ll;const int Inf=0x3f3f3f3f;const int Mod=1e9+7;const    int N=1e5+10;int A[n],ans[n];int Main () {int cas,n,x,kk=0;    scanf ("%d", &cas);        while (cas--) {scanf ("%d", &n);        int cnt=0,num=0;            for (int i=1;i<=n;i++) {scanf ("%d", &x);            if (!x) cnt++;        else a[++num]=x-cnt;            } if (!num) {printf ("Case #%d:%d\n", ++kk,cnt);        Continue        } int len=1;        ANS[1]=A[1];            for (int i=2;i<=num;i++) {if (A[i]>ans[len]) ans[++len]=a[i];              else {int pos=lower_bound (ans+1,ans+len,a[i])-ans;            Ans[pos]=a[i]; }} printf ("Case #%d:%d\n ", ++kk,len+cnt); } return 0;}

0 can be converted to any integer, including negative numbers, obviously to find the LIS as far as possible to put 0 in must be correct. So we can take 0 out of the remaining to do O (Nlogn) of the LIS, the statistical results of the time to calculate the number of 0. In order to guarantee the strict increment, we can subtract each weight value s[i] minus I front 0 number, then do LIS, can guarantee the result is strictly increment.

HDU 5773 Maximum increment subsequence (NLOGN) + greedy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.