博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3183 Stump Removal(我的水题之路——高峰烧火,在线判断)
阅读量:4068 次
发布时间:2019-05-25

本文共 2778 字,大约阅读时间需要 9 分钟。

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

9125433662

Sample Output

378

Source

有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 
#include
#include
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;}

转载地址:http://xloji.baihongyu.com/

你可能感兴趣的文章
大数相乘不能用自带大数类型
查看>>
字节跳动后端开发一面
查看>>
CentOS Tensorflow 基础环境配置
查看>>
centOS7安装FTP
查看>>
FTP的命令
查看>>
CentOS操作系统下安装yum的方法
查看>>
ping 报name or service not known
查看>>
FTP 常见问题
查看>>
zookeeper单机集群安装
查看>>
do_generic_file_read()函数
查看>>
Python学习笔记之数据类型
查看>>
Python学习笔记之特点
查看>>
Python学习笔记之安装
查看>>
shell 快捷键
查看>>
VIM滚屏操作
查看>>
EMC 2014存储布局及十大新技术要点
查看>>
linux内核内存管理(zone_dma zone_normal zone_highmem)
查看>>
将file文件内容转成字符串
查看>>
循环队列---数据结构和算法
查看>>
优先级队列-数据结构和算法
查看>>