Codeforces 599C Day at the Beach(想法题,排序)

C. Day at the Beach

One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was 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 were n castles built by friends. Castles are numbered from 1 to n, and the height of the i-th castle is equal tohi. When friends were about to leave, Squidward noticed, that castles are not ordered by their height, and this looks ugly. Now friends are going to reorder the castles in a way to obtain that condition hihi+ 1 holds for all i from 1 to n- 1.

Squidward suggested the following process of sorting castles:

  • Castles are split into blocks — groups of consecutive castles. Therefore the block from i to j will include castles i,i+ 1, ...,j. A block may consist of a single castle.
  • The partitioning is chosen in such a way that every castle is a part of exactly one block.
  • Each block is sorted independently from other blocks, that 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 may always be achieved by saying that the whole sequence is a single block.

Even Patrick understands that increasing the number of blocks in partitioning will ease the sorting process. Now friends ask you to count the maximum possible number of blocks in a partitioning that satisfies all the above requirements.

Input

The first line of the input contains a single integer n (1 n 100 000— the number of castles Spongebob, Patrick and Squidward made from sand during the day.

The next line contains n integers hi (1 hi 109). 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

来自 <http://codeforces.com/contest/599/problem/C

 

【题意】:

N个数字,求最多能分成多少块,使得每个块内单独排序后,整体有序。

 

【解题思路】:

解法非常多。

1、复制数组进行排序,同时求前缀和,每出现一次Sum1==Sum2,说明这一段数值的组成相同,即可以单独排序。

2、顺着求一遍最大前缀,倒着求一遍最小后缀,对于每个i,若最大前缀不大于最小后缀,说明前后两部分可以单独排序,即分块+1

3、线段树--挖坑待填。

解法一:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #define inf 0x3f3f3f3f
 7 #define LL long long
 8 #define maxn 110000
 9 #define IN freopen("in.txt","r",stdin);
10 using namespace std;
11 
12 int n;
13 int a[maxn];
14 int b[maxn];
15 
16 int main(int argc, char const *argv[])
17 {
18     //IN;
19 
20     while(scanf("%d",&n)!=EOF)
21     {
22         for(int i=1; i<=n; i++)
23         {
24             scanf("%d",&a[i]);
25             b[i]=a[i];
26         }
27         sort(b+1,b+n+1);
28 
29         int ans = 0;
30         int tmp = 0;
31         for(int i=1; i<=n; i++)
32         {
33             tmp += a[i]-b[i];
34             if(tmp==0) ans++;
35         }
36 
37         printf("%d
", ans);
38     }
39 
40     return 0;
41 }

解法二:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #define inf 0x3f3f3f3f
 7 #define LL long long
 8 #define maxn 110000
 9 #define IN freopen("in.txt","r",stdin);
10 using namespace std;
11 
12 int n;
13 int num[maxn];
14 int pmax[maxn];
15 int smin[maxn];
16 
17 int main(int argc, char const *argv[])
18 {
19     //IN;
20 
21     while(scanf("%d",&n)!=EOF)
22     {
23         for(int i=1;i<=n;i++)
24             scanf("%d",&num[i]);
25 
26         pmax[1]=num[1];smin[n]=num[n];
27         for(int i=2;i<=n;i++)
28             pmax[i]=max(pmax[i-1],num[i]);
29         for(int i=n-1;i>=1;i--)
30             smin[i]=min(smin[i+1],num[i]);
31 
32         int ans = 0;
33         for(int i=1;i<=n-1;i++){
34             if(pmax[i]<=smin[i+1])
35                 ans++;
36         }
37 
38         printf("%d
", ans+1);
39     }
40 
41     return 0;
42 }
原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5007704.html