ACM训练赛

5 篇文章 0 订阅

tyd非常喜欢玩雪,下雪下了n天,第i天她会堆一堆大小为Vi的雪堆,但因为天气原因,每堆雪会融化Ti,问每天总共融化了多少雪
Input
第一行一个 N (1 ≤ N ≤ 105) — 下雪天数.

第二行 N 个数 V1, V2, …, VN (0 ≤ Vi ≤ 109), Vi 表示第i天新增的雪堆大小 .

第三行 N个数 T1, T2, …, TN (0 ≤ Ti ≤ 109), Ti 表示第i天每堆雪融化的体积 .

Output
输出一行 N个数, 第 i个数表示第i天总共融化了多少雪 .

Examples
Input
3
10 10 5
5 7 2
Output
5 12 4
Input
5
30 25 20 15 10
9 10 12 4 13
Output
9 20 35 11 25
Note
第一个样例, tyd第一天堆了大小为10的雪堆, 第一天融化了5. 第二天, 她又堆了大小为10的雪堆. 这堆雪第二天融化了7.第一堆雪第二天融化了5,化完了.第三天她堆了大小为5的雪堆,这堆雪第三天融化了2,第二堆雪也融化了2.所以第一天总共融化了5,第二天5+7=12,第三天2+2=4
原题链接
一开始写了一个,毫无疑问的超时(Time limit),额。。

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100005;
int build[N], thaw[N];
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin>>build[i];
	}
	for (int i = 0; i < n; i++)
	{
		cin>>thaw[i];
	}
	int cnt;
	for (int i = 0; i < n; i++)
	{
		cnt = 0;
		for (int j = 0; j <= i; j++)
		{
			if (build[j] - thaw[i] < 0)
			{
				cnt = cnt + build[j];
				build[j] = 0;
			}
			else
			{
                build[j] = build[j] - thaw[i];
				cnt = cnt + thaw[i];
			}
		}
		cout << cnt << " " ;
	}
	return 0;
}

emm…copy大神佳作

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll build[N],thaw[N],sum[N];
priority_queue< ll, vector<ll>, greater<ll> > s;
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>build[i];
	}
	for(int i=1;i<=n;i++)
	{
		cin>>thaw[i]; 
		sum[i]=sum[i-1]+thaw[i];
	}
	ll cnt;
	s.push(build[1]);
	if(build[1]>thaw[1])
	{
		cout<<thaw[1];
	}
	else
	{
		cnt=build[1];
		s.pop();
	}
	for(int i=2;i<=n;i++)
	{
		cnt = 0;
		s.push(build[i]+sum[i-1]);
		while(!s.empty()&&s.top()<=sum[i])
		{
			cnt+=s.top()-sum[i-1];
			s.pop();
		}
		cnt += thaw[i]*s.size();
		cout<<" "<<cnt;
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值