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[];
int main()
{
ll n,t,m;
cin>>t;
while(t--)
{
cin>>n>>m;
for(ll i=;i<=n;i++)
{
cin>>p[i].k;
p[i].now=;
}
ll x,y;
ll ans=;
for(ll i=;i<m;i++)
{
cin>>x>>y;
p[x].now=p[x].now+y;//wa了好多次,有可能多次都是从同一个瓶子开始加可乐
}
for(ll i=;i<n;i++)
{
if(p[i].now>p[i].k)
{
p[i+].now=p[i+].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=;i<=n;i++)
{
if(i!=n)
cout<<p[i].now<<' ';
else
cout<<p[i].now<<endl;
}
}
return ;
}

J. Cola的更多相关文章

  1. 【Java并发编程实战】-----“J.U.C”:CAS操作

    CAS,即Compare and Swap,中文翻译为"比较并交换". 对于JUC包中,CAS理论是实现整个java并发包的基石.从整体来看,concurrent包的实现示意图如下 ...

  2. 【Java并发编程实战】-----“J.U.C”:Exchanger

    前面介绍了三个同步辅助类:CyclicBarrier.Barrier.Phaser,这篇博客介绍最后一个:Exchanger.JDK API是这样介绍的:可以在对中对元素进行配对和交换的线程的同步点. ...

  3. 【Java并发编程实战】-----“J.U.C”:CountDownlatch

    上篇博文([Java并发编程实战]-----"J.U.C":CyclicBarrier)LZ介绍了CyclicBarrier.CyclicBarrier所描述的是"允许一 ...

  4. 【Java并发编程实战】-----“J.U.C”:CyclicBarrier

    在上篇博客([Java并发编程实战]-----"J.U.C":Semaphore)中,LZ介绍了Semaphore,下面LZ介绍CyclicBarrier.在JDK API中是这么 ...

  5. 【Java并发编程实战】-----“J.U.C”:ReentrantReadWriteLock

    ReentrantLock实现了标准的互斥操作,也就是说在某一时刻只有有一个线程持有锁.ReentrantLock采用这种独占的保守锁直接,在一定程度上减低了吞吐量.在这种情况下任何的"读/ ...

  6. JAVA并发编程J.U.C学习总结

    前言 学习了一段时间J.U.C,打算做个小结,个人感觉总结还是非常重要,要不然总感觉知识点零零散散的. 有错误也欢迎指正,大家共同进步: 另外,转载请注明链接,写篇文章不容易啊,http://www. ...

  7. Android Studio解决未识别Java文件(出现红J)问题

    1.问题:java文件出现了红J的问题,正常情况下应该是显示蓝色的C标识. 2.解决方案:切换到project视图下,找到app这个module里的build.gradle,在android结构里插入 ...

  8. //给定N个整数序列{A1,A2,A3...An},求函数f(i,j)=(k=i~j)Ak的求和

    //给定N个整数序列{A1,A2,A3...An},求函数f(i,j)=(k=i~j)Ak的求和 # include<stdio.h> void main() { ,sum1; ]={,- ...

  9. 面试题:给定数组a,找到最大的j-i, 使a[j]>a[i]

    第一种方法: 用两重循环对每对点都试一下,然后取最大值即可,时间复杂度为O(n2) #include <iostream> #include <algorithm> using ...

随机推荐

  1. 导入spark程序的maven依赖包时,无法导入,报错Unable to import maven project: See logs for details

    问题:导入spark程序的maven依赖包时,无法导入,且报错:0:23 Unable to import maven project: See logs for details 2019-08-23 ...

  2. Nacos client 客户端cpu占用100% 问题排查和解决方案

    Nacos version:1.1.3client version:1.0.0 dependency: 'org.springframework.cloud:spring-cloud-alibaba- ...

  3. 从七牛服务下载PDF文件

    /** * 从七牛下载PDF文件 * @param request * @param response * @param exhiId * @throws MalformedURLException ...

  4. Django 学习视图之FBV与CBV

    一. CBV与FBV CBV:Class Based View FBV:Function Based View 我们之前写过的都是基于函数的view,就叫FBV.还可以把view写成基于类的,那就是C ...

  5. S32K144之FlexMem,FlexNVM,FlexRAM,System RAM, SRAM 区别与联系

    参考手册中常常见到有关memory的关键字,如FlexMem,FlexNVM,FlexRAM,System RAM, SRAM,那么它们到底是什么意思呢?有什么区别和联系? 参考资料 [1]S32K1 ...

  6. 观察者设计模式(C#委托和事件的使用)

    观察者设计模式定义了对象间的一种一对多的依赖关系,以便一个对象的状态发生变化时,所有依赖于它的对象都得到通知并自动刷新.在现实生活中的可见观察者模式,例如,微信中的订阅号,订阅博客和QQ微博中关注好友 ...

  7. 字符串NSString与NSMutableString常用方法

    NSString 1.初始化 NSString *str1 = @"a OC Program"; 2.初始化 NSString *str2 = [[NSString alloc] ...

  8. C#调用Win32 的API函数--User32.dll ----转载

    Win32的API函数是微软自己的东西,可以直接在C#中直接调用,在做WinForm时还是很有帮助的.有时候我们之直接调用Win32 的API,可以很高效的实现想要的效果. using System; ...

  9. Angular4之时间管道

    {{时间戳 |date:“yyyy/MM/dd HH:mm:ss”}} “YYYY/MM/DD”不可

  10. js 用于运行string中的<script>和</script>之间的函数

    /** * Created by 炜文 on 2017/2/15. */ var intext = '485222<script> var i=2;var j=2;console.log( ...