题目链接

题目大意

t组数据(t<=20)

给你n个人(n<=1000)过河,每个人都有权值,一条船,每次船最多运2个人,每次的花费为两个人的较大花费

求所有人都过河需要的最小花费

题目思路

经典的过河问题,记录一下

先将权值从小到大排序一下

每次运两个人显然有两种最优的方法

1:先运(a[1],a[2])过去,a[1]回来,再运(a[n],a[n-1])过去,a[2]回来

cost1=a[n]+2*a[2]+a[1]

2:先运(a[n],a[1])过去,a[1]回来,再运(a[n-1],a[1])过去,a[1]再回来

cost2=a[n]+a[n-1]+2*a[1]

然后当n只有三个的时候特判一下即可

代码

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
//#include<unordered_map>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1000+5,inf=0x3f3f3f3f,mod=1e9+7;
const double eps=1e-5;
int n,a[maxn];
int main(){
int _;scanf("%d",&_);
while(_--){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
sort(a+1,a+1+n);
int ans=0;
while(n>3){
ans+=min(a[n]+a[n-1]+2*a[1],a[n]+2*a[2]+a[1]);
n-=2;
}
if(n==1){
ans+=a[1];
}else if(n==2){
ans+=a[2];
}else{
ans+=a[1]+a[2]+a[3];
}
printf("%d\n",ans);
}
return 0;
}

Crossing River 题解(贪心)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 702:Crossing River (贪心)

    [题目描述] N个人过河,一次过去2个回来一个,给出每个人所需时间,问最小过河时间. [题目链接] http://noi.openjudge.cn/ch0406/702/ [算法] 一开始想样例是怎么 ...

  4. Crossing River poj1700贪心

    题目描述:N个人过河,只有一只船,最多只能有两人划船,每个人划船速度不同,船速为最慢的人的速度.输入T为case个数,每个case输入N为人数,接下来一行输入的是每个人过河的时间,都不相同.要求输出N ...

  5. POJ 1700 Crossing River (贪心)

    Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9585 Accepted: 3622 Descri ...

  6. poj 1700 Crossing River 过河问题。贪心

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9887   Accepted: 3737 De ...

  7. Crossing River

    Crossing River 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26251 题意: N个人希望去过 ...

  8. Crossing River(1700poj)

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9919   Accepted: 3752 De ...

  9. poj1700--贪心--Crossing River

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12260   Accepted: 4641 D ...

随机推荐

  1. 【转】Hello SDL: Your First Graphics Window

    FROM: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php Hello SDL: Your First Graphics Window ...

  2. Object of type Decimal is not JSON serializable

    json遇到Decimal 型数据无法正确处理 解决方案 import json result = [ {'name': '小红', 'age': 26, 'balance': decimal.Dec ...

  3. 由python工作区导致的python代码能运行,但是PyCharm画红线的问题

    原文:https://www.zhihu.com/question/63028700 PyCharm在遇到模块找不到时,会使用红色波浪线提醒开发者. Python有一个工作区的概念,在默认情况下,当你 ...

  4. Flink的DataSource三部曲之二:内置connector

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  5. filter 请求过程中过滤链组装和调用解析

    自定义filter需要先下载依赖包 首先过滤链不是责任链的设计模式,因为一个request可以被链条上的多个filter处理.但是,对于责任链来说,一个request只能被链条中的一个handler处 ...

  6. xshell断线解决办法

    困扰了好几个月了,网上更改ssh配置文件.固定ip都不好使,终于找到xshell掉线的解决办法了 哈哈!!! 设备管理器-->网络适配器-->对应网络连接虚拟机方式,本机虚拟机桥接如图 右 ...

  7. leetcode128-generate-parentheses

    题目描述 给出n对括号,请编写一个函数来生成所有的由n对括号组成的合法组合. 例如,给出n=3,解集为: "((()))", "(()())", "( ...

  8. python爬虫构建代理ip池抓取数据库的示例代码

    爬虫的小伙伴,肯定经常遇到ip被封的情况,而现在网络上的代理ip免费的已经很难找了,那么现在就用python的requests库从爬取代理ip,创建一个ip代理池,以备使用. 本代码包括ip的爬取,检 ...

  9. 学习笔记——ESP8266项目的例子编译时发生cannot find -lstdc++问题的解决

    在尝试对进行ESP8266项目的例子进行编译时发生cannot find -lstdc++问题 第一想法是安装libstdc++,结果安装时又发生了下面的情况: 再次查找原因,最后发现当前安装的交叉编 ...

  10. Apache Shiro (Shiro-550)(cve_2016_4437)远程代码执行 - 漏洞复现

    0x00 漏洞原理 Apache Shiro框架提供了记住密码的功能(RememberMe),用户登录成功后会生成经过加密并编码的cookie.在服务端对rememberMe的cookie值, 先ba ...