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
Output
Input
Output
题意:
从1到n点,di表示i~i+1所需要的时间,以及要用到的花费,si表示在i点停留m时间后可以补充的花费,问到达终点所要用到的最短时间。
思路:
可以算是贪心。。
sum1表示从1~i 有用到的花费;
sum2表示从1~i 可以得到的花费;
只要出现了sum2<sum1;则需要在i之前的点停留至少m时间。。。问题时判断在哪点停留,且停留多久。
这里就要用到贪心;
求出1~i中可以得到花费的最大值,只要出现需要停留的情况,就在最大值处停留,并更新sum2的值。并且花费最大值也要随着一起更新。。
代码:
View Code
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 }
相关文章推荐
- Contest on codeforce A
- codeforce contest 713 #371 Div1 A
- codeforce contest 713 #371 Div1 B
- Codeforce Gym 100819L : 2015-2016 ACM-ICPC Pacific Northwest Regional Contest - L 这是一道阅读理解(微笑)
- Codeforce Gym 100819P : 2015-2016 ACM-ICPC Pacific Northwest Regional Contest - P 仔细看题啊!
- codeforce contest 713 #371 Div1 C
- codeforce contest 103 problem D Time to Raid Cowavans(根号算法)
- codeforce690A2 Collective Mindsets (medium) (from :Helvetic Coding Contest 2016 online mirror )
- CodeForce 237C Primes on Interval(二分+ 素数筛法)
- Codeforce-242E-XOR on Segment(线段树区间更新)
- codeforce 237 C. Primes on Interval
- codeforce 560 A Currency System in Geraldion
- Project Hosting on Google Code 为开放源代码项目提供了自由的协作开发环境。
- CodeForce 89A.Robbery
- CodeForce 180 C ——Letter
- [Android]startActivityForResult启动singleTask的Activity,则onActivitResult()立即回调且resultCode为RESULT_CANCEL
- Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)E. Game with String(枚举)
- codeforce-601A. The Two Routes(最短路)
- Codeforces Round #427 (Div. 2)B. The number on the board
- codeforce 829C