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 ...
随机推荐
- Android 串口驱动和应用测试
这篇博客主要是通过一个简单的例子来了解Android的串口驱动和应用,为方便后续对Android串口服务和USB虚拟串口服务的了解.这个例子中,参考了<Linux Device Drivers& ...
- Kotlin实例----android5.0新特性之palette
一.Palette的使用 使用Palette可以让我们从一张图片中拾取颜色,将拾取到的颜色赋予ActionBar,StatusBar以及UI背景色可以让界面色调实现统一或者加载不同图片时同步变化色调 ...
- 《Java编程的逻辑》第二部分 面向对象
- Angular——内置指令
内置指令 ng-app 指定应用根元素,至少有一个元素指定了此属性. ng-controller 指定控制器 ng-show控制元素是否显示,true显示.false不显示 ng-hide控制元素是否 ...
- TensorFlow:Windows下使用TensorFlow-Python版本
原文链接:Win10X64下安装使用TensorFlow 安装TensorFlow 由于Google那帮人已经把 TensorFlow 打成了一个 pip 安装包,所以现在可以用正常安装包的方式安装 ...
- sratookit
sratookit 下载后解压 tar -zxvf sratoolkit.2.8.2-1-ubuntu64.tar.gz 移动到专门安装生物信息软件的目录下 mv sratoolkit.2.8.2-1 ...
- Day 13 进程和线程
进程和线程 今天我们使用的计算机早已进入多CPU或多核时代,而我们使用的操作系统都是支持“多任务”的操作系统,这使得我们可以同时运行多个程序,也可以将一个程序分解为若干个相对独立的子任务,让多个子任务 ...
- Vova and Trophies CodeForces - 1082B(思维题)
Vova has won nn trophies in different competitions. Each trophy is either golden or silver. The trop ...
- Django CBV视图解决csrf认证
urls.py from django.conf.urls import url from appxx import views urlpatterns = [ url(r"^$" ...
- 29. 误拼写时的fuzzy模糊搜索技术
搜索的时候,可能输入的搜索文本会出现误拼写的情况,这时就需要es为我们进行智能纠错 比如有两个文档: doc1: hello world doc2: hello java 现在要搜索:hall ...