Codeforces Round #332 (Div. 2)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.
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.
Print the maximum possible number of blocks in a valid partitioning.
3
1 2 3
3
4
2 1 3 2
2
In the first sample the partitioning looks like that: [1][2][3].
In the second sample the partitioning is: [2, 1][3, 2]
题意:给你n个数,让你分块,快内数字进行排序,使得最后整体为升序,且块数尽量最大
题解:我们先离散化数据,再分别插入树状数组中,每次查询当前位置能否分块,能?自然就ans++;
///
#include<bits/stdc++.h>
using namespace std; typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//****************************************
const int N=+;
#define maxn 100000+5
int C[maxn],n;
int getsum(int x) {
int sum=;
while(x>) {
sum+=C[x];
x-=(x&-x);
}
return sum;
} void update(int x,int c) {
while(x<=n) {
C[x]+=c;
x+=(x&-x);
}
}
int a[N],b[N];
map<int ,int >Hash;
int main() { n=read();
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
b[i]=a[i];
}sort(b+,b+n+);
for(int i = ; i <= n; i++) {
if(Hash.count(a[i])) {
Hash[a[i]]++;
a[i] = Hash[a[i]];
}
else {
int tmp = a[i];
a[i] = lower_bound(b+, b+n+, a[i]) - b;
Hash[tmp] = a[i];
}
}int ans=;
for(int i=;i<=n;i++) {
update(a[i],);
int g=getsum(i) ;
if(g==i) {
ans++;
}
}
cout<<ans<<endl;
return ;
}
代码
Codeforces Round #332 (Div. 2)C. Day at the Beach 树状数组的更多相关文章
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp
C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...
- Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】
主题链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),当中有m(1 ≤ m ≤ ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
C. Inna and Candy Boxes Inna loves sweets very much. She has n closed present boxes lines up in a ...
- Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum 树状数组+离线
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...
- Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)
E. Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input sta ...
- 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 ...
随机推荐
- Struts2之一 初体验
Struts2 框架是基于MV模式开发的,它提供了一个核心控制器,用于对所有的请求进行统一处理,这个控制器是由一个名为FilterDispatcher的Servlet过滤器来充当的. 01.需要在we ...
- Objective-C——Runtime理解
动态语言 OC是一门不折不扣的动态语言,所以它的很多机制都是动态运行时决定的.这点和C语言不一样,C语言是静态绑定,也就是编译后所有的一切都已经决定了.这一点和C语言的函数指针有些类似,很多时候函数指 ...
- Elasticsearch_Lucene基础
Lucene基本概念 文档(document):索引与搜索的主要载体,它包含一个或多个字段,存放将要写入索引的或将从索引搜索出来的数据. 字段(field):文档的一个片段,它包含字段的名称和字段的内 ...
- SAS学习笔记之《SAS编程与数据挖掘商业案例》(4)DATA步循环与控制、常用全程语句、输出控制
SAS学习笔记之<SAS编程与数据挖掘商业案例>(4)DATA步循环与控制.常用全程语句.输出控制 1. 各种循环与控制 DO组 创建一个执行语句块 DO循环 根据下标变量重复执行DO和E ...
- 安卓socket 心跳和信鸽自定义提示音
/** * 连接socket 和心跳 */ public class SocketService extends Service { private static addNewOrderInterfa ...
- Floyd模板
比较简单的算法:但是当点太多需要剪枝,不然很耗时 void Floyd() { ;k<n;++k) ;i<n;++i) ;j<n;++j) dj[i][j] = min(dj[i][ ...
- 02--Tomcat总体结构分析一
注:此文章大部分参考大神文档,并且结合自身理解,补充了其他相关知识,谢绝转载. 大神原文地址链接:http://www.ibm.com/developerworks/cn/java/j-lo ...
- JavaWeb项目中引入spring框架
主要步骤有以下3步: 1:下载spring的jar包2:在项目中web.xml中添加spring配置3:bean配置文件-applicationContext.xml 1:引入包,这个就不说了,官网下 ...
- registerDataSetObserver:浅析Andorid ListView和Adapte
最近由于遇到将内容分部绑定到ListView里的需求,追踪源码之后对ListView和Adapter有了点肤浅的认识,在此与大家分享. 这里用到了观察者模式,在ListView的setAdapter里 ...
- 文艺平衡树(区间翻转)(Splay模板)
这篇blog写的吼啊 #include<cstdio> #include<iostream> #include<cstring> using namespace s ...