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. Codeforces 864E Fire(背包DP)

    背包DP,决策的时候记一下 jc[i][j]=1 表示第i个物品容量为j的时候要选,输出方案的时候倒推就好了 #include<iostream> #include<cstdlib& ...

  2. Codeforces VK Cup Finals #424 Div.1 C. Bamboo Partition(数论)

    题目要求符合以下条件的最大的d 化简得 注意到 最多只有2*sqrt(a[i]-1)种取值,也就是一共最多有n*sqrt(10^19)种取值,于是枚举一下d,计算出符合上上式的最大的d更新答案,然后d ...

  3. linux查看内存cpu占用

    linux查看内存cpu占用top 命令  按q退出 可以添加额外选项选择按进程或按用户查看如: top -u gitu PID:进程idPR:进程的优先级别,越小越优先被执行NInice:值VIRT ...

  4. UVA 1640 The Counting Problem

    https://vjudge.net/problem/UVA-1640 题意:统计区间[l,r]中0——9的出现次数 数位DP 注意删除前导0 #include<cmath> #inclu ...

  5. Enterprise Architect 13 : 需求建模 自动命名并计数

    如何给模型中的需求元素配置计数器以自动设置新创建元素的名称和别名: Configure -> Settings -> Auto Names and Counters 设置好后的效果图:

  6. HDFS不存在绝对路径,无法找到文件所在具体位置

    This is set in the dfs.datanode.data.dir property, which defaults to file://${hadoop.tmp.dir}/dfs/da ...

  7. quick-cocos2d-x数据存储 UserDefault GameState io

    看了quick-cocos2d-x 的framework,发现里面有一个GameState,查了下,是数据存储的类,于是稍稍总结下我用到过的数据存储方式吧. 一共是三种方法: cc.UserDefau ...

  8. 理解js中私有变量

    私有变量在js中是个什么概念.当下我的认识是var所定义的变量,实际可以理解为属性和方法,或者单单是临时存储器,不归属任何对象. 一个声明函数: function a(){  var v = &quo ...

  9. windows下安装python过程

    方法一:如果你的电脑没有安装python,推荐使用anaconda(自带python环境,同时自带各种第三方库,可以省去很多麻烦) 这里提供两个下载地址:1,.官网https://www.anacon ...

  10. Angular2.0 基础: 环境搭建

    最近在学习Angular2的使用,其实看过Angular2 文档的都知道,相比于之前的Angular1,Angular2 的改动还是挺大的. 而对于‘angular2 的本地开发环境的搭建的中,我们首 ...