[codeforces 241]A. Old Peykan

[codeforces 241]A. Old Peykan

试题描述

There are n cities in the country where the Old Peykan lives. These cities are located on a straight line, we'll denote them from left to right as c1, c2, ..., cn. The Old Peykan wants to travel from city c1 to cn using roads. There are (n - 1) one way roads, the i-th road goes from city ci to city ci + 1 and is di kilometers long.

The Old Peykan travels 1 kilometer in 1 hour and consumes 1 liter of fuel during this time.

Each city ci (except for the last city cn) has a supply of si liters of fuel which immediately transfers to the Old Peykan if it passes the city or stays in it. This supply refreshes instantly k hours after it transfers. The Old Peykan can stay in a city for a while and fill its fuel tank many times.

Initially (at time zero) the Old Peykan is at city c1 and s1 liters of fuel is transferred to it's empty tank from c1's supply. The Old Peykan's fuel tank capacity is unlimited. Old Peykan can not continue its travel if its tank is emptied strictly between two cities.

Find the minimum time the Old Peykan needs to reach city cn.

输入

The first line of the input contains two space-separated integers m and k (1 ≤ m, k ≤ 1000). The value m specifies the number of roads between cities which is equal to n - 1.

The next line contains m space-separated integers d1, d2, ..., dm (1 ≤ di ≤ 1000) and the following line contains m space-separated integers s1, s2, ..., sm (1 ≤ si ≤ 1000).

输出

In the only line of the output print a single integer — the minimum time required for The Old Peykan to reach city  cn from city c1.

输入示例

4 6
1 2 5 2
2 3 3 4

输出示例

10

数据规模及约定

见“输入

题解

先尽量往右走,发现油量不够时再把加油的时间补回来,因为 k 是固定的,而且没经过一个城市就能在瞬间得到这个城市拥有的油量,所以只要加油时贪心地每次都取当前经过的所有城市中拥有最多油量的城市即可。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std;

const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
    if(Head == Tail) {
        int l = fread(buffer, 1, BufferSize, stdin);
        Tail = (Head = buffer) + l;
    }
    return *Head++;
}
int read() {
    int x = 0, f = 1; char c = getchar();
    while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
    while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

#define maxn 1010
#define LL long long
int n, k, d[maxn], s[maxn];

int main() {
	n = read(); k = read();
	for(int i = 1; i <= n; i++) d[i] = read();
	for(int i = 1; i <= n; i++) s[i] = read();
	
	int ni = 1, mx = 0;
	LL nt = 0, ns = 0;
	for(; ni <= n + 1;) {
		mx = max(mx, s[ni]);
		ns += s[ni];
		if(ns >= d[ni]) ;
		else {
			int x = (d[ni] - ns) / mx;
			if(mx * x == (d[ni] - ns)) ; else x++;
			nt += (LL)x * k;
			ns += (LL)x * mx;
		}
		ns -= d[ni]; nt += d[ni]; ni++;
	}
	
	printf("%I64d\n", nt);
	
	return 0;
}

 

转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5814733.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值