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/problem/C
Description
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 Input
3
1 2 3
Sample Output
3
HINT
题意
给你n个数,问你最多分成多少块,使得在块内单独排序之后,然后再组合起来的结果和最后排序的结果一样
题解:
对于每个数i,判断一下这个数到上一个划开的位置的最大值是否小于后面的最小值,如果是,就在这儿划开
最大最小值可以On,也可以偷懒像我一样用线段树。。。
代码
#include<iostream>
#include<stdio.h>
using namespace std; #define maxn 500000
const int inf = 1e9+;
struct node
{
int l,r,ma,mi;
}a[maxn];
int d[maxn];
void build(int x,int l,int r)
{
a[x].l=l,a[x].r=r;
a[x].ma=-inf,a[x].mi=inf;
if(l==r)
{
a[x].ma=d[l];
a[x].mi=d[l];
return;
}
else
{
int mid=(l+r)>>;
build(x<<,l,mid);
build(x<<|,mid+,r);
a[x].ma=max(a[x<<].ma,a[x<<|].ma);
a[x].mi=min(a[x<<].mi,a[x<<|].mi);
}
}
int query1(int x,int st,int ed)
{
int l=a[x].l,r=a[x].r;
if(st<=l&&r<=ed)
return a[x].mi;
else
{
int mid=(l+r)>>;
int mi1=inf,mi2=inf;
if(st<=mid)
mi1=query1(x<<,st,ed);
if(ed>mid)
mi2=query1(x<<|,st,ed);
return min(mi1,mi2);
}
} int query2(int x,int st,int ed)
{
int l=a[x].l,r=a[x].r;
if(st<=l&&r<=ed)
return a[x].ma;
else
{
int mid=(l+r)>>;
int mi1=-inf,mi2=-inf;
if(st<=mid)
mi1=query2(x<<,st,ed);
if(ed>mid)
mi2=query2(x<<|,st,ed);
return max(mi1,mi2);
}
} int main()
{
int n;
scanf("%d",&n);
int dd,ee;
for(int i=;i<=n;i++)
scanf("%d",&d[i]);
build(,,n);
int ans = ;
int tmp = ;
for(int i=;i<n;i++)
{
if(query2(,tmp,i)<=query1(,i+,n))
{
ans++;
tmp = i+;
}
}
cout<<ans<<endl;
}
Codeforces Round #332 (Div. 2) C. Day at the Beach 线段树的更多相关文章
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp
D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...
- 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 ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)
题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x, ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心
B. "Or" Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/578 ...
- Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)
题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...
- Codeforces Round #343 (Div. 2) D - Babaei and Birthday Cake 线段树+DP
题意:做蛋糕,给出N个半径,和高的圆柱,要求后面的体积比前面大的可以堆在前一个的上面,求最大的体积和. 思路:首先离散化蛋糕体积,以蛋糕数量建树建树,每个节点维护最大值,也就是假如节点i放在最上层情况 ...
随机推荐
- 业界最具影响力MySQL精品文章荟萃(300篇)
MySQL是一种关联数据库管理系统,SQL语言是用于访问数据库的最常用标准化语言.本文档收集的资料有MySQL数据库备份与恢复,配置,解决方案等,供大家方便统一阅读. 博客专题 1 MySQL ...
- Linux 7 中Yum 配置 说明
在之前的Blog中写了Linux Yum 的配置方法,参考: Linux 平台下 YUM 源配置 手册 http://www.cndba.cn/account/article/details/154 ...
- 最好最实用的PHP二次开发教程
◆二次开发 1.什么是二次开发? 二次开发,简单的说就是在现有的软件上进行定制修改,功能的扩展,然后达到自己想要的功能和效果,一般来说都不会改变原有系统的内核. 2.为什么要二次开发? 随着信息化技术 ...
- rpm 命令参数使用详解
RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包 二进制包(Binary)以及源代码包(Source)两 ...
- C#中常见的winform控件命名规范
我们知道Button 常常简称为btn,那么Winform中的其它控件呢,这篇文章在C#的winform控件命名规范 的基础上对一些控件的名称的简称进行了整理. 1. 标准控件 NO. 控件类型简写 ...
- java web 学习十(HttpServletRequest对象1)
一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...
- java jvm学习笔记十一(访问控制器)
欢迎装载请说明出处: http://blog.csdn.net/yfqnihao/article/details/8271665 这一节,我们要学习的是访问控制器,在阅读本节之前,如果没有前面几节的 ...
- <转>ERP的测试用例模板
1页面部分(1) 页面清单是否完整(是否已经将所需要的页面全部都列出来了)(2) 页面是否显示(在不同分辨率下页面是否存在,在不同浏览器版本中页面是是否显示)(3) 页面在窗口中的显示是否正确.美观( ...
- 《Python核心编程》 第六章 序列 - 课后习题
课后习题 6–1.字符串.string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? 答:成员关系操作符(in.not in) import string ...
- C语言反转字符串
也是面腾讯的一道编程题=,= 这题比较简单 代码如下: #include <stdio.h> #include <string.h> // 非递归实现字符串反转 char *r ...