过河问题(POJ 1700)

http://www.cppblog.com/abilitytao/archive/2009/03/28/78197.html

有点不明白,为什么一定要两个两个的,存在一个最快的带动,不是也可以的么,再深度理解。。。

解题思路:
当人数等于1,2,3的时候:答案很容易得出;
当人数大于等于4时:

若设过桥速度最快的那个人过桥时间为a,第二快为b;过桥第二慢的那个人过桥时间为y,最慢为z;
此时有两种过桥方案:
一.最快和次快的人先过,然后最快的回来,然后最慢与次慢的人再过,次快的回来;
二.最快的和最慢的过,快的回来,在和次慢的过,快的再回来;

第一种方法时间为b*2+a+z (+ b)
第二种方法时间为y+z+2*a (+ b)
如果第一种大于第二种 有2*b+a+z>y+z+2*a
化简得
2*b>y+a;
此时只要比较2*b和a+y的大小即可知道那种方法更优 O(∩_∩)O~ 编程解决即可

import java.io.*;
import java.util.*;
import java.math.*;

public classMain {
	
	int t, n, a[];
	
	void run() {
		t = cin.nextInt();
		while (t-- > 0) {
			n = cin.nextInt();
			a = new int[n + 1];
			for (int i = 1; i <= n; i++) 
				a[i] = cin.nextInt();
			int ans = 0;
			while (n > 0) {
				if (n == 1) {
					ans += a[1];
					break;
				} else if (n == 2) {
					ans += a[2];
					break;
				} else if (n == 3) {
					ans += a[1] + a[2] + a[3];
					break;
				} else if (n >= 4) {
					if (2 * a[2] < a[1] + a[n - 1]) {
						ans += 2 * a[2] + a[1] + a[n];
					} else {
						ans += 2 * a[1] + a[n - 1] + a[n];
					}
					n -= 2;
				}
			}
			System.out.println(ans);
		}
	}               
	
	public static void main(String[] args) {
		Mainsolved = new Main();
		solved.run();
	}
	
	static InputStreaminputStream = System.in;
	static InputReadercin = new InputReader(inputStream);
	
//	Scanner cin = new Scanner(new BufferedInputStream(System.in));
	
}


classInputReader { 

    private InputStreamstream; 
    private byte[] buf = new byte[1024]; 
    private int curChar; 
    private int numChars; 

    public InputReader(InputStreamstream) { 
        this.stream = stream; 
    } 

    public int read() { 
        if (numChars == -1) 
            return -1; 
            //throw new InputMismatchException(); 
        if (curChar >= numChars) { 
            curChar = 0; 
            try { 
                numChars = stream.read(buf); 
            } catch (IOExceptione) { 
                throw new InputMismatchException(); 
            } 
            if (numChars <= 0) 
                return -1; 
        } 
        return buf[curChar++]; 
    } 

    public int nextInt() { 
        int c = read(); 
        if (c == -1) 
            return -1; 
        while (isSpaceChar(c)) 
            c = read(); 
        int sgn = 1; 
        if (c == '-') { 
            sgn = -1; 
            c = read(); 
        } 
        int res = 0; 
        do { 
            if (c < '0' || c > '9') 
                throw new InputMismatchException(); 
            res *= 10; 
            res += c - '0'; 
            c = read(); 
        } while (!isSpaceChar(c)); 
        return res * sgn; 
    } 
     
    public Stringnext() {   
        StringBuilderstr = new StringBuilder();   
        int ch;   
        while (isSpaceChar(ch = read()));   
        if (ch == -1)   
            return null;   
        do {   
            str.appendCodePoint(ch);   
        } while (!isSpaceChar(ch = read()));   
        return str.toString();   
    }  
     
    public static boolean isSpaceChar(int c) { 
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; 
    } 

    public char nextCharacter() { 
        int c = read(); 
        while (isSpaceChar(c)) 
            c = read(); 
        return (char) c; 
    } 

} 

posted on 2013-02-21 22:45  Sure_Yi  阅读(219)  评论(0编辑  收藏  举报

导航