codeforces 599c

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 to hi. 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 castlesi, 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
Note

In the first sample the partitioning looks like that: [1][2][3].

In the second sample the partitioning is: [2, 1][3, 2】

题意:给出一组数据,要求你进行分块选择,如 2 1 3 2分成【2,1】,【3,2】,那么块内进行排序之后这些块拼接起来就是一个有序序列,要求分成尽可能多的块

思路:开始以为是寻求最长上升子序列的题,然而 1 3 5 1 3 6这组数据就是一个反例。。。。。

应该是逐个查看:以i为分界线,那么在i之前找一个最大数MAX,在i之后找一个最小数MIN,如果如果MAX小于等于MIN,那么答案就累加1,代表i这个数字可以分成一个独立的块(为什么?因为如果i之后存在一个比它小的数,那么它肯定不能成为前面的一个独立的块,同理,i之前如果有一个大于i的数大于i之后的最小数,那么i也不能成为独立的一个快,它必须存在于MAX到MIN之间)

#include <iostream>
#include <cstring>
#include <queue>
#define M(a) memset(a,0,sizeof(a))
using namespace std;
const int maxsize=1e5+;
int a[maxsize];
int MIN[maxsize];
int main()
{
int n;
while(cin>>n)
{
for(int i=;i<=n;i++) {cin>>a[i];} int mi=1e9+;
for(int i=n;i>=;i--)
{
mi=min(mi,a[i]);
MIN[i]=mi;
} int ans=;
int maxx=a[];
for(int i=;i<=n;i++)
{
maxx=max(maxx,a[i]);
if(i==n) {ans++; continue;}
int minx=MIN[i+];
if(maxx<=minx) ans++;
}
cout<<ans<<endl;
}
return ;
}

C. Day at the Beach的更多相关文章

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

    C. Day at the Beach One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunate ...

  2. Codeforces Round #332 (Div. 2) C. Day at the Beach 线段树

    C. Day at the Beach Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599/p ...

  3. Codeforces Round #326 (Div. 2) D. Duff in Beach dp

    D. Duff in Beach Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/probl ...

  4. CF- Day at the Beach

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

  5. 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 ...

  6. codeforces 587B B. Duff in Beach(dp)

    题目链接: B. Duff in Beach time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. Codeforces 553D Nudist Beach(二分答案 + BFS)

    题目链接 Nudist Beach 来源  Codeforces Round #309 (Div. 1) Problem D 题目大意: 给定一篇森林(共$n$个点),你可以在$n$个点中选择若干个构 ...

  8. codeforces 553 D Nudist Beach

    题意大概是.给出一个图,保证每一个点至少有一条边以及随意两点间最多一条边.非常显然这个图有众多点集,若我们给每一个点定义一个权值,那每一个点集都有一个最小权值点,如今要求出一个点集,这个点集的最小权值 ...

  9. Codeforces Round #332 (Div. 2)C. Day at the Beach 树状数组

    C. Day at the Beach   One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortuna ...

随机推荐

  1. bzoj1604

    treap+并查集 我们能想到一个点和最近点对连接,用并查集维护,但是这个不仅不能求,而且还是不对的,于是就看了题解 把距离转为A(x-y,x+y),这样两点之间的距离就是max(x'-X',y'-Y ...

  2. VIDIOC_S_INPUT 作用 (转载)

    转载:http://blog.csdn.net/kickxxx/article/details/7088658 G_INPUT和S_INPUT用来查询和选则当前的input 一个video设备节点可能 ...

  3. E20170612-sl

     tampon n.     卫生棉塞; 止血棉塞;    sanitary  n. 公共厕所;  adj.     卫生的; 清洁的;    belonging n.     附属品,附件,属性; ...

  4. [Swift通天遁地]一、超级工具-(7)创建一个图文并茂的笔记本程序

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. MySQL replace into 用法(insert into 的增强版)

    转 http://blog.csdn.net/risingsun001/article/details/38977797 MySQL replace into 用法(insert into 的增强版) ...

  6. 在计算机视觉与人工智能领域,顶级会议比SCI更重要(内容转)

    很多领域,SCI是王道,尤其在中国,在教师科研职称评审和学生毕业条件中都对SCI极为重视,而会议则充当了补充者的身份.但是在计算机领域,尤其是人工智能与机器学习领域里,往往研究者们更加青睐于会议 我无 ...

  7. css3 动画 vs js 动画

    之前被问到过,css3 动画与 js 动画孰优孰劣,脑袋的第一反应就是性能上肯定 css3 动画会好很多,但别人说不对,我就在想,不对?难道还有别的原因吗?答案是肯定的.先来看看二者实现动画的原理吧. ...

  8. JavaScript(十一)Dom

    Dom(Document object module) 1.获取dom对象的方法 正常用的方法 推荐 getElementById()//通过id选择唯一的dom getElementsByClass ...

  9. arduino相关资料和网站

    2018-02-0212:59:12 昨天晚上在论坛里看了大半夜,找到了很多有意思的项目,总结一下! http://www.51hei.com/bbs/dpj-105654-1.html   ---贪 ...

  10. Python学习日记之字典深复制与浅复制

    Python中通过copy模块有两种复制(深复制与浅复制) copy 浅复制 复制时只会复制父对象,而不会复制对象的内部的子对象. deepcopy 深复制 复制对象及其子对象 因此,复制后对原dic ...