[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
", nt);
	
	return 0;
}
原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5814733.html