Codeforces Round #332 (Div. 2) C. 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 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;
}
發佈了77 篇原創文章 · 獲贊 22 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章