CF- Day at the Beach

时间:2023-03-09 08:27:15
CF- Day at the Beach
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].

CF- Day at the Beach

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

CF- Day at the Beach


思路:
这个题目卡到第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 ;
}