Description
n个杯子,第i个杯子容量为a[i],m次操作,每次往杯子x里倒y单位的水,第i个杯子满了之后多余的水就会倒进第i+1个杯子里,如果第n个杯子也满了那么多余的水就浪费,问浪费了多少水
Input
第一行一整数T表示用例组数,每组用例首先输入两整数n和m表示杯子数量和操作数,之后输入n个整数a[i]表示每个杯子的容量,最后m行每行两个整数x和y表示往x杯子里倒y单位的水(1<=n,m<=1e5,1<=a[i]<=1e9,1<=y<=1e9)
Output
首先输出浪费了多少水,然后输出n个整数表示操作结束后每个杯子里的水量
Sample Input
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
Sample Output
2
2 0 3 2 1
0
3 4 4 0 6 3
Solution
简单题,先把操作弄完再把第i个杯子里多余的水倒进第i+1个杯子里
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 111111
int T,n,m,a[maxn];
ll b[maxn];
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
memset(b,0,sizeof(b));
ll ans=0;
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
b[x]+=y;
}
for(int i=1;i<n;i++)
if(b[i]>=a[i])
b[i+1]+=b[i]-a[i],b[i]=a[i];
if(b[n]>a[n])ans=b[n]-a[n],b[n]=a[n];
printf("%I64d\n",ans);
for(int i=1;i<=n;i++)printf("%I64d%c",b[i],i==n?'\n':' ');
}
return 0;
}