とんちゃんといっしょ

Cloudに関する技術とか日常とかについて書いたり書かなかったり

ケーキカット

Problem C
http://icpc.logos.ic.i.u-tokyo.ac.jp/icpc2007/contest/C_ja.html

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class C {
	Scanner sc = new Scanner(System.in);
	
	class Box implements Comparable<Box>{
		int x, y, large;
		
		Box(int w, int d){
			x = w;
			y = d;
			large = x * y;
		}
		
		public int compareTo(Box o) {
			return this.large - o.large;
		}
	}
	
	C(){
		for(;;){
			int n, w, d;
			n = sc.nextInt();
			w = sc.nextInt();
			d = sc.nextInt();
			if(w==0 && d==0) break;
			
			List<Box> list = new LinkedList<Box>();
			list.add(new Box(w,d));
			for(int i = 0; i < n; i++){
				int p, s;
				p = sc.nextInt();
				s = sc.nextInt();
				
				Box b1, b2;
				Box b = list.get(p-1);
				list.remove(p-1);
				
				for(;;){
					if(s - b.x < 0){
						b1 = new Box(s, b.y);
						b2 = new Box(b.x-s, b.y);
						break;
					}
					s -= b.x;
					
					if(s - b.y < 0){
						b1 = new Box(b.x, s);
						b2 = new Box(b.x, b.y - s);
						break;
					}
					s -= b.y;
				}
				
				if(b1.large < b2.large){
					list.add(p-1,b2);
					list.add(p-1,b1);
				}else{
					list.add(p-1,b1);
					list.add(p-1,b2);
				}
				
			}
			Collections.sort(list);
			for(int j = 0; j < list.size()-1; j++){
				System.out.print(list.get(j).large + " ");
			}
			System.out.println(list.get(list.size()-1).large);
		}
	}
	
	public static void main(String[] args) {
		new C();
	}
}