洛谷P3434 [POI2006]KRA-The Disks [模拟]
KRA
题目描述
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.
一个框,告诉你每一层的宽度。
向下丢给定宽度的木块。
木块会停在卡住他的那一层之上,异或是已经存在的木块之上。
询问丢的最后一个木块停在第几层。
输入输出格式
输入格式:
The first line of the standard input contains two integers n and m ( 1≤n,m≤300 000 ) separated by a single space and denoting the height of Johnny's tube (the number of cylinders it comprises) and the number of disks Johnny intends to throw into it, respectively. The second line of the standard input contains nn integersr1,r2,⋯,rn ( 1≤ri≤1 000 000 000 1≤i≤n ) separated by single spaces and denoting the diameters of the apertures carved through the consecutive cylinders (in top-down order), which the tube consists of. The third line contains mm integersk1,k2,⋯,km ( 1≤kj≤1 000 000 000 for 1≤j≤m ) separated by single spaces and denoting the diameters of consecutive disks which Johnny intends to throw into the tube.
输出格式:
The first and only line of the standard output should contain a single integer denoting the depth which the last disk stops at. Should the disk not fall into the tube at all, the answer should be 0 .
输入输出样例
7 3
5 6 4 3 6 2 3
3 2 5
2
分析:
今天考试遇到的题,实际上还是比较水的。(洛谷上的标签居然还是蓝题。。。orz)
只需要模拟就可以了。可以得知到,如果上面有一根较窄的管子,那么下面比它宽的管子实际上就都并没有什么用处,所以可以从第一根管子开始将后面的管子宽度调整,然后每放一个盘子下去就从后往前遍历把它放在第一根比它宽的管子处,记该处为pos,然后可以直接把n变成pos-1,进行优化(显而易见的,盘子卡在那个地方后下面的管子都不管用了)。复杂度比较玄学,但是很显然是没有问题的,近似于O(n)。
Code:
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+;
int n,m,r[N],a[N];
inline int read()
{
char ch=getchar();int num=;bool flag=false;
while(ch<''||ch>''){if(ch=='-')flag=true;ch=getchar();}
while(ch>=''&&ch<=''){num=num*+ch-'';ch=getchar();}
return flag?-num:num;
}
inline int find(int x)
{
for(int i=n;i>=;i--){
if(r[i]>=x)return i;}
return ;
}
int main()
{
n=read();m=read();int pos;
for(int i=;i<=n;i++)r[i]=read();
for(int i=;i<=m;i++)a[i]=read();
for(int i=;i<=n;i++){
if(r[i]>r[i-])r[i]=r[i-];}
for(int i=;i<=m;i++){
pos=find(a[i]);
if(pos==){printf("");return ;}
else n=pos-;}
printf("%d\n",pos);
return ;
}
洛谷P3434 [POI2006]KRA-The Disks [模拟]的更多相关文章
- 洛谷P3434 [POI2006]KRA-The Disks
P3434 [POI2006]KRA-The Disks 题目描述 For his birthday present little Johnny has received from his paren ...
- 洛谷 P3434 [POI2006]KRA-The Disks
P3434 [POI2006]KRA-The Disks 题目描述 For his birthday present little Johnny has received from his paren ...
- 洛谷 P3434 [POI2006]KRA-The Disks 贪心
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输出样例 输出样例 说明 思路 AC代码 题面 题目链接 P3434 [POI2006]KRA-The Disks 题目 ...
- 洛谷P3434 [POI2006]KRA-The Disks(线段树)
洛谷题目传送门 \(O(n)\)的正解算法对我这个小蒟蒻真的还有点思维难度.洛谷题解里都讲得很好. 考试的时候一看到300000就直接去想各种带log的做法了,反正不怕T...... 我永远只会有最直 ...
- [洛谷P3444] [POI2006]ORK-Ploughing
洛谷题目链接[POI2006]ORK-Ploughing 题目描述 Byteasar, the farmer, wants to plough his rectangular field. He ca ...
- 洛谷 P3695 CYaRon!语 题解 【模拟】【字符串】
大模拟好啊! 万一远古计算机让我写个解释器还真是得爆零了呢. 题目背景 「千歌です」(我是千歌).「曜です」(我是曜).「ルビィです」(我是露比).「3人合わせて.We are CYaRon! よろし ...
- 洛谷P3435 [POI2006]OKR-Period of Words [KMP]
洛谷传送门,BZOJ传送门 OKR-Period of Words Description 一个串是有限个小写字符的序列,特别的,一个空序列也可以是一个串. 一个串P是串A的前缀, 当且仅当存在串B, ...
- 【题解】洛谷P1065 [NOIP2006TG] 作业调度方案(模拟+阅读理解)
次元传送门:洛谷P1065 思路 简单讲一下用到的数组含义 work 第i个工件已经做了几道工序 num 第i个工序的安排顺序 finnish 第i个工件每道工序的结束时间 need 第i个工件第j道 ...
- 【题解】洛谷P3435 [POI2006] OKR-Periods of Words(KMP)
洛谷P3435:https://www.luogu.org/problemnew/show/P3435 思路 来自Kamijoulndex大佬的解释 先把题面转成人话: 对于给定串的每个前缀i,求最长 ...
随机推荐
- SQL SERVER2008不在同一局域网内实现订阅发布的方法
我们知道,在新建订阅的时候,必须要使用实际服务器的真实名称才可以,IP地址,别名都不可以,在同一个局域网内是没有问题的.但是两台服务器不在同一个局域网内就不行了 要想实现订阅和发布,本人用到的最简单的 ...
- java学习——equals()和==的比较
equals 方法是 java.lang.Object 类的方法. 下面从两个方面来说明equals()和==的差别:(1)对于字符串变量来说,使用“==”和“equals()”方法比较字符串时,其比 ...
- python初步学习-面向对象之类(一)
python 面向对象 python 从设计之初就已经是一门面向对象的语言,正因为如此,在python中创建一个类和对象是很容易的. 对象对象奇数简介 类(Class): 用于描述具有相同的属性和方法 ...
- typeof运算符
javascript中typeof用来判断一个变量或表达式的数据类型. typeof 返回值有六种可能: "number," "string," "b ...
- Go语言 6 结构体、方法和接口
文章由作者马志国在博客园的原创,若转载请于明显处标记出处:http://www.cnblogs.com/mazg/ Go学习群:415660935 结构体(struct)是由一系列具有相同类型或不同类 ...
- struts入门
1.概念
- thinkphp中的验证器
- LCD常用接口原理【转】
转自:http://blog.csdn.net/wocao1226/article/details/23870149 LCD常用接口原理 点击打开链接 点击打开链接 点击打开链接 点击打开链接 点击打 ...
- S3C6410 SPI全双工读写流程分析(原创)【转】
转自:http://blog.csdn.net/hustyangju/article/details/21165721 原创博文,知识共享!转载请注明出处:http://blog.csdn.net/h ...
- Linux kernel kfifo分析【转】
转自:https://zohead.com/archives/linux-kernel-kfifo/ 本文同步自(如浏览不正常请点击跳转):https://zohead.com/archives/li ...