Codeforces 599C Day at the Beach (idea title, sort)

Source: Internet
Author: User
Tags cmath

C. Day at the Beach

One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather is bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles.

At the end of the day there wereNCastles built by friends. Castles is numbered from1ToN, and the height of the i-th Castle is equal tohi hi≤hi+ 1< Span lang= "X-none" > holds for All i from 1  to n-1

Squidward suggested the following process of sorting castles:

  • Castles is split into blocks -groups of consecutive Castles. Therefore The block from i to J would include castles i,i+ 1, ...,J. A block may consist of a single castle.
  • The partitioning is chosen in such a by that every castle are a part of exactly one block.
  • Each of the blocks is sorted independently from blocks, which is the sequence hi,hi+ 1,. ..,HJ becomes sorted.
  • The partitioning should satisfy the condition that after each block is sorted, the sequence hi becomes Sorted too. This could always be achieved by saying, the whole sequence is a single block.

Even Patrick understands that increasing the number of blocks in partitioning would ease the sorting process. Now friends ask for count the maximum possible number of blocks in a partitioning so satisfies all the above requirem Ents.

Input

the first line of the input contains a single integer  n  (1 ≤< Span lang= "X-none" >n≤ 000)   -the number of castles Spongebob, Patrick and Squidward made from sand during the day.

The next line contains n integers hi (1 ≤hi≤ 9 ). The i-th of these integers corresponds to the height of the i-th Castle.

Output

Print the maximum possible number of blocks in a valid partitioning.

Sample Test (s)

Input

3

1 2 3

Output

3

Input

4

2 1 3 2

Output

2

From <Http://codeforces.com/contest/599/problem/C>

Codeforces Round #332 (Div. 2)

"Test Instructions":

N numbers, the maximum number of blocks can be divided, so that each block within a single order, the overall order.

"Problem-solving ideas":

There are many solutions.

1, copy the array to sort, at the same time to seek the prefix and, each occurrence of Sum1==sum2, indicating that the composition of the value of the same, that can be sorted separately.

2, follow the maximum prefix, backward to find the minimum suffix, for each i, if the maximum prefix is not greater than the minimum suffix, the two parts can be ordered separately, that is, sub-block +1.

3, line tree - dig pit to fill.

Solution One:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6 #defineINF 0x3f3f3f3f7 #defineLL Long Long8 #defineMAXN 1100009 #defineIn Freopen ("In.txt", "R", stdin);Ten using namespacestd; One  A intN; - intA[MAXN]; - intB[MAXN]; the  - intMainintargcChar Const*argv[]) - { -     //in ; +  -      while(SCANF ("%d", &n)! =EOF) +     { A          for(intI=1; i<=n; i++) at         { -scanf"%d",&a[i]); -b[i]=A[i]; -         } -Sort (b +1, b+n+1); -  in         intAns =0; -         intTMP =0; to          for(intI=1; i<=n; i++) +         { -TMP + = a[i]-B[i]; the             if(tmp==0) ans++; *         } $ Panax Notoginsengprintf"%d\n", ans); -     } the  +     return 0; A}

Solution Two:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6 #defineINF 0x3f3f3f3f7 #defineLL Long Long8 #defineMAXN 1100009 #defineIn Freopen ("In.txt", "R", stdin);Ten using namespacestd; One  A intN; - intNUM[MAXN]; - intPMAX[MAXN]; the intSMIN[MAXN]; -  - intMainintargcChar Const*argv[]) - { +     //in ; -  +      while(SCANF ("%d", &n)! =EOF) A     { at          for(intI=1; i<=n;i++) -scanf"%d",&num[i]); -  -pmax[1]=num[1];smin[n]=Num[n]; -          for(intI=2; i<=n;i++) -Pmax[i]=max (pmax[i-1],num[i]); in          for(inti=n-1; i>=1; i--) -Smin[i]=min (smin[i+1],num[i]); to  +         intAns =0; -          for(intI=1; i<=n-1; i++){ the             if(pmax[i]<=smin[i+1]) *ans++; $         }Panax Notoginseng  -printf"%d\n", ans+1); the     } +  A     return 0; the}

Codeforces 599C Day at the Beach (idea title, sort)

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.