Created
July 2, 2012 07:24
-
-
Save kunishi/3031663 to your computer and use it in GitHub Desktop.
ACM International Collegiate Programming Contest, Japan Domestic, 2004, Problem A
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.LinkedList; | |
/* | |
* A.java | |
* | |
* Created on 2005/06/09, 22:57 | |
* | |
* To change this template, choose Tools | Options and locate the template under | |
* the Source Creation and Management node. Right-click the template and choose | |
* Open. You can then make changes to the template in the Source Editor. | |
*/ | |
/** | |
* | |
* @author kunishi | |
*/ | |
public class A { | |
public int n; | |
public int r; | |
public LinkedList deck = null; | |
/** Creates a new instance of A */ | |
public A() { | |
} | |
public static void main(String[] args) { | |
A a = new A(); | |
a.simulate(args[0]); | |
// System.out.println(a.n); | |
// System.out.println(a.r); | |
} | |
public void simulate(String filename) { | |
int p, c; | |
BufferedReader br; | |
String s; | |
LinkedList cut; | |
try { | |
br = new BufferedReader(new FileReader(filename)); | |
s = br.readLine(); | |
while (!s.equals("0 0")) { | |
n = Integer.parseInt(s.substring(0, s.indexOf(" "))); | |
r = Integer.parseInt(s.substring(s.indexOf(" ")+1, s.length())); | |
// Initialize deck | |
deck = new LinkedList(); | |
for (int i = 1; i <= n; i ++) { | |
deck.addLast(new Integer(i)); | |
} | |
for (int k = 0; k < r; k ++) { | |
s = br.readLine(); | |
p = Integer.parseInt(s.substring(0, s.indexOf(' '))); | |
c = Integer.parseInt(s.substring(s.indexOf(' ')+1, s.length())); | |
cut = new LinkedList(); | |
for (int j = 0; j < c; j ++) { | |
cut.addLast(deck.remove(n-p-j)); | |
} | |
while (cut.size() != 0) { | |
deck.addLast(cut.removeLast()); | |
} | |
} | |
System.out.println(deck.getLast()); | |
s = br.readLine(); | |
} | |
} catch (FileNotFoundException e) { | |
System.out.println(e); | |
} catch (IOException e) { | |
System.out.println(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment