zoukankan      html  css  js  c++  java
  • J. Cola

    J. Cola

    time limit per test

    4.0 s

    memory limit per test

    64 MB

    input

    standard input

    output

    standard output

    At Cola factory there is N bottles on a line. Also, there is a machine that pour cola in these bottles one by one. Every thing was working well. Suddenly, the machine started acting in a strange way !!

    It started choosing a random bottle and pour a random quantity of cola in this bottle.

    When a bottle is full, the rest of the cola goes to the bottle on the right of it, and if it was the last bottle, then the rest of the cola will be wasted .

    As an engineer in this factory, you were given the capacity of every bottle in Litters. Also, you know the record of machine moves because it is stored in the Microcontroller memory of the machine. You are asked to compute the amount of wasted cola, and the amount of cola every bottle has at the end.

    Input

    Your program will be tested on one or more test cases. The first line of the input will be a single integer T the number of test cases. Followed by the test cases.

    Every test case starts with two numbers N and M (1 ≤ N ≤ 105), (1 ≤ M ≤ 105) denoting the number of bottles, and the number of moves the machine had did.

    Then follow N integers on a line separated by spaces. The integer Ci denotes the capacity of the ith bottle (1 ≤ Ci ≤ 109) where (1 ≤ i ≤ N).

    M lines follow denoting the moves. Each line contains two integers X and Y (1 ≤ X ≤ N), (1 ≤ Y ≤ 109) means that the machine poured Yliters of cola in the Xth bottle .

    Output

    For each test case print two lines.

    First line contains the amount of wasted cola.

    The second line contains N integers separated by spaces denoting the amount of cola in every bottle at the end.

    Example
    input
    Copy
    2
    5 3
    5 4 3 2 1
    3 4
    4 4
    1 2
    6 3
    3 4 5 5 6 7
    3 3
    1 8
    5 9
    output
    Copy
    2
    2 0 3 2 1
    0
    3 4 4 0 6 3

    题意:n个杯子,第i个杯子容量为a[i],m次操作,每次往杯子x里倒y单位的水,第i个杯子满了之后多余的水就会倒进第i+1个杯子里,如果第n个杯子也满了那么多余的水就浪费,问浪费了多少水 

    完全按照题目意思模拟肯定是会超时的,所以先不管加可乐的时候会不会漏出,先加进去,加完m次之后,在统一处理大于容量的情况

    #include<iostream>
    #include<algorithm>
    #include<string.h>
    #include<string>
    #include<vector>
    #include<stack>
    #include<math.h>
    #define mod 998244353
    #define ll long long
    #define MAX 0x3f3f3f3f
    using namespace std;
    struct node 
    {
        ll k;
        ll now;
    }p[100005];
    int main()
    {
        ll n,t,m;
        cin>>t;
        while(t--)
        {
            cin>>n>>m;
            for(ll i=1;i<=n;i++)
            {
                cin>>p[i].k;
                p[i].now=0;
            }
            ll x,y;
            ll ans=0;
            for(ll i=0;i<m;i++)
            {
                cin>>x>>y;
                p[x].now=p[x].now+y;//wa了好多次,有可能多次都是从同一个瓶子开始加可乐
            }
            for(ll i=1;i<n;i++)
            {
                if(p[i].now>p[i].k)
                {
                    p[i+1].now=p[i+1].now+p[i].now-p[i].k;
                    p[i].now=p[i].k;
                }
            }
            if(p[n].now>p[n].k)
            {
                ans=p[n].now-p[n].k;
                p[n].now=p[n].k;
            }
            cout<<ans<<endl;
            for(ll i=1;i<=n;i++)
            {
                if(i!=n)
                    cout<<p[i].now<<' ';
                else
                    cout<<p[i].now<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    静态嵌套类(Static Nested Class)和内部类(Inner Class)的不同?
    编写多线程程序有几种实现方式?
    文件拷贝
    如何实现对象克隆?
    c#如何保存richtextbox的rtf格式
    C#实现文件与二进制互转并存入数据库
    c#中绝对路径和相对路径
    C#实现MySQL数据库中的blob数据存储
    CSS控制文字,超出显示省略号
    ES6 语句判断简写
  • 原文地址:https://www.cnblogs.com/-citywall123/p/11222956.html
Copyright © 2011-2022 走看看