您的位置:首页 > 其它

Contest on codeforce A

2013-01-24 11:20 489 查看
A. Old Peykan

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Input
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).

Output
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.

Sample test(s)

Input
4 6
1 2 5 2
2 3 3 4


Output
10


Input
2 3
5 6
5 5


Output
14




题意:

从1到n点,di表示i~i+1所需要的时间,以及要用到的花费,si表示在i点停留m时间后可以补充的花费,问到达终点所要用到的最短时间。


思路:

可以算是贪心。。
sum1表示从1~i 有用到的花费;
sum2表示从1~i 可以得到的花费;
只要出现了sum2<sum1;则需要在i之前的点停留至少m时间。。。问题时判断在哪点停留,且停留多久。
这里就要用到贪心;
求出1~i中可以得到花费的最大值,只要出现需要停留的情况,就在最大值处停留,并更新sum2的值。并且花费最大值也要随着一起更新。。


代码:





View Code

1 #include<iostream>
 2 #include<math.h>
 3 #include<cstdlib>
 4 using namespace std;
 5 int d[1005];
 6 int s[1005];
 7 int s2[1005];
 8 int main(){
 9     int n,m;
10     while(cin>>n>>m){
11        for(int i=1;i<=n;i++){
12                cin>>d[i];
13            //    s2[i]=s[i];
14                }
15        for(int k=1;k<=n;k++)
16                cin>>s[k];
17        int sum1=0;
18        int sum2=0;
19        int re=0;
20     //   sort(s2,s2+n);
21        int maxn=0;
22        int res=0;
23        for(int j=1;j<=n;j++){
24                sum1+=d[j];
25                sum2+=s[j];
26                if(maxn<s[j])
27                        maxn=s[j];
28                if(sum2-sum1<0){
29                        if(maxn<re)
30                            maxn=re;
31                         int temp=(abs(sum2-sum1))/maxn;
32                        if(abs(sum2-sum1)%maxn)
33                              temp++;
34                        res+=temp;
35                        sum2+=(maxn*temp);
36                        re=maxn;
37                        maxn=0;
38                        }
39                        }
40        cout<<sum1+m*res<<endl;
41        }
42     return 0;
43 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: