贪心Crossing river
英文题目:
A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has a different rowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.
1
4
1 2 5 10
17 ----------------------------------------------------------------------------------------------------------------------------------------------
中文描述:就是有一条河,只有一条船,有n个人,每个人有自己的过河时间,一条船只能载两个人,过河时间算两个人中最大时间那个。算出最少过河时间。 个人理解:首先我做这道题题目时候,这个题目使用贪心策略;
首先我们来分析四个人的情况,因为3、2、1个人的时候很好办;首先有a(最快)、b(次快)、c(次慢)、d(最慢),分两个策略来解决问题。
1.a和b过河,a回来(保证时间最少),c和d过河,b回去(保证时间最少),保证下一次运输时候,a和b来作为来回运输的人,保证时间最少。
2.a和d过河,a回来,a和c过去,a回来。
所以我们只要在n>3个人的时候来比较这两个策略那一个策略时间比较少,然后n-2;直到人数剩下3人一下(包括3个人)。 我们来代码实现:
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<iterator>
#include<string>
#include<string.h>
#include<ctype.h>
#include<map>
#include<stack>
#include<queue>
#include<iostream>
#include<time.h> using namespace std; #define rep(i ,a, b) for(int i = a; i <= b; i++)
#define per(i, a, b) for(int i = a; i <= b; i--)
int a[]; int main()
{
int t, n, sum;//最简单的基础贪心问题;
scanf("%d", &t);//过河有两种策略;
while(t--)
{
n = ;
sum = ;
scanf("%d", &n);
for(int i = ; i < n; i++)
{
scanf("%d", &a[i]);
}
sort(a, a+n);
while(n > )
{
sum = min(sum+a[]+a[]+a[n-]+a[],sum+a[n-]+a[]+a[n-]+a[]);
n-=;//保证人数过去时时间最大的两个人。
}
if(n == )
{
sum += a[]+a[]+a[];
}
if(n==)
{
sum +=a[];
}
if(n==)
{
sum += a[];
}
printf("%d\n", sum);
}
return ;
}
贪心Crossing river的更多相关文章
- POJ 1700 Crossing River (贪心)
Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9585 Accepted: 3622 Descri ...
- poj 1700 Crossing River 过河问题。贪心
Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9887 Accepted: 3737 De ...
- Crossing River
Crossing River 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26251 题意: N个人希望去过 ...
- Crossing River(1700poj)
Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9919 Accepted: 3752 De ...
- poj1700--贪心--Crossing River
Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12260 Accepted: 4641 D ...
- ACM学习历程——POJ 1700 Crossing River(贪心)
Description A group of N people wishes to go across a river with only one boat, which can at most ca ...
- poj-1700 crossing river(贪心题)
题目描述: A group of N people wishes to go across a river with only one boat, which can at most carry tw ...
- Crossing River poj1700贪心
题目描述:N个人过河,只有一只船,最多只能有两人划船,每个人划船速度不同,船速为最慢的人的速度.输入T为case个数,每个case输入N为人数,接下来一行输入的是每个人过河的时间,都不相同.要求输出N ...
- 702:Crossing River (贪心)
[题目描述] N个人过河,一次过去2个回来一个,给出每个人所需时间,问最小过河时间. [题目链接] http://noi.openjudge.cn/ch0406/702/ [算法] 一开始想样例是怎么 ...
随机推荐
- shell傳遞參數
Shell 传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 比如我們 ...
- python图像处理:pytesseract和PIL
大概介绍下相关模块的概念: Python-tesseract 是光学字符识别Tesseract OCR引擎的Python封装类.能够读取任何常规的图片文件(JPG, GIF ,PNG , TIFF等) ...
- Azure DevKit(AZ3166)源码找不到头文件问题
Get “Error Presented: #include errors detected” when opening a project The error message is Error Pr ...
- Nmap 使用技巧及其攻略
Nmap是一款免费开源的网络发现和安全审计工具,支持Windows和Linux平台,有命令行版本和图形化版本.个人建议去学习 nmap 的命令行版本,因为与图形化版本 zenmap 相比,它提供了更多 ...
- JS控制div跳转到指定的位置的几种解决方案总结
原文:http://www.jb51.net/article/96574.htm 这篇文章主要介绍了JS控制div跳转到指定的位置的几种解决方案总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考 ...
- Linux chmod +755和chmod +777 各是什么意思呢?
你可以在linux终端先输入ls -al,可以看到如: -rwx-r--r-- (一共10个参数)第一个跟参数跟chmod无关,先不管.2-4参数:属于user5-7参数:属于group8-10参数: ...
- php header函数导出excel表格
推荐一个除了用PHPExcel导出表格之外的另外一种比较简单不需要引入类文件的表格导入方法——header()导出excel表格. 导出表格的步骤封装成了方法,以便于重复使用,代码如下: /** * ...
- linux(centos 7) 下安装nodejs
1,到node官网下载linux版本,有32和64位版本 2,将文件上传到linux下 3,使用tar -xvf node-v8.9.3-linux-x64.tar.xz 进行解压 4,建立软连接,变 ...
- dubbo接口访问控制
微服务背景下,一个web应用都可能不再service依赖,而是通过RPC调用远端服务器上的服务.这些服务里,就包括了一些不能轻易暴露的后台功能接口.暴露出去的dubbo接口注册到某一个zk上后,该du ...
- WorldWind源码剖析系列:星球表面渲染类WorldSurfaceRenderer
星球表面渲染类WorldSurfaceRenderer描述如何渲染星球类(如地球)表面影像纹理.该类的类图如下. 星球类World包含的主要的字段.属性和方法如下: public const int ...