zoukankan      html  css  js  c++  java
  • Codeforces Round #218 (Div. 2) D. Vessels

    D. Vessels
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ailiters.

    Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.

    Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries:

    1. Add xi liters of water to the pi-th vessel;
    2. Print the number of liters of water in the ki-th vessel.

    When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.

    Input

    The first line contains integer n — the number of vessels (1 ≤ n ≤ 2·105). The second line contains n integersa1, a2, ..., an — the vessels' capacities (1 ≤ ai ≤ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m — the number of queries (1 ≤ m ≤ 2·105). Each of the next m lines contains the description of one query. The query of the first type is represented as "pi xi", the query of the second type is represented as "ki" (1 ≤ pi ≤ n1 ≤ xi ≤ 1091 ≤ ki ≤ n).

    Output

    For each query, print on a single line the number of liters of water in the corresponding vessel.

    Sample test(s)
    input
    2
    5 10
    6
    1 1 4
    2 1
    1 2 5
    1 1 4
    2 1
    2 2
    output
    4
    5
    8
    input
    3
    5 10 8
    6
    1 1 12
    2 2
    1 1 6
    1 3 2
    2 2
    2 3
    output
    7
    10
    5

      这个题的做法应该有很多。

      这种写法有点像并查集。

       其实也就是模拟的思想,把已经满了的节点delete,不断的调整后继节点。每个点大概访问2次左右,跑了140ms。

    #include <iostream>
    #include <string>
    #include <string.h>
    #include <map>
    #include <stdio.h>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <math.h>
    #include <set>
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std ;
    typedef long long LL ;
    const int Max_N = 200008 ;
    int  Right[Max_N] ;
    int  now[Max_N] ;
    int  limit[Max_N] ;
    int  N ,M ;
    
    int find_right(int id,int x){
         if(id != -1){
            int leave = limit[id] - now[id] ;
            if(leave >= x){
                now[id] += x ;
                return id ;
            }
            else{
                now[id] = limit[id] ;
                return Right[id] = find_right(Right[id],x - leave) ;
            }
         }
         else
            return -1 ;
    }
    
    int main(){
        int kind ,id ,x ;
        while(scanf("%d",&N)!=EOF){
             for(int i = 1 ; i <= N ; i++){
                 scanf("%d",&limit[i]) ;
                 now[i] = 0 ;
                 Right[i] = i+1 ;
             }
             Right[N] = -1 ;
             scanf("%d",&M) ;
             while(M--){
                   scanf("%d%d",&kind,&id) ;
                   if(kind == 1){
                      scanf("%d",&x) ;
                      find_right(id,x) ;
                   }
                   else
                      printf("%d
    ",now[id]) ;
             }
        }
        return 0 ;
    }
  • 相关阅读:
    项目的搭建步骤:
    MySQL复习笔记记录
    记录搭建SSM框架中常用到的功能:(监听器)、过滤器和拦截器 以及相关的拓展内容的学习记录
    为什么要配置spring**.xml或者applicationContext.xml --学习笔记
    java8 Lambda表达式学习笔记
    java多线程高并发学习从零开始——初识volatile关键字
    java多线程高并发学习从零开始——新建线程
    JVM工作机制以及异常处理之内存溢出OOM(OutOfMemoryError)/SOF(StackOverflowError)--Java学习记录2——更新中
    记录使用idea构建出现错误:failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:2.9.1:jar——Java学习记录3
    Oracle创建用户和表空间的步骤 和 导入dmp文件的方法 —— 数据库学习
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3471671.html
Copyright © 2011-2022 走看看