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. 精益创业之父Steve Blank: 怎样让企业内部创新获得50倍增速

    编者注:本文英文版来自创新大师Steve Blank的个人博客,中文版由天地会珠海分舵进行编译.应用在初创企业打造上面的精益创业相信我们已经耳熟能详,可是假设我们面对的是一个已经发展起来的企业.或者是 ...

  2. 给Android程序猿的六个建议

    假设你一年前写的代码 , 在如今看来你还感觉写的非常不错 , 那么说明你学习的不够多. 不要在Context中持有静态引用 public class MainActivity extends Loca ...

  3. TinyXml快速入门(二)

    在<TinyXml快速入门(一)>中我介绍了使用TinyXml库如何创建和打印xml文件,下面我介绍使用tinyxml库对xml文件进行一系列的操作,包括获取xml文件声明,查询指定节点. ...

  4. 关于CSS选择器的效率问题

    最近一段时间接触CSS比较多,所以从网上找了写资料,这里做下总结. 以下是CSS选择器的效率排名: id选择器(#myid) 类选择器(.myclassname) 标签选择器(div,h1,p) 相邻 ...

  5. 【转】AFNetworking之于https认证

    转自:http://www.cocoachina.com/ios/20161220/18393.html 写在开头: 本来这篇内容准备写在AFNetworking到底做了什么?(三)中的,但是因为我想 ...

  6. 各版本IIS安装方法

      各版本IIS安装方法 Windows 2000 V5.0 将操作系统安装光盘放入光驱,打开“控制面板”→“添加或删除程序”→“添加/删除 Windows 组件”,勾选“Internet信息服务(I ...

  7. mysql的distinct理解

    select distinct id,name from route where update_time>=''; 上面的sql语句的逻辑是两条记录的id,name只要有一个不一样,就算不一样. ...

  8. java克隆总结

    对象clone,注意基本类型和指针类型.

  9. Asp服务器控件(HyperLink、Button) 绑定后台参数 DataBinder.Eval

    HyperLink动态绑定参数 <asp:HyperLink id="MbCenterHLnk" runat="server" Text='会员中心' T ...

  10. Visual Studio使用技巧记录

    1.关闭调试,iis express仍显示在托盘中: 工具 ---> 选项 ---> 调试 ---> 编辑并继续,取消选择“编辑并继续”的选择框 2.关闭浏览器一直请求: 在调试旁边 ...