Codeforces-470 div2 C题
C. Producing Snowtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume Vi and put it in her garden.
Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is Ti, each pile will reduce its volume by Ti. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other.
Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.
You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day.
InputThe first line contains a single integer N (1 ≤ N ≤ 105) — the number of days.
The second line contains N integers V1, V2, ..., VN (0 ≤ Vi ≤ 109), where Vi is the initial size of a snow pile made on the day i.
The third line contains N integers T1, T2, ..., TN (0 ≤ Ti ≤ 109), where Ti is the temperature on the day i.
OutputOutput a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.
ExamplesinputCopy3
10 10 5
5 7 2output5 12 4inputCopy5
30 25 20 15 10
9 10 12 4 13output9 20 35 11 25NoteIn the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day.
题目大意:就是求每天会融化多少雪。
思路:这题目两种思路
1、二分查找第i堆雪什么时候融化。
2、优先队列+前缀和 (优先队列也可以用multiset )
开始我只想到是优先队列,但是怎么处理那些剩下的雪没有好的方法。
后面又想到了是前缀和,但是又没和优先队列联系起来。
AC代码
1 #include<iostream> 2 #include<bits/stdc++.h> 3 #define ll long long 4 using namespace std; 5 priority_queue < ll , vector < ll > , greater < ll > > q; 6 ll a[100500]; 7 ll c[100500]; 8 ll sum[100500]; 9 int main(){ 10 ll n; 11 cin>>n; 12 for(ll i=1;i<=n;i++) 13 scanf("%d",a+i); 14 for(ll i=1;i<=n;i++){ 15 scanf("%d",c+i); 16 sum[i]=c[i]+sum[i-1]; 17 } 18 for(ll i=1;i<=n;i++){ 19 q.push(a[i]+sum[i-1]); //关键一步!!!让新加的雪堆变成第一天就已经放置的雪堆!!! 20 ll ans=0; 21 while(!q.empty()&&q.top()<sum[i]){ 22 ans+=(q.top()-sum[i-1]); 23 q.pop(); 24 } 25 ans+=c[i]*q.size(); 26 cout<<ans<<' '; 27 } 28 cout<<endl; 29 return 0; 30 }