GCJ Round2 B题 Deterministic方法实现

虽然用随机化方法可以很容易过此题,代码实现也很方便,但还是写了一个Deterministic Solution的方法,具体的实现方案看GCJ官方的analysis

public class BProblem {
	
	private class Circle implements Comparable<Circle>{
		public long R;
		public int ind;
		@Override
		public int compareTo(Circle o) {
			// TODO Auto-generated method stub
			return this.R > o.R ? -1:1;
		}
		
	}
	
	private Circle[] circles;
	private long[][] queue;
	private int head,tail;
	private long[][] res;
	
	public void run(){
		try {
			System.setIn(new BufferedInputStream(new FileInputStream("B-large-practice.in")));
			System.setOut(new PrintStream(new File("data.out")));
			Scanner cin = new Scanner(System.in);
			int t;
			t = cin.nextInt();
			int ca ;
			for(ca = 1;ca<=t;ca++){
				int n;
				n = cin.nextInt();
				long L,W;
				L = cin.nextLong();
				W = cin.nextLong();
				circles = new Circle[n];
				int i;
				for(i=0;i<n;i++){
					circles[i] = new Circle();
					circles[i].R = cin.nextLong();
					circles[i].ind = i;
				}
				Arrays.sort(circles);
				res = new long[n][2];
				queue = new long[n+5][3];
				head=tail=0;
				queue[tail][0] = 0;
				queue[tail][1] = L;
				queue[tail++][2] = 0;
				long offset=0;
				boolean pop = true;
				int num = 0;
				long l=0,r=0,h=0;
				while(num < n){
					if(pop){
						pop = false;
						l=queue[head][0];
						r= queue[head][1];
						h=queue[head][2];
						head++;
					}
					if(offset == 0 && l != 0){
						pop = true;
						continue;
					}
					if(offset >= r){
						pop = true;
						continue;
					}
					res[circles[num].ind][0] = offset;
					res[circles[num].ind][1] = h;
					num++;
					if(h+circles[num-1].R*2 <= W){
						queue[tail][0] = offset;
						queue[tail][1] = (offset+circles[num-1].R*2) < L ? (offset+circles[num-1].R*2) : L;
						queue[tail++][2] = h + circles[num-1].R*2;
					}
					offset += circles[num-1].R*2;
					if(num==n) break;
					if(offset >= r){
						pop = true;
						if(offset >= L) 
							offset = 0;
					}
				}
				System.out.print("Case #"+ca+":");
				for(i=0;i<n;i++)
					System.out.print(" "+res[i][0]+" "+res[i][1]);
				System.out.println();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new BProblem().run();
	}

}


你可能感兴趣的:(exception,String,File,Class,ini)