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 ;
}

Codeforces 599C Day at the Beach(想法题,排序)的更多相关文章

  1. CodeForces 111B - Petya and Divisors 统计..想法题

    找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...

  2. CodeForces - 669D Little Artem and Dance 想法题 多余操作

    http://codeforces.com/problemset/problem/669/D 题意:n个数1~N围成一个圈.q个操作包括操作1:输入x, 所有数右移x.操作2:1,2位置上的数(swa ...

  3. Codeforces 599C. Day at the Beach 模拟

    Day at the Beach time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. CodeForces 599C Day at the Beach

    预处理一下i到n的最小值. #include<cstdio> #include<cstring> #include<cmath> #include<algor ...

  5. HDU 4972 Bisharp and Charizard 想法题

    Bisharp and Charizard Time Limit: 1 Sec  Memory Limit: 256 MB Description Dragon is watching NBA. He ...

  6. HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...

  7. Codeforces Round #609 (Div. 2)前五题题解

    Codeforces Round #609 (Div. 2)前五题题解 补题补题…… C题写挂了好几个次,最后一题看了好久题解才懂……我太迟钝了…… 然后因为longlong调了半个小时…… A.Eq ...

  8. codeforces 657C - Bear and Contribution [想法题]

    题目链接: http://codeforces.com/problemset/problem/657/C ----------------------------------------------- ...

  9. Codeforces 602B Approximating a Constant Range(想法题)

    B. Approximating a Constant Range When Xellos was doing a practice course in university, he once had ...

随机推荐

  1. Android adb 常用技巧

    1.在命令行管理模拟器设备(AVD) list:列出机器上所有已经安装的Android版本和AVD设备 list avd:列出机器上所有已经安装的AVD设备: list target:列出机器上所有已 ...

  2. jetty ZipException: invalid entry size

    The issue, as I suspected, was due a corrupt JAR file. The solution for me was to clear my local rep ...

  3. easyui-dialog中文件上传处理

    function openDialog() { // $('#dlg').dialog('open'); //EasyUi的dialog中文件上传,后台获取不到文件,需要改写为下面这样 $(" ...

  4. UVa 11752 (素数筛选 快速幂) The Super Powers

    首先有个关键性的结论就是一个数的合数幂就是超级幂. 最小的合数是4,所以枚举底数的上限是pow(2^64, 1/4) = 2^16 = 65536 对于底数base,指数的上限就是ceil(64*lo ...

  5. erl0003-ets 几种类型的区别和ets效率建议 <转>

    rlang内置大数据量数据库 ets,dets 初窥 发布日期:2011-10-24 18:45:48   作者:dp studio ets是Erlang term storage的缩写, dets则 ...

  6. Spring学习之AOP

    Spring-AOP(Aspect-orented programming) 在业务流程中插入与业务无关的逻辑,这样的逻辑称为Cross-cutting concerns,将Crossing-cutt ...

  7. 一、oracle 高水位线详解

    一.什么是水线(High Water Mark)? 所有的oracle段(segments,在此,为了理解方便,建议把segment作为表的一个同义词) 都有一个在段内容纳数据的上限,我们把这个上限称 ...

  8. 解释一下,在你往浏览器中输入一个URL后都发生了什么,要尽可能详细

    这道题目没有所谓的完全的正确答案,这个题目可以让你在任意的一个点深入下去, 只要你对这个点是熟悉的.以下是一个大概流程: 浏览器向DNS服务器查找输入URL对应的IP地址. DNS服务器返回网站的IP ...

  9. 嵌入式 hi3518c下ramdisk文件系统与文件系统烧写以及uboot中change-the-env

    NULL RAM : mkdir ramdisk_test  临时挂在点 dd if=/dev/zero of=123 bs=1k count=10000 建立空硬盘 losetup /dev/loo ...

  10. 【转】堆栈跟踪中收到一个UnhandledExceptionFilter调用时,如何查找问题异常堆栈

    定义没有异常处理程序来处理引发的异常时调用UnhandledExceptionFilter函数.函数通常将异常传递到捕获并处理它所尝试的 Ntdll.dll 文件. 在某些情况下,在其中存在的进程内存 ...