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 of 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
题意:
输入n个数,代表高度,将其分成连续的块,块不交叉,对这些块里的数排序后这n个数也完成了排序。求最大的块数。
思路:
算法1:关注高度的高低顺序和高度的输入顺序,用pair将二者关联起来。按低到高排序。遍历记录当前的最大输入顺序,后面的高度总大于前面的高度。如果当前输入顺序n1等于当前最大输入顺序,说明前n1个输入的刚好可以从低到高排序,组成一个block,ans++。继续往后找输入顺序n2等于当前最大输入顺序n2的时候,说明n1到n2个输入的数刚好组成从低到高的排序,ans++。以此类推。
代码如下:
//1) 31MS
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#define MAXN 100000+5
using namespace std;
int h[MAXN],n;
pair <int,int> p[MAXN];
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
while(scanf("%d",&n)==1){
for(int i=0;i<n;i++){
scanf("%d",&h[i]);
p[i].first=h[i];
p[i].second=i;
}
sort(p,p+n);
int tmpMax=0,ans=0;
for(int i=0;i<n;i++){
if(p[i].second>tmpMax) tmpMax=p[i].second;
if(i==tmpMax) ans++;
}
cout<<ans<<endl;
}
return 0;
}
对输入的高度按低到高排序,存在sorted[]里。记录原始高度的输入顺序,记录在h[]里。
分别累计sorted 和 h[]的前i项和t2,t1。每当t1=t2时,即sorted的前i项跟h的前i项元素相同,虽然不一定一一对应,ans++。
反证法证明该方法正确。
假设t1=t2, 即sorted的前i项跟h的前i项元素不相同。在h[]的前i项中有a,b, sorted前i项中有c,d。a+b=c+d, 但是a,b 不在sorted前i项中,c, d 不在h前i项中。不妨设a
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n, h[200000],sorted[200000];
int main() {
while(scanf("%d", &n)==1){
for(int i = 0; i < n; ++i) {
scanf("%d", &h[i]);
sorted[i] = h[i];
}
sort(sorted, sorted + n);
int t1 = 0, t2 = 0;
int res = 0;
for(int i = 0; i < n; ++i) {
t1 += h[i]; t2 += sorted[i];
if( t1 == t2 ) {
res += 1;
}
}
cout << res << endl;
}
return 0;
}
//mn[i]:输入顺序大于等于i的所有高度中最小的
//mx到遍历到输入顺序为i的高度时,所有中高度中最大的
所以如果m[i]>=mx, 前i个一定可以组成一个block,ans++
3)234ms
#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
int n, mx, h[100001], mn[100001];
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
while(scanf("%d",&n)==1){
for(int i = 0; i < n; ++i){
cin >> h[i];
}
mn[n - 1] = h[n -1];
for(int i = n - 2; i >= 0; --i)
mn[i] = min(h[i], mn[i + 1]);
int mx = h[0], otv = 1;
for(int i = 1; i < n; ++i)
{
if(mn[i] >= mx)
otv++;
mx = max(h[i], mx);
}
cout << otv<<endl;
}
return 0;
}