Ferry Loading II_贪心
Description
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
Output
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一趟。求最短时间和次数
【思路】贪心
- 最短时间只和最后一只船有关系. 如果 m%n==0 则每次都运n个,必是最优。
- 否则,第一次运 m%n个, 可保证最优。
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int n,m,t;
int main()
{
int ti;
scanf("%d",&ti);
while(ti--)
{
scanf("%d%d%d",&n,&t,&m);
int a=m%n;
int k=m/n;
if(a!=)//余数不为0,多运一次
{
k++;
}
int mint=;
int tmp;
int i=;
if(a)//余数不为0,第一次将a辆车运过去
{
for(i=;i<a;i++)
{
scanf("%d",&tmp);
}
mint=tmp+*t;//a辆车中最后一辆车达到的时间出发
}
int cnt=;
while(i<m)//另外的m/n组
{
scanf("%d",&tmp);
cnt++;
if(cnt==n)//到n一组的车辆
{
if(tmp>mint)//时间晚的话,从tmp时间出发
mint=tmp+*t;
else mint+=*t;//时间早的话,直接出发,没有等待时间
cnt=;//清零
}
i++;
}
mint-=t;//最后一趟,不用回来了
printf("%d %d\n",mint,k);
}
return ;
}
Ferry Loading II_贪心的更多相关文章
- poj 2336 Ferry Loading II ( 【贪心】 )
Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3704 Accepted: 1884 ...
- TOJ 2419: Ferry Loading II
2419: Ferry Loading II Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Subm ...
- Ferry Loading III[HDU1146]
Ferry Loading IIITime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- POJ-2336 Ferry Loading II(简单DP)
Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3763 Accepted: 1919 Desc ...
- POJ 2609 Ferry Loading(双塔DP)
Ferry Loading Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1807 Accepted: 509 Sp ...
- poj-2336 Ferry Loading II(dp)
题目链接: Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3946 Accepted: ...
- Ferry Loading||
uva10440:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&am ...
- [POJ2336]Ferry Loading II
题目描述 Description Before bridges were common, ferries were used to transport cars across rivers. Rive ...
- 【HDOJ】1406 Ferry Loading III
模拟,注意需要比较队头与当前时间的大小关系. #include <cstdio> #include <cstring> #include <cstdlib> #de ...
随机推荐
- lrzsz在CentOS7的安装
在超级用户下打一句命令: yum install lrzsz 或者,在普通用户打一句命令,需要输入超级用户密码: sudo yum install lrzsz 然后使用Xshell 5建立连接即可
- 支持SQL Server数据库又支持MongoDB数据库的数据访问设计
网站整体架构建议采用工厂模式 分别包括:数据访问层DAL,数据访问接口层IDAL,工厂层DALFactory,业务逻辑层,显示层这样的架构方式 在WebConfig配置采用何种数据库的数据访问层 &l ...
- Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- 并发容器之CopyOnWriteArrayList
原文链接: http://ifeve.com/java-copy-on-write/ Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容 ...
- [转]Java FileInputStream与FileReader的区别
在解释Java中FileInputStream和FileReader的具体区别之前,我想讲述一下Java中InputStream和Reader的根本差异,以及分别什么时候使用InputStream和R ...
- asp.net core StaticFiles中间件修改wwwroot
new StaticFileOptions() { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentD ...
- C#入门篇6-5:字符串操作 测试StringBuilder的运行效率
//测试StringBuilder的运行效率 public static void Fun2() { #region string string str = "我喜欢编程!"; / ...
- plsql快速选中一行的快捷键
实际工作中,经常用到pl/sql,在sql window中,经常性的用到选中一行然后按F8执行这条sql语句.用鼠标选中一行不是特别方便.用快捷键就快多了. 1.使用home键(不是windows键奥 ...
- 如何在android项目中引用project作为类库引用
前言: 在我们开发项目的时候,存在很多多个项目共有一个资源.逻辑代码的情况,这种情况一般我们采用在开发项目中导入别的项目作为引用的类库.资源等. 操作: 1. 新建一个android项目common ...
- POJ 3522 Slim Span 最小生成树,暴力 难度:0
kruskal思想,排序后暴力枚举从任意边开始能够组成的最小生成树 #include <cstdio> #include <algorithm> using namespace ...