P3434 [POI2006]KRA-The Disks
题目描述
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 nnn and mmm ( 1≤n,m≤300 0001\le n,m\le 300\ 0001≤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 nnn integers r1,r2,⋯,rnr_1,r_2,\cdots,r_nr1,r2,⋯,rn ( 1≤ri≤1 000 000 0001\le r_i\le 1\ 000\ 000\ 0001≤ri≤1 000 000 000 for 1≤i≤n1\le i\le n1≤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 mmm integers k1,k2,⋯,kmk_1,k_2,\cdots,k_mk1,k2,⋯,km ( 1≤kj≤1 000 000 0001\le k_j\le 1\ 000\ 000\ 0001≤kj≤1 000 000 000 for 1≤j≤m1\le j\le m1≤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 000 .
输入输出样例
7 3
5 6 4 3 6 2 3
3 2 5
2
Solution:
本题并不难。
显然每次一个物品从上往下掉,只会受到中途最小值的限制,于是我们直接用$f[i]$记录$1$到第$i$层之间的最小值。
然后每次放入一个物品,从最底层往上找,找到当前物品刚好不会被限制的层数,更新最底层的位置,每次都这样往上扫,若层数已经为$0$了就输出无解,否则输出最后的层数就好了。
代码:
#include<bits/stdc++.h> #define il inline #define ll long long #define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++) #define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--) using namespace std; const int N=300005; int n,m,f[N],x,ans; il int gi(){ int a=0;char x=getchar(); while(x<'0'||x>'9')x=getchar(); while(x>='0'&&x<='9')a=(a<<3)+(a<<1)+x-48,x=getchar(); return a; } int main(){ n=gi(),m=gi(); ans=n+1;f[0]=0x7fffffff; For(i,1,n) x=gi(),f[i]=min(f[i-1],x); while(m--){ x=gi(); while(ans--){ if(f[ans]<x)continue; break; } if(ans<=0)puts("0"),exit(0); } cout<<ans; return 0; }