uva 10440 Ferry Loading II
来源:互联网 发布:结构体中的数组赋值 编辑:程序博客网 时间:2024/04/28 08:18
原题:
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?
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.
You may assume that 0 < n,t,m < 1440. The arrival times for each test case are in non-decreasing
order.
Sample Input
2
2 10 10
0
10
20
30
40
50
60
70
80
90
2 10 3
10
30
40
Sample Output
100 5
50 2
大意:
有m辆车要过河,有一个渡船,每次可以运n辆车。船往返的时间为t,每辆车到达渡口的时间是一个固定的数。现在问你最少用多长时间能把所以车运过河,在最少时间的情况下最少运多少次。
#include <bits/stdc++.h>using namespace std;int car[1500];int main(){ ios::sync_with_stdio(false); int t,m,n,k,tot,num; cin>>k; while(k--){ cin>>n>>t>>m; tot = num = 0; for(int i = 1 ;i <= m ;i++){ cin>>car[i]; } if(n >= m ){ tot = car[m] + t; num = 1; } else{ if(m % n == 0){ num = m / n; for(int i = n ;i <= m ;i += n){ tot = max(tot,car[i]); tot += t*2; } } else{ num = m / n + 1; tot = car[m % n] + t*2; for(int i = m%n + n ;i <= m ;i += n){ tot = max(tot , car[i]); tot += t*2; } } tot -= t; } cout<<tot<<" "<<num<<endl; } return 0;}
解答:
这道题给定的车到达的时间就是非递减序列,所以不用排序。想要运送时间最少,那就是尽量把车往船上装(在m能整除n的情况下)。如果m不能整除n,那么先把m除以n的余数个车辆送过去,然后再过来把车都装上船,同时也是最少运送次数。
- uva 10440 Ferry Loading II
- uva 10440 Ferry Loading II
- UVa 10440 - Ferry Loading II
- Ferry Loading II
- ZOJ 1918 Ferry Loading II
- 【贪心】poj2336 ferry loading II
- Ferry Loading II 贪心算法
- poj 2336 Ferry Loading II
- POJ2336 Ferry Loading II 动态规划
- ZQUOJ1393 Ferry Loading II 解题报告
- POJ 2336 Ferry Loading II [贪心 DP]
- HRBEU/HEUOJ----1019 Ferry Loading II
- POJ2336 Ferry Loading II 贪心动规
- poj 2336 Ferry Loading II 贪心
- POJ 2336 Ferry Loading II 动态规划
- uva 10901 - Ferry Loading III(simulation)
- UVA 10901 Ferry Loading III 队列模拟
- 贪心之 Ferry Loading II (POJ 2336)
- MyEclipse2014破解
- POJ 3320 (尺取法 map)
- 2016年,文本分析、情感分析和社交分析的10大趋势
- android点击ListView的item后变色
- HttpServlet
- uva 10440 Ferry Loading II
- 纵观设计模式
- 4--静态链表
- spring框架之AOP
- 数据库适当添加索引进行查询优化
- 简易引导图的实现
- 关于View的onMeasure()、onSizeChanged()、onLayout()、onDraw()调用顺序
- CocoaPods安装和使用及问题:Setting up CocoaPods master r
- easyUI 验证