http://poj.org/problem?id=1700

题目大意:

有n个人要过坐船过河,每一个人划船有个时间a[i],每次最多两个人坐一条船过河。且过河时间为两个人中速度慢的,求n个人过河的最短时间。

思路:

贪心。

对于每次过河的,有两种情况:

//最快和最慢过去,然后最快回来。在和次慢过去。最快回来
int action1=a[i-1] + a[0] + a[i-2] +a[0];
//最快和次慢过去,然后最快回来,在次慢和最慢过去,次慢回来
int action2=a[1] +a[0] + a[i-1] +a[1];

每次取最优就可以。

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=1024;
int a[MAXN];
int main()
{
int T;
scanf("%d",&T);
while((T--)!=0)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]); sort(a,a+n); int i;
int ans=0;
for(i=n;i>=4;i-=2)
{
//最快和最慢过去,然后最快回来,在和次慢过去,最快回来
int action1=a[i-1] + a[0] + a[i-2] +a[0]; //最快和次慢过去,然后最快回来,在次慢和最慢过去。次慢回来
int action2=a[1] +a[0] + a[i-1] +a[1];
ans=ans+ ( (action1 < action2 )? action1:action2);
}
if(i==3)
ans+= a[0]+a[1]+a[2];
if(i==2)
ans+=a[1];
if(i==1)
ans+=a[0];
printf("%d\n",ans);
}
return 0;
}

import java.util.Arrays;
import java.util.Scanner; public class Main { public static int[] a=new int[1024]; public static void main(String[] args) {
int T;
Scanner in=new Scanner(System.in);
T=in.nextInt();
while((T--)!=0)
{
int n=in.nextInt();
for(int i=0;i<n;i++)
a[i]=in.nextInt(); Arrays.sort(a,0,n); int i;
int ans=0;
for(i=n;i>=4;i-=2)
{
//最快和最慢过去,然后最快回来,在和次慢过去。最快回来
int action1=a[i-1] + a[0] + a[i-2] +a[0]; //最快和次慢过去。然后最快回来,在次慢和最慢过去,次慢回来
int action2=a[1] +a[0] + a[i-1] +a[1];
ans=ans+ ( (action1 < action2 )? action1:action2);
}
if(i==3)
ans+= a[0]+a[1]+a[2];
if(i==2)
ans+=a[1];
if(i==1)
ans+=a[0];
System.out.println(ans);
} } }

poj 1700 Crossing River C++/Java的更多相关文章

  1. POJ 1700 Crossing River (贪心)

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

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

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

  3. POJ 1700 - Crossing River

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13982   Accepted: 5349 Description A gr ...

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

  5. POJ 1700 cross river (数学模拟)

                                                                                                       ...

  6. 1700 Crossing River

    题目链接: http://poj.org/problem?id=1700 1. 当1个人时: 直接过河 t[0]. 2. 当2个人时: 时间为较慢的那个 t[1]. 3. 当3个人时: 时间为 t[0 ...

  7. Crossing River(1700poj)

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

  8. Crossing River

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

  9. poj1700--贪心--Crossing River

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

随机推荐

  1. 【Android】OAuth验证和新浪微博的oauth实现

    关于OAuth验证 OAuth是当下流行的授权方案,twitter,facebook,google等大型网站的开放平台都支持了oauth验证模式,国内的新浪微博.腾讯微博.163微博的开放平台也相继支 ...

  2. User-Defined-Literal自定义字面量

    c++支持多种内置类型的字面量,比如: 123u // unsigned int 1.0 // double 1.6f // float 'a' // char 4ULL // unsigned lo ...

  3. SQLite区分大小写查询

    http://www.cnblogs.com/zhuawang/archive/2013/01/15/2861566.html 大部分数据库在进行字符串比较的时候,对大小写是不敏感的.但是,在SQLi ...

  4. html5+css3比较好的学习教程和demo效果

    1.css3 Animate.css 动画库--- http://daneden.github.io/animate.css/ 2.font-awesome 完美的图标字体,只为Bootstrap设计 ...

  5. source activate my_env 失败,source not found

    今天连接到服务器后,安装anaconda.虽然在安装过程中选择将anaconda加入到系统变量中去.而且在 ~/.bashrc 中确实有 export PATH="/home/xnh/ana ...

  6. 在构造函数中使用new时的注意事项

    果然,光看书是没用的,一编程序,很多问题就出现了-- 注意事项: 1. 如果构造函数中适用了new初始化指针成员,则构析函数中必须要用delete 2. new与delete必须兼容,new对应del ...

  7. JAVA-数据库之更新记录

    相关资料:<21天学通Java Web开发> 更新记录 StatementUpdate.jsp <%@ page language="java" contentT ...

  8. node.js官方文档chm电子书的制作

    制作软件:WebCHMSetup2.22.zip,http://www.onlinedown.net/soft/31553.htm 制作好的电子书:Node.js(v6.10.2).zip 参考链接: ...

  9. Oracle报错:ORA-06508: PL/SQL: 无法找到正在调用的程序单元

    原因:调用的存储过程已经被修改过. 解决方案:重新编译存储过程.

  10. poj3436 ACM Computer Factory, 最大流,输出路径

    POJ 3436 ACM Computer Factory 电脑公司生产电脑有N个机器.每一个机器单位时间产量为Qi. 电脑由P个部件组成,每一个机器工作时仅仅能把有某些部件的半成品电脑(或什么都没有 ...