UVA-1471 Defense Lines tree-like array/two min

Source: Internet
Author: User

Defense Lines

After the last war devastated your country, you-as the king of the Land of ardenia-decided it was
Improve the defense of your capital city. A part of your forti?cation are a line of Mage
Towers, starting near the city and continuing to the northern Woods. Your Advisors determined that the
Quality of the defense depended only on one factor:the length of a longest contiguous tower sequence
of increasing heights. (They gave you a lengthy explanation, but the only thing you understood is
That it had something to does with ring energy bolts at enemy forces).
After some hard negotiations, it appeared the building new towers is out of question. Mages of
Ardenia has agreed to demolish some of their towers, though. Demolish arbitrary number of
Towers, but the mages enforced one Condition:these Towers has to be consecutive.
For example, if the heights of towers were, respectively, 5, 3, 4, 9, 2, 8, 6, 7, 1, then by demolishing
Towers of Heights 9, 2, and 8, the longest increasing sequence of consecutive towers is 3, 4, 6, 7.
Input
The input contains several test cases. The. RST line of the input contains a positive integer z≤25,
Denoting the number of test cases. Then Z-test cases follow, each conforming to the format described
Below.
The input instance consists of the lines. The. RST one contains one positive integer n≤2 105
Denoting the number of towers. The second line contains n positive integers not larger than 109
Separated by single spaces being the heights of the towers.
Output
For each test case, the your program have to write a output conforming to the format described below.
You should output one line containing the length of a longest increasing sequence of consecutive
Towers, achievable by demolishing some consecutive towers or no tower @ all.
Sample Input
2
9
5 3 4 9 2 8 6 7 1
7
1 2 3 10 4 5 6
Output
4
6

Test instructions

Give you a sequence of n number, you can delete a continuous sub-sequence, so that the remainder of the sequence has a maximum length of continuous increment sub-sequence, ask you what is the longest

Exercises

The first thing we think about is to enumerate which paragraph to delete, and eventually it will be the longest

But obviously O (n^3)

Our preprocessing for I this number to the left and right respectively can be derived from the length, this is O (n^2), not enough

We enumerate only the right endpoint, do not enumerate the left endpoint, but use a binary/tree array to find a j<i and A[j]<a[i] The value of the largest a[j];

#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<map>using namespacestd;Const intN = 1e6+7, M =30005, mod = 1e9+7, INF =0x3f3f3f3f; typedefLong Longll;//The difference is 1, the same is 0intn,a[n],b[n],righ[n],lef[n],t; intC[n],a[n]; intLowbit (intx) {returnx& (-x); }    voidAddintXintadd) {         for(; x < N; x + =lowbit (x)) {C[x]=Max (add,c[x]); }    }    intSumintx) {ints =0;  for(; x >0; X-=lowbit (x)) {s=Max (c[x],s); }        returns; }voidinit () {memset (C,0,sizeof(C)); memset (Righ,0,sizeof(righ)); memset (Lef,0,sizeof(LEF)); Righ[n]=1; lef[1] =1;  for(inti = n1; I >=1; i--){        if(A[i] >= a[i+1]) Righ[i] =1; ElseRigh[i] = righ[i+1] +1; }     for(inti =2; I <= n;i++){        if(A[i] > a[i-1]) Lef[i] = lef[i-1] +1; ElseLef[i] =1; }   //for (int i=n;i>=1;i--) cout<<righ[i]<<endl;}intMain () {intT; scanf ("%d",&T);  while(t--) {       intAns =1; scanf ("%d",&N);  for(intI=1; i<=n;i++) scanf ("%d", &a[i]), b[i] =A[i]; Sort (b+1, b+n+1); intc = Unique (b +1, b+n+1)-B-1;  for(intI=1; i<=n;i++) A[i] = Lower_bound (b +1, b+c+1, A[i])-b;        Init (); Add (a[1],lef[1]);  for(intI=2; i<=n;i++) {ans= Max (Ans,sum (a[i]-1)+Righ[i]);        Add (A[i],lef[i]); } printf ("%d\n", ans); }    return 0;}

UVA-1471 Defense Lines tree-like array/two min

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.