zoukankan      html  css  js  c++  java
  • 洛谷 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 nn and mm (1le n,mle 300 0001n,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 integers r_1,r_2,cdots,r_nr1​​,r2​​,,rn​​ (1le r_ile 1 000 000 0001ri​​1 000 000 000 for 1le ile n1in) 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 integers k_1,k_2,cdots,k_mk1​​,k2​​,,km​​ (1le k_jle 1 000 000 0001kj​​1 000 000 000 for 1le jle m1jm) 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 00.

     

    输入输出样例

    输入样例#1:
    7 3
    5 6 4 3 6 2 3
    3 2 5
    输出样例#1:
    2
    思路:先预处理出通洞上方最窄的地方,然后模拟。
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define MAXN 300100
    using namespace std;
    int n,m,deep;
    int val[MAXN],bns[MAXN],cost[MAXN];
    int main(){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&val[i]);
        for(int i=1;i<=m;i++)
            scanf("%d",&cost[i]);
        bns[1]=val[1];
        for(int i=2;i<=n;i++)
            if(bns[i-1]<val[i])    bns[i]=bns[i-1];
            else bns[i]=val[i];
        deep=n;
        for(int i=1;i<=m;i++){
            while(bns[deep]<cost[i])    deep--;
            deep--;
            if(deep==0){
                cout<<"0";
                return 0;
            }
        }
        cout<<deep+1;
    }
     
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    算法Sedgewick第四版-第1章基础-014一用stack把前置表达式转为后置表达式并计算值
    算法Sedgewick第四版-第1章基础-013一用stack实现自动补全表达式括号
    算法Sedgewick第四版-第1章基础-012一用stack实现输出一个数的二进制形式
    算法Sedgewick第四版-第1章基础-011一用链表实现bag、queue、stack
    算法Sedgewick第四版-第1章基础-010一检查括号是否成对出现
    算法Sedgewick第四版-第1章基础-009一链表与数组的比较及其他数据结构
    算法Sedgewick第四版-第1章基础-008一用数组实现栈(泛型、可变大小)
    算法Sedgewick第四版-第1章基础-007一用两个栈实现简单的编译器
    webApi2 上传大文件代码
    IE8 AJAX 不能正常工作 解决办法
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/7655793.html
Copyright © 2011-2022 走看看