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. bzoj1968: [Ahoi2005]COMMON 约数研究(数论)

    计算每一个数的贡献就好了..O(N) n/i只有2*sqrtn个取值于是可以优化到O(sqrtn) #include<bits/stdc++.h> #define ll long long ...

  2. bzoj2058: [Usaco2010 Nov]Cow Photographs(逆序对)

    题目大意:给出n个数的序列,每次可以交换相邻的两个数,问把序列变成编号i在编号i+1左边,编号1在编号n右边(一个环)最少需要多少步.如:35421最少交换两次变为34512. 一开始看到这题,只会O ...

  3. swift的UIlabel

    let label = UILabel(frame:CGRect(x:,y:,width:,height:)); label.text="i am a am a label am a lab ...

  4. Android提示框与通知的使用

    1.通知 Android 3.0以前使用的方法 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION ...

  5. ContentProvider学习

    1.创建类继承ContentProvider类,并实现增.删.改.查功能. public static final String AUTHORITY = "com.diysoul.lists ...

  6. acm1878欧拉回路

    欧拉回路解释 对于本题我们只要把每个点的度进行记录,判断是否存在奇数度的点,如果是就可以判断不是欧拉回路,如果不是就在一个点出发,进行dfs搜索, 看能否走到起点,因为对于欧拉回路是一个闭合的回路,无 ...

  7. 第一章 深入web请求过程

    B/S架构的的好处: 客户端使用统一的浏览器(browser).由于浏览器的统一性,它不需要特殊的配置和网络连接,有效的屏蔽了不同服务提供商提供给用户使用服务的差异性.另外一点是浏览器的交互特性使得用 ...

  8. javascript实现购物车思路

    /* 思路: 第一步:获取所要操作的节点对象 第二步:当页面加载完后,需要计算本地cookie存了多少[个]商品,把个数赋值给count 第三步:为每一个商品对应的添加购物车按钮绑定一个点击事件onc ...

  9. 更改本地hosts文件

    在 C:\Windows\System32\drivers\etc 下更改 hosts 文件 127.0.0.1 www.baidu.com 这样访问 www.baidu.com 这个地址,其实是访问 ...

  10. vector基础

    //STL基础 //容器 //vector #include "iostream" #include "cstdio" #include "vecto ...