C. Day at the Beach
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 to hi. 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 mf 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 
Note

In the first sample the partitioning looks like that: [1][2][3].

In the second sample the partitioning is: [2, 1][3, 2]


思路:
这个题目卡到第12组数据TLE了,不过我的算法肯定是正确的
说一下这题的一点收获:
将一组数重现排序,获得一组新的数列,然后可以得到原数字在新数字中的向量,但是这里有一点需要注意,每个点对应的新点都是唯一确定的,这中唯一性是容易出错的地方
可以用一个vis数组来标记一下,然后找具体的点就是距离自己最近的
最后数据的分块用的是并查集

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#define INF 0x7fffffff
using namespace std; struct N{
int val;//存储数据
int pos;//初始位置
int exp;//排序位置
int dir;//移动方向(1是向右,-1是向左)
int vis;//是否被访问
int dis;//移动位置
}num[];
__int64 sa[];
__int64 father[];
__int64 s[];
int n; void set_init()
{
for(int i = ;i <= n;i++){
father[i] = i;
s[i] = ;
}
} int main()
{
while(cin>>n)
{
for(int i = ;i <= n;i++) {
cin>>num[i].val;
num[i].dir = ;
num[i].dis = INF;
num[i].exp = i;
num[i].pos = i;
num[i].vis = ;
sa[i] = num[i].val;
}
sort(sa+,sa++n);
for(int i = ;i <= n;i++) {
int final;
for(int j = ;j <= n;j++)
//如果在排序好后的数组中找到了自己新pos
//并且所找到点到自己现在的距离是最短的
if(num[i].val == sa[j] && abs(num[i].pos-j) < num[i].dis && !num[j].vis) {
//记录向量的方向
if(num[i].pos > j)
num[i].dir = -;
else if(num[i].pos < j)
num[i].dir = ;
else num[i].dir = ;
//记录新的位置
num[i].exp = j;
//记录平移的距离
num[i].dis = abs(num[i].pos-j);
final = j;
}
num[final].vis = ;
}
//剩下的问题就变成了并查集
int ans = ;
set_init();
for(int i = ;i <= n;i++)
{
//原地不动
if(num[i].dir == ) continue;
//向右移动
else if(num[i].dir = ) {
for(int j = i+;j <= num[i].exp;j++) {
int x,y;
for(x = i;x != father[x];x = father[x])
father[x] = father[father[x]];
for(y = j;y != father[y];y = father[y])
father[y] = father[father[y]];
if(x == y) continue;
else {
if(s[i] < s[j]) {
father[i] = j;
s[i] += s[j];
}
else {
father[j] = i;
s[j] += s[i];
}
}
}
}
//向左移动
else {
for(int j = i-;j >= num[i].exp;j--) {
int x,y;
for(x = i;x != father[x];x = father[x])
father[x] = father[father[x]];
for(y = j;y != father[y];y = father[y])
father[y] = father[father[y]];
if(x == y) continue;
else {
if(s[i] < s[j]) {
father[i] = j;
s[i] += s[j];
}
else {
father[j] = i;
s[j] += s[i];
}
}
}
}
}
for(int i = ;i <= n;i++)
if(i == father[i])
ans++;
cout<<ans<<endl;
}
return ;
}
 

CF- Day at the Beach的更多相关文章

  1. [cf 599C] Day at the Beach

    题意:有n个数,将其分组使整个数列排序后每组中的数仍在该组中,求最多的分组数. 代码很易懂 #include <iostream> #include <algorithm> # ...

  2. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  3. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  4. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  5. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  6. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  7. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  8. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

  9. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

  10. CF #375 (Div. 2) D. bfs

    1.CF #375 (Div. 2)  D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...

随机推荐

  1. myeclipse 于 否update software 解

    In some situations you may not be able to install or update software using the menu commands in the  ...

  2. 【代码优化】equals深入理解

    覆盖equals时,遵守通用约定 对equal方法的覆盖看起来非常easy,可是有很多情况是容易导致错误,最好的避免这些错误的办法 就是不覆盖equals方法. 必须遵循的原则: 自反性--对于不论什 ...

  3. 字符串格式化 String.format() 案例

    转换符 转换符      说    明 %s            字符串类型 %c            字符类型 %b            布尔类型 %d            整数类型(十进制 ...

  4. SpringMVC09异常处理和类型转化器

    public class User { private String name; private Integer age; public String getName() { return name; ...

  5. 获取web路径的几种方式

    1.string str1 = Request.ApplicationPath.ToString();           返回路径为:\HolterClientWeb 2.HttpServerUti ...

  6. CSS和CSS3中的伪元素和伪类(总结)

    好多人伪类和伪元素分清楚,其实就是一句话,“伪类的效果可以通过添加一个实际的类来达到,而伪元素的效果则需要通过添加一个实际的元素才能达到”. CSS中伪类包括: :first-child :lang ...

  7. CSS3 target 伪类不得不说那些事儿(纯CSS实现tab切换)

    是不是觉得target有点眼熟?! 今天要讲的不是HTML的<a>标签里面有个target属性. target伪类是css3的新属性. 说到伪类,对css属性的人肯定都知道:hover.: ...

  8. MeasureSpec学习

    在自定义View和ViewGroup的时候,我们经常会遇到int型的MeasureSpec来表示一个组件的大小,这个变量里面不仅有组件的尺寸大小,还有大小的模式. 这个大小的模式,有点难以理解.在系统 ...

  9. ORACLE安装过程中检查步骤出现的错误和解决方法【转】

    Checking operating system requirements ...Expected result: One of redhat-3,redhat-4,SuSE-9,asianux-1 ...

  10. Objective-C学习篇10—NSDate与NSDateFormatter

    NSDate NSDate 时间类,继承自NSObject,其对象表示一个时间点 NSDate *date = [NSDate date]; NSLog(@"date = %@", ...