博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[POI2006] KRA-The Disks (贪心)
阅读量:4569 次
发布时间:2019-06-08

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

题目描述

For his birthday present little Johnny has received from his parents a new plaything which consists of a tube and a set of disks. The aforementioned tube is of unusual shape. Namely, it is made of a certain number of cylinders (of equal height) with apertures of different diameters carved coaxially through them. The tube is closed at the bottom, open at the top. An exemplary tube consisting of cylinders whose apertures have the diameters: 5cm, 6cm, 4cm, 3cm, 6cm, 2cm and 3cm is presented in the image below.

The disks in Johnny's plaything are cylinders of different diameters and height equal to those forming the tube.

Johnny has invented a following game: having a certain set of disks at his disposal, he seeks to find what depth the last of them would stop at, assuming that they are being thrown into the centre of the tube. If, for instance, we were to throw disks of consecutive diameters: 3cm, 2cm and 5cm, we would obtain the following situation:

As you can see, upon being thrown in, every disk falls until it gets stuck (which means that it lies atop a cylinder, aperture of which has a diameter smaller than the diameter of the disk) or it is stopped by an obstacle: the bottom of the tube or another disk, which has already stopped.

The game being difficult, Johnny constantly asks his parents for help. As Johnny's parents do not like such intellectual games, they have asked you - an acquaintance of theirs and a programmer - to write a programme which will provide them with answers to Johnny's questions.

TaskWrite a programme which:

reads the description of the tube and the disks which Johnny will throw into it from the standard input,computes the depth which the last disk thrown by Johnny stops at,writes the outcome to the standard output.

一个框,告诉你每一层的宽度。

向下丢给定宽度的木块。

木块会停在卡住他的那一层之上,异或是已经存在的木块之上。

询问丢的最后一个木块停在第几层。

输入输出样例

输入样例#1:

7 3

5 6 4 3 6 2 3
3 2 5

输出样例#1:

2

Solution

要考虑到:

  • 每一个筒都只能使用一次
  • 若上面有一个较窄的入口,那么下方最多的能通过的就是该宽度。

所以我们可以预先处理出一个可以通过的宽度大小的数组 \(wide\)

其中: \(wide[i]=min(wide[i],wide[i-1])\)

然后可以发现这个序列是满足升序的.既可以 O(n)或者 O(nlogn) 求解。

代码

#include
using namespace std;const int maxn=300008;int n,m;int wide[maxn];int last;int main(){ freopen("kra.in","r",stdin); freopen("kra.out","w",stdout); scanf("%d%d",&n,&m); last=n+1; for(int i=1;i<=n;i++) scanf("%d",&wide[i]); for(int i=2;i<=n;i++) if(wide[i]>wide[i-1]) wide[i]=wide[i-1]; for(int i=1;i<=m;i++) { int x; scanf("%d",&x); int mid,l=1,r=last-1; while(l<=r) { mid=l+r>>1; if(wide[mid]>=x) {last=mid;l=mid+1;} else r=mid-1; } } if(n>m) cout<

转载于:https://www.cnblogs.com/Kv-Stalin/p/9313568.html

你可能感兴趣的文章
第三次冲刺(三)
查看>>
android实现静默安装demo
查看>>
数据缓存方案
查看>>
HDU 1086:You can Solve a Geometry Problem too
查看>>
HIPO图
查看>>
工作日志2014-06-30
查看>>
稀疏矩阵
查看>>
OpenCV2马拉松第14圈——边缘检測(Sobel,prewitt,roberts)
查看>>
移动端事件点透问题
查看>>
P1896 [SCOI2005]互不侵犯
查看>>
ESP定律手工脱壳步骤
查看>>
wex5 教程 之 图文讲解 登陆,注册,页面跳转
查看>>
问题7:JavaScript 常用正则示例
查看>>
xampp 虚拟机配置
查看>>
第五次实验
查看>>
从统计学角度来看深度学习(1):递归广义线性模型
查看>>
nginx发布Asp.net程序
查看>>
Spring Bean引用例子
查看>>
您访问的URL地址不被允许。
查看>>
docker 初探之简单安装 ----Windows10
查看>>