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;
}

 

转载于:https://www.cnblogs.com/-citywall123/p/11222956.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值