Day at the Beach
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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
Note

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

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

题意:将这n个数分成几个区间,在每个区间内进行升序排序,最后要总体是升序的。

思路:后面的区间的最小的数一定会比前面区间最大的数还要大或者相等。Max[i]表示从1一个数到第i的数中最大的那个数,那Max数组一定是升序的。从最后一个数开始往前,Min表示当前的最小值,如果最小值大于或等于当前位置的最大值,那么区间数量就加1。

#include<bits/stdc++.h>
using namespace std;
int h[],Max[];
int main()
{
int i,n;
scanf("%d",&n);
Max[]=;
for(i=; i<=n; i++)
{
scanf("%d",&h[i]);
if(h[i]>Max[i-]) Max[i]=h[i];
else Max[i]=Max[i-];
}
int ans=,Min=;
for(i=n;i>;i--)
{
if(Min>=Max[i]) ans++;
if(Min>h[i]) Min=h[i]; }
cout<<ans<<endl;
}

Codeforces 599C. 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 599C Day at the Beach

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

  3. codeforces 723B Text Document Analysis(字符串模拟,)

    题目链接:http://codeforces.com/problemset/problem/723/B 题目大意: 输入n,给出n个字符的字符串,字符串由 英文字母(大小写都包括). 下划线'_' . ...

  4. Codeforces Round #304 C(Div. 2)(模拟)

    题目链接: http://codeforces.com/problemset/problem/546/C 题意: 总共有n张牌,1手中有k1张分别为:x1, x2, x3, ..xk1,2手中有k2张 ...

  5. Codeforces 749C:Voting(暴力模拟)

    http://codeforces.com/problemset/problem/749/C 题意:有n个人投票,分为 D 和 R 两派,从1~n的顺序投票,轮到某人投票的时候,他可以将对方的一个人K ...

  6. Educational Codeforces Round 2 A. Extract Numbers 模拟题

    A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  7. Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))

    B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. CodeForces 797C Minimal string:贪心+模拟

    题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...

  9. Codeforces Beta Round #3 C. Tic-tac-toe 模拟题

    C. Tic-tac-toe 题目连接: http://www.codeforces.com/contest/3/problem/C Description Certainly, everyone i ...

随机推荐

  1. js常见兼容

    滚动条的兼容写法(谷歌chrome) document.documentElement.scrollTop || document.body.scrollTop   阻止浏览器默认行为的兼容写法  e ...

  2. QT按键(Qbutton)改变颜色

    第一种是按键上面的字颜色的改变:   ui->motor1->setStyleSheet("color: red"); 效果: 第二种是背景改变: ui->mot ...

  3. servlet里的过滤器filter

    过滤器的主要作用 1,任何系统或网站都要判断用户是否登录. 2,网络聊天系统或论坛,功能是过滤非法文字. 3,统一解决编码 怎么创建一个过滤器: 1,生成一个普通的class类,实现Filter接口( ...

  4. systemctl管理系统配置、服务

    systemctl daemon-reload \&& systemctl enable docker \&& systemctl start docker \& ...

  5. Code First use dotConnect for MySQL

    http://www.dotblogs.com.tw/yc421206/archive/2014/03/24/144507.aspx dotConnect for MySQL 是一家強大的 3rd C ...

  6. spring security 学习文档

    web service Prepared by:   Sea                                                                       ...

  7. MySQL Windows 安装与配置

    <01> 下载解压 MySQL 至 D盘, 将文件夹改名为 MySQL <02> 控制台状态下进入 MySQL/bin 目录 输入 mysqld -install  回车 安装 ...

  8. 关于消息队列的使用----ActiveMQ,RabbitMQ,ZeroMQ,Kafka,MetaMQ,RocketMQ

    一.消息队列概述消息队列中间件是分布式系统中重要的组件,主要解决应用解耦,异步消息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构.目前使用较多的消息队列有ActiveMQ,RabbitM ...

  9. Web Deploy

    Web Deploy 服务器安装设置与使用 Win2008R2配置WebDeploy Visual Studio 使用Web Deploy发布项目

  10. spring cloud 服务提供者

    1. pom 依赖: <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...