Bridge over a rough river
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4143   Accepted: 1703

Description

A group of N travelers (1 ≤ N ≤ 50) has approached an old and shabby bridge and wishes to cross the river as soon as possible. However, there can be no more than two persons on the bridge at a time. Besides it's necessary to light the way with a torch for safe crossing but the group has only one torch.

Each traveler needs ti seconds to cross the river on the bridge; i=1, ... , N (ti are integers from 1 to 100). If two travelers are crossing together their crossing time is the time of the slowest traveler.

The task is to determine minimal crossing time for the whole group.

Input

The input consists of two lines: the first line contains the value of N and the second one contains the values of ti (separated by one or several spaces).

Output

The output contains one line with the result.

Sample Input

4
6 7 6 5

Sample Output

29

Source

Northeastern Europe 2001, Western Subregion
 
想不到的说....
题意:n个人过河,每次能过去两个人,这两个人带了手电筒,过了河之后需要有个人送回来,问所有人通过的最少时间.
 
题解:要是想成每个人过河的时候要时间最短的人送回来,然后耗时最短和耗时最长的一起回去那就错了,比如说:
四个人过桥花费的时间分别为 1 2 5 10,按照上面的想法答案是19,但是实际答案应该是17。
      

具体步骤是这样的:
      

第一步:1和2过去,花费时间2,然后1回来(花费时间1);
      

第二歩:3和4过去,花费时间10,然后2回来(花费时间2);
      第三部:1和2过去,花费时间2,总耗时17
 
贪心想法:对于第i个人,1.如果已经有i-1个人已经过了河,那么肯定是派时间最短的过来接他:dp[i] = dp[i-1]+a[1]+a[i];
2.如果已经有i-2个人已经过了河,那么第一步,先派耗时最短的来接他,然后给手电筒给a[i]和a[i-1],然后再派耗时次小的
人过来,耗时a[2],然后一起回去耗时a[2] dp[i] = dp[i-2]+a[i]+a[1]+2*a[2];
dp[i] = max(1,2)
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; int a[],dp[];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
for(int i=;i<=n;i++) scanf("%d",&a[i]);
memset(dp,,sizeof(dp));
sort(a+,a+n+);
dp[] = a[];
dp[] = a[];
for(int i=;i<=n;i++){
dp[i] = min(dp[i-]+a[i]+a[],dp[i-]+a[i]+*a[]+a[]);
}
printf("%d\n",dp[n]);
}
}

poj 1700(java大数据版):注意处理n=1的特殊情况

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner; public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int tcase= sc.nextInt();
while(tcase-->){
int n =sc.nextInt();
if(n==) {
int v = sc.nextInt();
System.out.println(v);
continue;
}
BigInteger [] a = new BigInteger[n+];
BigInteger [] dp = new BigInteger[n+];
for(int i=;i<=n;i++){
int v = sc.nextInt();
a[i] = BigInteger.valueOf(v);
}
Arrays.sort(a,,n+);
//for(int i=1;i<=n;i++) System.out.println(a[i]);
dp[] = a[];
dp[] = a[];
for(int i=;i<=n;i++){
BigInteger c1 = dp[i-].add(a[i]).add(a[]);
BigInteger c2 = dp[i-].add(a[]).add(a[].multiply(BigInteger.valueOf())).add(a[i]);
if(c1.compareTo(c2)>) dp[i] = c2;
else dp[i] = c1;
}
System.out.println(dp[n]);
}
} }

poj 3404&&poj1700(贪心)的更多相关文章

  1. POJ 1456(贪心)

    #include <string.h> #include <iostream> #include <queue> #include <stdio.h> ...

  2. poj -3614 Sunscreen(贪心 + 优先队列)

    http://poj.org/problem?id=3614 有c头奶牛在沙滩上晒太阳,每头奶牛能忍受的阳光强度有一个最大值(max_spf) 和最小值(min_spf),奶牛有L种防晒霜,每种可以固 ...

  3. POJ 3614 Sunscreen 贪心

    题目链接: http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MSMemory Limit: 65536K 问题描述 to avoid ...

  4. POJ 1456 - Supermarket - [贪心+小顶堆]

    题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarke ...

  5. poj 2287(贪心)

    Tian Ji -- The Horse Racing Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12490   Acc ...

  6. poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...

  7. poj 2431 Expedition 贪心

    简单的说说思路,如果一开始能够去到目的地那么当然不需要加油,否则肯定选择能够够着的油量最大的加油站加油,,不断重复这个贪心的策略即可. #include <iostream> #inclu ...

  8. poj 3154 Graveyard 贪心

    //poj 3154 //sep9 #include <iostream> #include <cmath> using namespace std; double a[204 ...

  9. Crossing River poj1700贪心

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

随机推荐

  1. Spring中@Resource与@Autowired、@Qualifier的用法与区别(转)

    1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...

  2. Just Random HDU - 4790 思维题(打表找规律)分段求解

    Coach Pang and Uncle Yang both love numbers. Every morning they play a game with number together. In ...

  3. python递归读取目录列表

    import os def listdirs(base): for line in os.listdir(base): fullpath = os.path.join(base,line) if os ...

  4. stout代码分析之十:c++11之move和forward

    stout中大量使用了c++11的特性,而c++11中move和forward大概是最神奇的特性了. 左值和右值的区别 ; // a是左值,0是右值 int b = rand(); // b是左值,r ...

  5. 一图看懂深度学习框架对比----Caffe Torch Theano TensorFlow

      Caffe Torch Theano TensorFlow Language C++, Python Lua Python Python Pretrained Yes ++ Yes ++ Yes ...

  6. 9.python爬虫--pyspider

    pyspider简介 PySpider:一个国人编写的强大的网络爬虫系统并带有强大的WebUI.采用Python语言编写,分布式架构,支持多种数据库后端,强大的WebUI支持脚本编辑器,任务监视器,项 ...

  7. 【HDU】5269 ZYB loves Xor I

    [算法]trie [题解] 为了让数据有序,求lowbit无法直接排序,从而考虑倒过来排序,然后数据就会呈现出明显的规律: 法一:将数字倒着贴在字典树上,则容易发现两数的lowbit就是它们岔道结点的 ...

  8. 苹果API常用英语名词---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 苹果API常用英语名词0. indicating 决定1.in order to 以便 ...

  9. NYOJ 1237 最大岛屿 (深搜)

    题目链接 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己的的战船黑珍珠1号要征服各个海岛的海盜,最后成为海盗王.  这是一个由海洋. ...

  10. idea 导入 java json 包

    1.java 项目导包 找到 External Libraries 下面的java版本包,在点击鼠标右键.直接找到jar路径全部选中导入即可.