zoukankan      html  css  js  c++  java
  • Evanyou Blog 彩带

      题目传送门

    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 ( 1n,m300 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 ( 1ri1 000 000 001in ) 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 ( 1kj1 000 000 000 for 1jm ) 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 .

     

    输入输出样例

    输入样例#1:
    7 3
    5 6 4 3 6 2 3
    3 2 5
    输出样例#1:

    2


      分析:

      今天考试遇到的题,实际上还是比较水的。(洛谷上的标签居然还是蓝题。。。orz)

      只需要模拟就可以了。可以得知到,如果上面有一根较窄的管子,那么下面比它宽的管子实际上就都并没有什么用处,所以可以从第一根管子开始将后面的管子宽度调整,然后每放一个盘子下去就从后往前遍历把它放在第一根比它宽的管子处,记该处为pos,然后可以直接把n变成pos-1,进行优化(显而易见的,盘子卡在那个地方后下面的管子都不管用了)。复杂度比较玄学,但是很显然是没有问题的,近似于O(n)。

      Code:

    #include<bits/stdc++.h>
    using namespace std;
    const int N=3e5+7;
    int n,m,r[N],a[N];
    inline int read()
    {
      char ch=getchar();int num=0;bool flag=false;
      while(ch<'0'||ch>'9'){if(ch=='-')flag=true;ch=getchar();}
      while(ch>='0'&&ch<='9'){num=num*10+ch-'0';ch=getchar();}
      return flag?-num:num;
    }
    inline int find(int x)
    {
      for(int i=n;i>=1;i--){
        if(r[i]>=x)return i;}
      return 0;
    }
    int main()
    {
      n=read();m=read();int pos;
      for(int i=1;i<=n;i++)r[i]=read();
      for(int i=1;i<=m;i++)a[i]=read();
      for(int i=2;i<=n;i++){
        if(r[i]>r[i-1])r[i]=r[i-1];}
      for(int i=1;i<=m;i++){
        pos=find(a[i]);
        if(pos==0){printf("0");return 0;}
        else n=pos-1;}
      printf("%d
    ",pos);
      return 0;
    }
  • 相关阅读:
    Shell学习笔记 ——第一天
    Myclipse 安装 Maven遇见的N个异常
    Myeclipse 创建 Web Maven项目
    Guava API
    String 转Map(基于Guava类库)
    Mybatis——helloWorld级程序
    redis
    listener、context、filter、servlet及其加载顺序
    junit 单元测试
    hibernate —— 树状存储
  • 原文地址:https://www.cnblogs.com/cytus/p/9313491.html
Copyright © 2011-2022 走看看