1014 of greedy strategy
来源:互联网 发布:Java编程思想豆瓣 编辑:程序博客网 时间:2024/06/01 03:57
Problem O
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 71 Accepted Submission(s) : 13
Problem Description
Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry.
There is a ferry across the river that can take n cars across the river in t minutes and return in t minutes. m cars arrive at the ferry terminal by a given schedule. What is the earliest time that all the cars can be transported across the river? What is the minimum number of trips that the operator must make to deliver all cars by that time?
There is a ferry across the river that can take n cars across the river in t minutes and return in t minutes. m cars arrive at the ferry terminal by a given schedule. What is the earliest time that all the cars can be transported across the river? What is the minimum number of trips that the operator must make to deliver all cars by that time?
Input
The first line of input contains c, the number of test cases. Each test case begins with n, t, m. m lines follow, each giving the arrival time for a car (in minutes since the beginning of the day). The operator can run the ferry whenever he or she wishes, but can take only the cars that have arrived up to that time.
Output
For each test case, output a single line with two integers: the time, in minutes since the beginning of the day, when the last car is delivered to the other side of the river, and the minimum number of trips made by the ferry to carry the cars within that time. <br> <br>You may assume that 0 < n, t, m < 1440. The arrival times for each test case are in non-decreasing order.
Sample Input
22 10 1001020304050607080902 10 3103040
Sample Output
100 550 2
题目要求:货船运送货车过河问题。给与货船最大装船量和货车的数目和到达时间,要求保证最小运送次数下的最短时间。
思路:时间最短,即保证最后一辆货车最早到岸,而其前最大装船量内的货车不计入时间。进行递归调用后得到结果。m%n为0,直接n辆递归计算时间,否者先运前m%n辆然后n量递归求出时间。
细节:判断运完一次回岸后是否积累到最大装车量,用于计算时间。
#include<iostream>#include<iostream>#include<string.h>#include<set>#include<stdio.h>#include<vector>#include<algorithm>#include<numeric>#include<math.h>#include<string.h>#include<sstream>#include<stdio.h>#include<string>#include<cstdlib>#include<algorithm>#include<iostream>#include<map>#include<queue>#include<iomanip>#include<cstdio>using namespace std;int main(){int n, m, t,e,c,x,y,o,i;scanf("%d", &c);while (c--){scanf("%d%d%d", &n, &t, &m);x = m / n;o=m%n;if (o)x++;y=0;for(i=1;i<=m;i++) { scanf("%d",&e); if(o) { if(i==o)y=e; else if(i%n==o) { if(y+2*t>e)y+=2*t; else y=e; } } else if(i==n){y=e;} else if(i%n==0) { if(y+2*t>e)y+=2*t; else y=e; } }y+=t;printf("%d %d\n", y, x);}}
0 0
- 1014 of greedy strategy
- 1000 of greedy strategy*
- 1001 of greedy strategy
- 1002 of greedy strategy
- 1003 of greedy strategy
- 1004 of greedy strategy
- 1005 of greedy strategy
- 1007 of greedy strategy*
- 1010 of greedy strategy
- 1011 of greedy strategy
- 1012 of greedy strategy
- 1013 of greedy strategy
- 1015 of greedy strategy
- 1016 of greedy strategy
- 1017 of greedy strategy*
- the summary of greedy strategy
- Change of strategy
- 1006 of strategy
- Java经典设计模式(1):五大创建型模式(附实例和详解)
- 机器学习整理
- Linux下安装RabbitMQ
- jQuery.validate使用手册,详解
- 编译器的工作过程
- 1014 of greedy strategy
- 15电气郄慧敏vb作业4VB中数字的交换
- React-Native run-android报错以及adb 命令不识别
- Java经典设计模式(2):七大结构型模式(附实例和详解)
- 二分查找【模板】
- WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED解决方法
- 左侧ListView,右侧联动ListView,且右侧中ListView中嵌套GridView
- Android中Service与Activity的有关于进程与线程的问题
- IOS面试题一