poj 3045 Cow Acrobats

来源:互联网 发布:哪些女装淘宝店铺推荐 编辑:程序博客网 时间:2024/05/30 02:23

按照w+s排序然后计算

这个题目和动态规划中的叠乌龟一脉相承。

还有动态规划里的stacking box和stacking turtle是有很大区别的

这个证明其实蛮复杂的,回头写一下

想了好久,决定瞎逼把DP里叠乌龟的sorting方法用一下,居然过了,看来多刷题还是有用。

我不会证明但是我会A。。。

/*ID: daniel.20LANG: JAVATASK: fence8*/import java.io.*;import java.math.BigInteger;import java.util.*;class cow implements Comparable<cow>{    int w,s;    public cow(int a,int b){        w=a;s=b;    }    @Override    public int compareTo(cow t) {        return (this.w+this.s)-(t.w+t.s);    }}class problem{    int n;    cow[] arr;    void solver() throws IOException{        long start = System.currentTimeMillis();        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));        n = Integer.valueOf(reader.readLine());        arr = new cow[n];        for(int i=0;i<n;i++){            StringTokenizer st = new StringTokenizer(reader.readLine());            int t1 = Integer.valueOf(st.nextToken());            int t2 = Integer.valueOf(st.nextToken());            arr[i]=new cow(t1,t2);        }        Arrays.sort(arr);        long sum[] = new long[n];        sum[0] = arr[0].w;        long max = -arr[0].s;        for(int i=1;i<n;i++){            sum[i]=sum[i-1]+arr[i].w;            if(sum[i-1]-arr[i].s>max)max=sum[i-1]-arr[i].s;        }        //Dumper.print_1_arr(sum, n);        System.out.println(max);    } }public class fence8 {    public static void main (String [] args) throws Exception {        problem p = new problem();        p.solver();    }}



0 0
原创粉丝点击