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 hi≤hi+ 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、线段树--挖坑待填。

解法一:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define inf 0x3f3f3f3f
#define LL long long
#define maxn 110000
#define IN freopen("in.txt","r",stdin);
using namespace std; int n;
int a[maxn];
int b[maxn]; int main(int argc, char const *argv[])
{
//IN; while(scanf("%d",&n)!=EOF)
{
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b+n+); int ans = ;
int tmp = ;
for(int i=; i<=n; i++)
{
tmp += a[i]-b[i];
if(tmp==) ans++;
} printf("%d\n", ans);
} return ;
}

解法二:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define inf 0x3f3f3f3f
#define LL long long
#define maxn 110000
#define IN freopen("in.txt","r",stdin);
using namespace std; int n;
int num[maxn];
int pmax[maxn];
int smin[maxn]; int main(int argc, char const *argv[])
{
//IN; while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d",&num[i]); pmax[]=num[];smin[n]=num[n];
for(int i=;i<=n;i++)
pmax[i]=max(pmax[i-],num[i]);
for(int i=n-;i>=;i--)
smin[i]=min(smin[i+],num[i]); int ans = ;
for(int i=;i<=n-;i++){
if(pmax[i]<=smin[i+])
ans++;
} printf("%d\n", ans+);
} return ;
}

最新文章

  1. WiX Toolset 教程索引页
  2. Android之NetworkOnMainThreadException异常
  3. 安装less
  4. nyoj-291 互素数个数 欧拉函数
  5. 表与表 不同条件下的关联SQL
  6. Git CMD - fetch: Download objects and refs from another repository
  7. android之硬件访问服务框架
  8. Python属性、方法和类管理系列之----__slots__属性
  9. 未能从程序集“WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35“ 中加载“System.Windows.SplashSceen”
  10. 处理通过&lt;input type=&quot;file&quot;&gt;的Post 请求
  11. [RM 状态机详解2] RMAppAttempt状态机详解
  12. Ajax.html:35 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user&#39;s experience. For more help, check https://xhr.spec.whatwg.org
  13. Jenkins强制语言设置
  14. Linux shell 重定向学习笔记
  15. CSS3中很容易混淆的transform,translate,transition。如何去区分,以及综合写法。
  16. 微信APP支付,支付宝APP支付demo
  17. 【Ubuntu】如何修改IP
  18. nginx与php-fpm通信的两种方式
  19. R8500 MPv2 版本 刷梅林改版固件
  20. Use Memory Layout from Target Dialog Scatter File

热门文章

  1. 【转载】Java垃圾回收内存清理相关(虚拟机书第三章),GC日志的理解,CPU时间、墙钟时间的介绍
  2. HDU 4923
  3. BootStrap基本样式
  4. bzoj2489
  5. Sass结合Modernizr的使用方法
  6. bzoj1567: [JSOI2008]Blue Mary的战役地图
  7. POJ 3084 Panic Room (最小割建模)
  8. HDU 1269 迷宫城堡 (强连通分量,常规)
  9. 【C#学习笔记】类型转换
  10. zoj 1119 /poj 1523 SPF