Skip to content

Instantly share code, notes, and snippets.

@sugumura
Created October 1, 2011 11:10
Show Gist options
  • Save sugumura/1255894 to your computer and use it in GitHub Desktop.
Save sugumura/1255894 to your computer and use it in GitHub Desktop.
Google Code Jam 2011 A
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class A {
public static void main(String[] args) throws IOException {
String inputFileName = "input-file.in";
File inputFile = new File(inputFileName);
FileInputStream fis = new FileInputStream(inputFile);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
BufferedWriter out = new BufferedWriter(new FileWriter("resource/a.out"));
try {
String msg;
int q = Integer.parseInt(br.readLine());
int count = 1;
while ((msg = br.readLine()) != null) {
String[] split = msg.split(" ");
CardShuffle cs = new CardShuffle(Integer.parseInt(split[0]),
Integer.parseInt(split[1]), Integer.parseInt(split[2]));
for (int i = 0; i < cs.c; i++) {
msg = br.readLine();
String[] split2 = msg.split(" ");
cs.cut(Integer.parseInt(split2[0]),
Integer.parseInt(split2[1]));
}
out.write("Case #" + (count++) + ": " + (cs.getCard()));
out.newLine();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
br.close();
out.close();
}
}
}
import java.util.ArrayList;
public class CardShuffle {
ArrayList<Integer> card;
int m;
int c;
int w;
public CardShuffle(int m, int c, int w) {
this.card = new ArrayList<Integer>(m);
for (int i = 1; i <= m; i++) { // カード初期化
card.add(i);
}
this.m = m;
this.c = c;
this.w = w;
}
public int getCard() {
return card.get(w - 1);
}
public void cut(int ai, int bi) {
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = ai; i <= (ai + bi - 1); i++) {
al.add(card.get(i - 1));
}
if (card.size() != 1) {
card.subList(ai-1, (ai + bi - 1)).clear();
}
al.addAll(card);
card = al;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment