贪心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/ [算法] 一开始想样例是怎么 ...
随机推荐
- C# post json 匿名类 序列化
//第一步 建立HTTP请求对象 var httpWebRequest = (HttpWebRequest)WebRequest.Create(apiURL); httpWebRequest.Cont ...
- Java 的布局管理器GridBagLayout的使用方法【图文说明】
https://www.cnblogs.com/taoweiji/archive/2012/12/14/2818787.html GridBagLayout是java里面最重要的布局管理器之一,可以做 ...
- [Python_5] Python 线程
0. 说明 Python 线程笔记 1. 低级 API # -*-coding:utf-8-*- """ 线程 """ "&quo ...
- .NET Core 使用 EF 出错的解决方法
在.NET Core 项目钟(类库),使用Entity Framework,建立模型生成数据库时,失败 Could not load assembly 'xxx'. Ensure it is refe ...
- 2.2Python基础语法(二)之运算符
返回总目录 目录: 1.Python运算符的分类 2.算数运算符 3.复合运算符 4.比较运算符 5.逻辑运算符 (一)Python运算符的分类: (二)算数运算符: 注意下面三种算数符号: 1.** ...
- beta冲刺————第一天(1/5)
人员的再次分配: 调走人员:陈裕鹏(原来在本队伍主要进行文章推荐算法的设计) 调入人员:陈邡(原Dipper团队,负责游戏内容的策划案,以及做一些后端的探索工作.) 现队员工作划分: 王国华,吴君毅, ...
- 团队作业6--展示博客(Alpha版本)
一.团队展示: 1.队名:软件1412--博客管理系统 2.队员学号(标记组长) 曾海明(组长):201421122036 周雅静(组员):201421122003 王珏(组员):2014211 ...
- django复习-3-请求与响应
一.请求request 前端向后端传递参数有几种方式? 提取URL的特定部分,如/weather/beijing/2018,可以在服务器端的路由中用正则表达式截取: "http://127. ...
- Scala学习之路 (七)Scala的柯里化及其应用
一.概念 柯里化(currying, 以逻辑学家Haskell Brooks Curry的名字命名)指的是将原来接受两个参数的函数变成新的接受一个参数的函数的过程.新的函数返回一个以原有第二个参数作为 ...
- vagrant特性——基于docker开发环境(docker和vagrant的结合)-2-命令
Docker Commands Docker provider公开了一些额外的vagrant命令,这些命令对于与Docker容器交互非常有用.这有助于你在vagrant之上的工作流程,这样你就可以在底 ...