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个湖,总要填掉多 ...
随机推荐
- PullToRefresh下拉刷新 加载更多 详解 +示例
常用设置 项目地址:https://github.com/chrisbanes/Android-PullToRefresh a. 设置刷新模式 如果Mode设置成Mode.PULL_FROM_STAR ...
- javascript高级特性(面向对象)
javascript高级特性(面向对象): * 面向对象: * 面向对象和面向过程的区别: * 面向对象:人就是对象,年龄\性别就是属性,出生\上学\结婚就是方法. * 面向过程:人出生.上学.工作. ...
- spring配置文件位置
参考http://name327.iteye.com/blog/1628884
- spring通过注解依赖注入和获取xml配置混合的方式
spring的xml配置文件中某个<bean></bean>中的property的用法是什么样的? /spring-beans/src/test/java/org/spring ...
- Dragger代码实现
转自:http://www.apkbus.com/blog-705730-60436.html 在工程中引入Dagger 如果想使用Dagger的话,需要添加两个函数库: dependencies { ...
- android 广播分类
安卓广播分为两类:1.普通广播, broadcast,广播发出之后所有满足条件的应用都能获取到广播里面的数据,缺点是应用获取广播中的数据修改之后不能传递给其它接收广播的应用:2.有序广播,orderb ...
- DNS负载均衡
1)DNS负载均衡的介绍 对于负载均衡的一个典型应用就是DNS负载均衡.庞大的网络地址和网络域名绝对是负载均衡体现优势的地方.那么它的具体原理是如何的呢?本文就将为大家详细介绍一下相关内容. DNS负 ...
- PHP E-mail
PHP E-mail 注入 首先,请看上一章中的 PHP 代码: <html><body> <?phpif (isset($_REQUEST['email']))//if ...
- 利用Format函数格式化时间和日期
在做机房收费系统的时候,因为需要使用到日期进行查询,所以在数据表中没有使用自动添加日期的功能,而是采用了自定义的格式插入.但由于事先没有对时间转换的格式进行统一,导致后面查询时出现的问题不断. 插入时 ...
- ubuntu11.10(TQ210)下移植boa服务器
平台:ubuntu11.10 一.下载源码包www.boa.org boa-0.94.13.tar.gz 二.解压,在其src目录下生产makefile #tar xvfz boa-0.94.1 ...