POJ 3183 Stump Removal(我的水题之路——高峰烧火,在线判断)
来源:互联网 发布:网络教育文凭毕业证 编辑:程序博客网 时间:2024/05/22 00:50
Stump Removal
Time Limit: 1000MS Memory Limit: 65536KTotal 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:
Help FJ determine the minimum number of explosive charges he needs to destroy the stumps.
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 2If 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 2Two 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
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
9125433662
Sample Output
378
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;}
- POJ 3183 Stump Removal(我的水题之路——高峰烧火,在线判断)
- POJ 3183 Stump Removal(水~)
- POJ 3183 Stump Removal(简单题)
- POJ 3183 Stump Removal
- poj 3183 Stump Removal
- POJ 3183 Stump Removal
- POJ 3183 Stump Removal G++
- POJ 3302 Subsequence(我的水题之路——子串判断)
- 机器学习实战(五)——基于单层决策树(dicision-stump)的adaBoosting
- 机器学习基础(十八) —— decision stump
- POJ 2636 Electrical Outlets(我的水题之路——水,电器接头)
- POJ 2840 Big Clock(我的水题之路——水,钟)
- POJ 2459 Feed Accounting(我的水题之路——英文题啊!!!)
- POJ 2535 Very Simple Problem(我的水题之路——看错题)
- POJ 2665 Trees(我的水题之路——移树,POJ100题啦!)
- POJ 1118 Lining Up(我的水题之路——斜率最多)
- POJ 1248 Safecracker(我的水题之路——五重循环暴力- -!)
- POJ 1250 Tanning Salon(我的水题之路——字母表数组标记)
- ffmpeg: the mother of all command lines
- Qt开发应用程序Debug与Release设置
- 思维方式的重要性
- java 不得不知道
- Mac下的git配置
- POJ 3183 Stump Removal(我的水题之路——高峰烧火,在线判断)
- 颇有意义的面试对话
- C++程序的编译过程及g++与之对应的几个参数
- 算法学习心得——动态规划法实现最长公共子序列(LCS)
- ActiveMQ——如何监控ActiveMQ
- 分析opencv的图像输入\出基本框架
- js 跟php服务端交互 <script language=\"javascript\" src=\"test.php?step=2&rnd={$timestamp}
- 调试介绍
- ActiveMQ——XML配置文件