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 ...
随机推荐
- 自定义View(6)paint设置图图层重叠时的显示方式,包含清空canvas
Paint.setXfermode 这个函数设置两个图层相交时的模式 在已有的图层上绘图将会在其上面添加一层新的图层. 如果新的图层是完全不透明的,那么它将完全遮挡住下面的图层,而setXfermod ...
- poj 1845 Sumdiv (数论)
题目链接 题意:求 A^B的所有约数之和对9901取模后的结果. 分析: 看了小优的博客写的. 分析来自 http://blog.csdn.net/lyy289065406/article/detai ...
- bzoj2794
这题我得到一个经验,bool型的dp一定要想办法把bool去掉来表示更多的东西(1933也是这个道理) 暴力大家都会,这里有两个限制条件 一个限制条件我们可以排序不断加入,另一个呢 我们可以用f[i] ...
- UVa 548 Tree【二叉树的递归遍历】
题意:给出一颗点带权的二叉树的中序和后序遍历,找一个叶子使得它到根的路径上的权和最小. 学习的紫书:先将这一棵二叉树建立出来,然后搜索一次找出这样的叶子结点 虽然紫书的思路很清晰= =可是理解起来好困 ...
- BZOJ2226: [Spoj 5971] LCMSum
题解: 考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和. 这是有公式的f[i]=phi[i]*i/2 然后卡一卡时就可以过了. 代码: #include<cstdio> # ...
- jquery 滚动条插件 jquery.nanoscroller.js
$(".listcontent .nano").nanoScroller(); $(".chatcontent .nano").nanoScroller({ ...
- UVA 350 Pseudo-Random Numbers 伪随机数(简单)
题意:给定Z, I, M, L,根据随机数产生式k=(Z*L+I)%M.但是L表示的是上一个产生的数,比如根据产生式产生了序列{2,5,4,3}那么5是由L=2算来的,4由L=5算来的..第1个所产 ...
- LeetCode: divideInteger
Title: Divide two integers without using multiplication, division and mod operator. If it is overflo ...
- MVC&WebForm对照学习:文件下载
说完了WebForm和MVC中的文件上传,就不得不说用户从服务器端下载资源了.那么今天就扯扯在WebForm和MVC中是如何实现文件下载的.说起WebForm中的文件上传,codeshark在他的博文 ...
- Python--类使用
类使用的几个注意点: 1. 类的语法结构:2. __init__(self),3. __metaclass__=type, (新式类)4. super(subclassname, self).__in ...