Codeforces 599C Day at the Beach(想法题,排序)
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.
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
【题意】:
N个数字,求最多能分成多少块,使得每个块内单独排序后,整体有序。
【解题思路】:
解法非常多。
1、复制数组进行排序,同时求前缀和,每出现一次Sum1==Sum2,说明这一段数值的组成相同,即可以单独排序。
2、顺着求一遍最大前缀,倒着求一遍最小后缀,对于每个i,若最大前缀不大于最小后缀,说明前后两部分可以单独排序,即分块+1。
3、线段树--挖坑待填。
解法一:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define inf 0x3f3f3f3f
#define LL long long
#define maxn 110000
#define IN freopen("in.txt","r",stdin);
using namespace std; int n;
int a[maxn];
int b[maxn]; int main(int argc, char const *argv[])
{
//IN; while(scanf("%d",&n)!=EOF)
{
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b+n+); int ans = ;
int tmp = ;
for(int i=; i<=n; i++)
{
tmp += a[i]-b[i];
if(tmp==) ans++;
} printf("%d\n", ans);
} return ;
}
解法二:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define inf 0x3f3f3f3f
#define LL long long
#define maxn 110000
#define IN freopen("in.txt","r",stdin);
using namespace std; int n;
int num[maxn];
int pmax[maxn];
int smin[maxn]; int main(int argc, char const *argv[])
{
//IN; while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
scanf("%d",&num[i]); pmax[]=num[];smin[n]=num[n];
for(int i=;i<=n;i++)
pmax[i]=max(pmax[i-],num[i]);
for(int i=n-;i>=;i--)
smin[i]=min(smin[i+],num[i]); int ans = ;
for(int i=;i<=n-;i++){
if(pmax[i]<=smin[i+])
ans++;
} printf("%d\n", ans+);
} return ;
}
Codeforces 599C Day at the Beach(想法题,排序)的更多相关文章
- CodeForces 111B - Petya and Divisors 统计..想法题
找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...
- CodeForces - 669D Little Artem and Dance 想法题 多余操作
http://codeforces.com/problemset/problem/669/D 题意:n个数1~N围成一个圈.q个操作包括操作1:输入x, 所有数右移x.操作2:1,2位置上的数(swa ...
- 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 ...
- CodeForces 599C Day at the Beach
预处理一下i到n的最小值. #include<cstdio> #include<cstring> #include<cmath> #include<algor ...
- HDU 4972 Bisharp and Charizard 想法题
Bisharp and Charizard Time Limit: 1 Sec Memory Limit: 256 MB Description Dragon is watching NBA. He ...
- HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题
http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...
- Codeforces Round #609 (Div. 2)前五题题解
Codeforces Round #609 (Div. 2)前五题题解 补题补题…… C题写挂了好几个次,最后一题看了好久题解才懂……我太迟钝了…… 然后因为longlong调了半个小时…… A.Eq ...
- codeforces 657C - Bear and Contribution [想法题]
题目链接: http://codeforces.com/problemset/problem/657/C ----------------------------------------------- ...
- Codeforces 602B Approximating a Constant Range(想法题)
B. Approximating a Constant Range When Xellos was doing a practice course in university, he once had ...
随机推荐
- HDU 4686 Arc of Dream(矩阵)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 思路: #include <iostream>#include <cs ...
- Sublime Text汉化方法和注册码
汉化方法 安装 SublimeText3 汉化包运行SublimeText3 点击 Preferneces -> Browse Packages 会打开 X:\..\Sublime Text 3 ...
- Android手机拍照
参考的这个视频教程:http://v.youku.com/v_show/id_XNjI5MzkzMjQ4.html和官方API档file:///D:/Android/androidstudio/sdk ...
- 结构体struct和typedef后面接指针的含义
typedef struct file { ... }FileInfo, *FileP; 上述程序中定义了一个结构体,结构体的名字为file,并且给其指针 取个别名为FileP,所以后续程序中出现Fi ...
- 利用Java自带的MD5加密java.security.MessageDigest;
MD5加密算法,即"Message-Digest Algorithm 5(信息-摘要算法)",它由MD2.MD3.MD4发展而来的一种单向函数算法(也就是HASH算法),它是国际著 ...
- 结构体mem_pool_t
/** Memory area header */ typedef struct mem_area_struct mem_area_t; /** Memory pool */ typedef stru ...
- 类Item_equal
class Item_equal: public Item_bool_func { List<Item_field> fields; /* list of equal field item ...
- LwIP源代码文件目录解析
1 -- LwIP源代码文件目录 root@motadou:/home/motadou/lwip/lwip-1.4.1# tree . ├── CHANGELOG ├── COPYING ├── do ...
- 【转】Android fill_parent和wrap_content分析
fill_parent设置一个顶部布局或控件强制性让它布满整个屏幕. wrap_content布局指根据视图内部内容自动扩展以适应其大小. 1. wrap_content <?xml versi ...
- android 触摸事件、点击事件的区别
针对屏幕上的一个View控件,Android如何区分应当触发onTouchEvent,还是onClick,亦或是onLongClick事件? 在Android中,一次用户操作可以被不同的View按次序 ...