贪心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/ [算法] 一开始想样例是怎么 ...
随机推荐
- android打开存储卡(TF卡\SD卡)中的sqlite文件
android的SDK直接支持sqlite3的API. 打开SD卡上面的sqlite数据库,不需要SQLiteOpenHelper的继承类.只需要,SQLiteDatabase中的一些静态方法.如 ...
- .NET Core 使用 EF 出错的解决方法
在.NET Core 项目钟(类库),使用Entity Framework,建立模型生成数据库时,失败 Could not load assembly 'xxx'. Ensure it is refe ...
- centos中,tomcat项目创建文件的权限
参考文章:https://cloud.tencent.com/info/5f02caa932fd6dbfc46a3bb01af135e0.html 我们在centos中输入umask,会看到输出002 ...
- Local policy - User rights assignment 对照表
"SeCreateTokenPrivilege" --> "Create a token object" "SeAssignPrimaryTo ...
- October 12th 2017 Week 41st Thursday
Be happy for this moment. This moment is your life. 为这一刻感到高兴,这一刻是你的人生. Yesterday Tencent became Asia ...
- phpstorm添加laravle语法支持
PHPStorm神器可以支持更友好的laravel框架代码提示,只需要执行如下才做: 第一步:在项目的composer.json中添加如下一行 "require": { " ...
- P1231 教辅的组成
传送门:https://www.luogu.org/problemnew/show/P1231 这是一道很不错的网络流入门题,关键在于如何建图. 首先,我们将练习册和源点连一条边权为1的边,然后若书 ...
- QGis C++ 开发之图层分类显示
开发环境:Win10 + VS2010 + Qt 4.8.6 + QGis 2.14.4 简单介绍下如何用C++方式实现QGis中图层分类显示的方法. 要实现图层的分类显示主要会用到QgsCatego ...
- Working With JSON
JavaScript对象表示法(JSON)是用于将结构化数据表示为JavaScript对象的标准格式,通常用于在网站上表示和传输数据(例如从服务器向客户端发送一些数据,因此可以将其显示在网页上). J ...
- QT 用listveiw显示图片
很多的时候需要浏览图片,一般会使用listview 显示图片,接下来我用listview显示图片 代码如下: QStandardItem * s1; QStandardItem * s2; QStan ...