Input
There’re several test cases. In each test case, in the first line is a single integer N (1 <= N <= 10 5) denoting the number of building’s floors. The following N lines specify the floors. Each of them contains two integers w i and s i (0 <= w i, s i <= 100000) separated by single spaces. Please process until EOF (End Of File).
Output
For each test case, your program should output a single integer in a single line - the minimal PDV of the whole building. If no floor would be damaged in a optimal configuration (that is, minimal PDV is non-positive) you should output 0.
Sample Input
3
10 6
2 3
5 4
2
2 2
2 2
3
10 3
2 5
3 3
Sample Output
1
0
2
交换两个板的位置
2)a'=sum+wj-si;b'=sum-sj;
如果1优于2,求解得有效的条件为wj-si>wi-sj
即wj+sj>wi+si
所以按si+wi的和排序贪心即可。
package lanqiaobei;
import java.util.*;
public class Main {
static Scanner in =new Scanner(System.in);
static Comparator<Floor> com=new Comparator<Floor>() {
@Override
public int compare(Floor o1, Floor o2) {
return (o1.s+o1.w)-(o2.s+o2.w);
}
};
public static void main(String[] args) {
while(in.hasNext()){
int n = in.nextInt();
int w,s;
List<Floor> st=new ArrayList<Floor>();
for (int i = 0; i < n; i++) {
w=in.nextInt();
s=in.nextInt();
Floor f = new Floor(w,s);
st.add(f);
}
Collections.sort(st,com);
int sum=0,max=0,t;
sum=st.get(0).w;
for (int i = 1; i <n; i++) {
t=sum-st.get(i).s;
if(t>max)
max=t;
sum+=st.get(i).w;
}
if(sum-st.get(st.size()-1).s>=0)
System.out.println(max);
else
System.out.println(0);
}
}
}
class Floor{
int w;
int s;
Floor(int w,int s){
this.w=w;
this.s=s;
}
}