POJ 3183 Stump Removal(我的水题之路——高峰烧火,在线判断)

Stump Removal
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2379   Accepted: 1282

Description

Always thinking of the cows' grazing experience, FJ has found that he must remove N (1 <= N <= 50,000) unsightly stumps from the pasture. The stumps are conveniently arranged in a straight line and numbered 1..N with each stump having some height H_i (1 <= H_i <= 10,000). 

FJ will use the traditional high explosives to destroy the stumps. These high explosives are formulated to destroy adjacent stumps as long as those adjacent stumps are strictly shorter than the nearest stump being destroyed. The blast can continue past the closest adjacent stump to the next adjacent stump if it is even shorter than the nearest stump just destroyed. As soon as a stump encountered by the blast wave is not shorter, though, no more destruction occurs on that side of the target stump (the other side follows the same rules with whatever stumps might appear there). 

Consider a line of nine stumps with these heights: 

              1 2 5 4 3 3 6 6 2
If FJ blows up the third stump (with height 5), then the second stump will also be destroyed (height 2) and the first stump (height 1) will also be destroyed. Likewise, the fourth stump (height 4) and fifth stump (height 3) will be destroyed since they are successively shorter, leaving the line like this: 

              * * * * * 3 6 6 2
Two more explosives (at stumps 7 and 8) will destroy the rest. 

Help FJ determine the minimum number of explosive charges he needs to destroy the stumps.

Input

Line 1: A single integer, N 

Lines 2..N+1: Line i+1 contains H_i

Output

Lines 1..?: Each line contains one integer which is the index of a stump to blow up. The indices must be listed in increasing order.

Sample Input

9
1
2
5
4
3
3
6
6
2

Sample Output

3
7
8

Source

USACO 2006 January Bronze

有N个草堆高度分别是Hi,现在要安装炸药,如果在某一个草堆高度为H0放炸药,则两边比它矮的草堆(高度分别为H1,H2<H0)也会销毁,同时,如果H1旁边的草堆H3<H1,则H3草堆也会同时销毁,之后的草堆也符合这种关系,比如:
1 2 5 4 3 3 6 6 2
第三个草堆点炸药,则1<2<5>4>3==3<6==6>2.
所以在#3(5)点可以销毁:#1~#5(1 2 5 4 3)
        在#7(6)点可以销毁:#6~#7(3 6)
        在#8(6)点可以销毁:#8~#9(6 2),这样就可以全部销毁。

分析:
可以发现,点火的地方都是属于草堆高峰的地方,要>=两边即可。

所以我们只要将所有的数据存储下来,然后找到巅峰处就可以输出。
而我采用了一个在线判断的方法,每次保存三个last、now、next(当前次从键盘读入的数),
如果last<=now && now <= next,则输出now的下标。
直到所有数据输入完毕,再判断最后一个数是否为草堆高峰:last<=now,如果成立,则输出now的下标。

注意点:
1)如果三个数字连续如:3 3 3,要输出中间那个数的下标。
2)注意末尾可能也是高峰的情况。
3)注意一开头就是高峰的情况。

代码(1AC):
#include <cstdio>
#include <cstdlib>
#include <cstring>

int main(void){
    int n;
    int i, j;
    int flag;
    int now, last, next;

    scanf("%d", &n);
        last = -1, now = 0;
        for (i = 1; i <= n; i++){
            scanf("%d", &next);
            if (last <= now && now >= next){
                printf("%d\n", i - 1);
            }
            last = now;
            now = next;
        }
        if (now >= last){
            printf("%d\n", i - 1);
        }
    return 0;
}


你可能感兴趣的:(Integer,存储,input,UP,each,output)