码迷,mamicode.com
首页 > 其他好文 > 详细

[Codeforces599C] Day at the Beach (贪心)

时间:2015-11-24 12:34:43      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

  • 题目概述:

  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 tohi. 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:

    1. 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.
    2. The partitioning is chosen in such a way that every castle is a part of exactly one block.
    3. Each block is sorted independently from other blocks, that is the sequence hi, hi + 1, ..., hj becomes sorted.
    4. 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.

  有一些沙子堆的城堡,现让你给这些城堡分成若干组,使得每一组的城堡是连续的,每一个城堡都必须在一个组里,且对每一组升序排序后,所有的沙堡都排好了序,问最多可分几组?

  • 输入格式:

  第一行一个整数n(1 ≤ n ≤ 100 000),表示城堡的数目;

  第二行包括n个整数h1, h2, ..., hn(1 ≤ hi ≤ 10^9),表示每一个城堡的高度。

  • 输出格式:

  一个整数,表示最多可分的组数。

  • 样例输入1:

  3

  1 2 3

  • 样例输出1:

  3

  • 样例输入2:

  4

  2 1 3 2

  • 样例输出2:

  2

  • 思路:

  比赛时我是这么想的:

  如果[i, j]可以被分为一组,那么将原序列排好序后,[i, j]上每一个数的个数应和未排序时[i, j]上每一个数的个数相同,例如样例2:

    未排序:2 1 3 2

    已排序:1 2 2 3

  在两个序列的[1, 2]中1均出现1次,2均出现1次,在[3, 4]中2均出现1次,3均出现1次。

  所以设一个bool类型的数组,用来存某一个数字在两个序列中出现次数是否相同。

  然后,然后我就傻傻地写了个线段树:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int MAXN = 100005;
 4 int a[MAXN], b[MAXN], c[MAXN], x;
 5 bool seg[MAXN * 3];
 6 map<int, int> Map;
 7 
 8 void update(int o, int l, int r) {
 9     int mid = l + r >> 1;
10     if(l == r) {
11         seg[o] = !!c[l]; return;
12     }
13     if(x <= mid) update(o << 1, l, mid);
14     else update(o << 1 | 1, mid + 1, r);
15     seg[o] = seg[o << 1] | seg[o << 1 | 1];
16 }
17 
18 int main() {
19     int n, ans = 0;
20     scanf("%d", &n);
21     for(int i = 1; i <= n; i++) {
22         scanf("%d", a + i);
23         b[i] = a[i];
24     }
25     sort(b + 1, b + n + 1);
26     for(int i = 1; i <= n; i++)
27         Map[b[i]] = i;
28     for(int i = 1; i <= n; i++) {
29         a[i] = Map[a[i]]; b[i] = Map[b[i]];
30     }
31     for(int i = 1; i <= n; i++) {
32         c[a[i]]++; c[b[i]]--;
33         x = a[i]; update(1, 1, n);
34         x = b[i]; update(1, 1, n);
35         if(!seg[1]) ans++;
36     }
37     printf("%d\n", ans);
38     return 0;
39 }

  考完后舔了舔题解,才发现自己是多傻:

  因为最终结果是升序,所以对于一个i,只要min(hi+1, hi+2, ..., hn) ≤ max(h1, h2, ..., hi),那么i以及之前没有分组的数可分为一组:  

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int MAXN = 100005;
 4 int a[MAXN], mina[MAXN], maxa[MAXN], x;
 5 
 6 int main() {
 7     int n, ans = 0;
 8     scanf("%d", &n);
 9     for(int i = 1; i <= n; i++)
10         scanf("%d", a + i);
11     maxa[1] = a[1]; mina[n] = a[n];
12     for(int i = 2; i <= n; i++)
13         maxa[i] = max(maxa[i - 1], a[i]);
14     for(int i = n - 1; i; i--)
15         mina[i] = min(mina[i + 1], a[i]);
16     for(int i = 1; i <= n; i++)
17         if(maxa[i] <= mina[i + 1]) ans++;
18     printf("%d\n", ans + 1);
19     return 0;
20 }

[Codeforces599C] Day at the Beach (贪心)

标签:

原文地址:http://www.cnblogs.com/CtrlCV/p/4991156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!