CF- Day at the Beach
2 seconds
256 megabytes
standard input
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.
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]

思路:
这个题目卡到第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的更多相关文章
- [cf 599C] Day at the Beach
题意:有n个数,将其分组使整个数列排序后每组中的数仍在该组中,求最多的分组数. 代码很易懂 #include <iostream> #include <algorithm> # ...
- 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 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- 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 ...
- 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 ...
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
- CF #375 (Div. 2) D. bfs
1.CF #375 (Div. 2) D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...
随机推荐
- MySQL批量更新死锁案例分析--转载
问题描述 在做项目的过程中,由于写SQL太过随意,一不小心就抛了一个死锁异常,如下: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackE ...
- Atlas mysql的读写分离和负载均衡<转>
mysql的读写分离和负载均衡 http://my.oschina.net/superbigfu/blog/178134
- 属性动画 LayoutTransition AnimatorInflater Keyframe 新特性
LayoutTransition设置动画 使用LayoutTransition可为布局的容器设置动画,当容器中的视图层次发生变化时产生相应的过渡的动画效果 过渡的类型一共有四种: LayoutTran ...
- Windows离线安装.NET3.X
Windows离线安装.NET3.X 当我们在Windows上安装软件的时候,总是会提示需要安装.NET3.X.而大多数人们选择在线安装,这样会很慢,不少人想到了离线安装的方式.其是只要你的电脑是Wi ...
- ASP.NET-FineUI开发实践-9
用了FineUI有一段时间了,还是分享下我咋改的吧,没想的那么难,我也是从小白来的. 基础是要懂JQ和EXTJS,主要是要懂JQ和EXTJS能干啥,这里有两个网站 http://www.w3schoo ...
- rmi rpc restful soa 区别
rmi rpc restful soa 区别 rmi vs rpc 参考文档:http://stackoverflow.com/questions/2728495/what-is-the-differ ...
- sharepoint2013小技巧
一.创建基于经典认证的应用程序 New-SPWebApplication -Name "Contoso Internet Site" -ApplicationPool " ...
- ubuntu学习笔记--不断更新中
1.rpm软件包相关: rpm软件包安装命令: rpm -ivh linuxqq-v1.0.2-beta1.i386.rpm rpm软件默认安装路径查询: rpm -ql *.rpm ubuntu如何 ...
- RecycleView 滑动到底部,加载更多
android.support.v7 包提供了一个新的组件:RecycleView,用以提供一个灵活的列表试图.显示大型数据集,它支持局部刷新.显示动画等功能,可以用来取代ListView与GridV ...
- 在浏览器中打不开Oracle 11gR2的企业管理器页面
最简单的办法,重建EM 四个步骤: emca -repos drop emca -repos create emca -config dbcontrol db emctl start dbconsol ...