Skip to content

Instantly share code, notes, and snippets.

@kenkoooo
Created December 12, 2015 08:51
Show Gist options
  • Save kenkoooo/c7e9ffcd14b764c3a1f1 to your computer and use it in GitHub Desktop.
Save kenkoooo/c7e9ffcd14b764c3a1f1 to your computer and use it in GitHub Desktop.
CODE RUNNER 2015 決勝で出したコードです。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.PriorityQueue;
public class CompanyInfo {
static final String TOKEN = "IN5EA6ODQ6U25TRTZUUA1LAFCFSXZMOO";
static Employee[] employees;
public static void main(String[] args) throws IOException,
InterruptedException {
while (true) {
companyUpdate();
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
}
}
}
public static void companyUpdate() throws IOException, InterruptedException {
employees = new Employee[50];
ArrayDeque<Integer> input = companyUpdateQuery();
int money = input.poll();
System.out.println(money);
int employeesNum = input.poll();
int kindsNum = input.poll();
for (int i = 0; i < 50; i++) {
int id = input.poll();
int[] speed = new int[50];
for (int j = 0; j < speed.length; j++) {
speed[j] = input.poll();
}
employees[i] = new Employee(speed);
employees[i].update(id, input.poll(), input.poll());
}
// 割り当てていない仕事たち
int jobs = input.poll();
PriorityQueue<JobQue> jobQues = new PriorityQueue<>();
for (int i = 0; i < jobs; i++) {
int jobID = input.poll();
int time = input.poll();
int amount = input.poll();
int kind = input.poll();
int reward = input.poll();
int penalty = input.poll();
System.out.println(jobID + " " + time + " " + amount);
jobQues.add(new JobQue(jobID, amount, time, kind));
}
while (!jobQues.isEmpty()) {
JobQue que = jobQues.poll();
int sumSpeed = 0;
ArrayList<Integer> empIDs = new ArrayList<>();
for (int j = 0; j < employees.length; j++) {
if (employees[j].time > 0)
continue;
empIDs.add(employees[j].id);
employees[j].time = 10;
sumSpeed += employees[j].speed[que.kind];
}
if (empIDs.size() > 0 && sumSpeed * que.time >= que.amount) {
assign(empIDs, que.id);
Thread.sleep(200L);
System.out.println("assigned");
} else {
for (int empID : empIDs) {
employees[empID].time = 0;
}
System.out.println("not assigned");
}
}
int outside = input.poll();
for (int i = 0; i < outside; i++) {
int jobID = input.poll();
int time = input.poll();
int amount = input.poll();
int kind = input.poll();
int reward = input.poll();
int penalty = input.poll();
int payment = input.poll();
}
}
public static void assign(ArrayList<Integer> empIDs, int jobID)
throws IOException {
String urlString = "http://game.coderunner.jp/assign?task="
+ String.valueOf(jobID);
urlString = urlString + "&worker=";
for (int i = 0; i < empIDs.size(); i++) {
if (i > 0) {
urlString = urlString + ",";
}
urlString = urlString + String.valueOf(empIDs.get(i));
}
urlString = urlString + "&token=IN5EA6ODQ6U25TRTZUUA1LAFCFSXZMOO";
System.out.println(urlString);
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
}
}
public static ArrayDeque<Integer> companyUpdateQuery() throws IOException {
URL url = new URL(
"http://game.coderunner.jp/getinfo?token=IN5EA6ODQ6U25TRTZUUA1LAFCFSXZMOO");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStreamReader isr = new InputStreamReader(
connection.getInputStream(), StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(isr);
ArrayDeque<Integer> list = new ArrayDeque<>();
String line = null;
while ((line = reader.readLine()) != null) {
String[] lStrings = line.split(" ");
for (int i = 0; i < lStrings.length; i++) {
list.add(Integer.parseInt(lStrings[i]));
}
}
return list;
}
return new ArrayDeque<Integer>();
}
}
class JobQue implements Comparable<JobQue> {
int id, amount, time, kind;
public JobQue(int id, int amount, int time, int kind) {
this.id = id;
this.amount = amount;
this.time = time;
this.kind = kind;
}
@Override
public int compareTo(JobQue o) {
return this.amount - o.amount;
}
}
class EmpQue implements Comparable<EmpQue> {
int id, speed;
public EmpQue(int id, int speed) {
this.id = id;
this.speed = speed;
}
@Override
public int compareTo(EmpQue o) {
return o.speed - this.speed;
}
}
class Employee {
int id, time, power;
int[] speed = new int[50];
public Employee(int[] s) {
for (int i = 0; i < speed.length; i++) {
speed[i] = s[i];
}
}
public void update(int id, int time, int power) {
this.id = id;
this.time = time;
this.power = power;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.PriorityQueue;
public class LastRunner {
public static void main(String[] args) {
new LastRunner().solve();
}
public void solve() {
int cnt = 0;
while (true) {
try {
cnt += superCool();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
}
}
}
public ArrayDeque<Integer> getGetOut() throws Exception {
URL url = new URL(
"http://game.coderunner.jp/getout?token=IN5EA6ODQ6U25TRTZUUA1LAFCFSXZMOO");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStreamReader isr = new InputStreamReader(
connection.getInputStream(), StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(isr);
ArrayDeque<Integer> list = new ArrayDeque<>();
String line = null;
while ((line = reader.readLine()) != null) {
String[] lStrings = line.split(" ");
for (int i = 0; i < lStrings.length; i++) {
list.add(Integer.parseInt(lStrings[i]));
}
}
return list;
}
return new ArrayDeque<Integer>();
}
public int superCool() throws Exception {
Employee[] employees = new Employee[50];
ArrayDeque<Integer> info = companyUpdateQuery();
int money = info.poll();
System.out.println(money);
int employeesNum = info.poll();
int kindsNum = info.poll();
for (int i = 0; i < 50; i++) {
int id = info.poll();
int[] speed = new int[50];
for (int j = 0; j < speed.length; j++) {
speed[j] = info.poll();
}
employees[i] = new Employee(speed);
employees[i].update(id, info.poll(), info.poll());
}
int jobs = info.poll();
PriorityQueue<JobQue> jobQues = new PriorityQueue<>();
for (int i = 0; i < jobs; i++) {
int jobID = info.poll();
int time = info.poll();
int amount = info.poll();
int kind = info.poll();
int reward = info.poll();
int penalty = info.poll();
System.out.println(jobID + " " + time + " " + amount);
JobQue yabaiJobQue = new JobQue(jobID, amount, time, kind, reward);
jobQues.add(yabaiJobQue);
}
ArrayDeque<Integer> outQue = getGetOut();
int jobNum = outQue.poll();
for (int i = 0; i < jobNum; i++) {
int jobID = outQue.poll();
int leftTime = outQue.poll();
int amount = outQue.poll();
int jobKind = outQue.poll();
int reward = outQue.poll();
System.out.println(jobID + " " + leftTime + " " + amount + " "
+ jobKind + " " + reward);
jobQues.add(new JobQue(jobID, amount, leftTime, jobKind, reward));
}
int asscnt = 0;
while (!jobQues.isEmpty()) {
JobQue job = jobQues.poll();
System.out.println(job.cospa);
PriorityQueue<EmpQue> empQues = new PriorityQueue<>();
for (int j = 0; j < employees.length; j++) {
if (employees[j].time > 0)
continue;
empQues.add(new EmpQue(employees[j].id,
employees[j].speed[job.kind]));
}
int sumSpeed = 0;
ArrayList<Integer> empIDs = new ArrayList<>();
while (!empQues.isEmpty() && sumSpeed * job.time < job.amount) {
EmpQue emp = empQues.poll();
sumSpeed += emp.speed;
empIDs.add(emp.id);
employees[emp.id].time = 10;
}
if (empIDs.size() > 0 && sumSpeed * job.time >= job.amount
&& assign(empIDs, job.id) && job.time < 60) {
Thread.sleep(200L);
System.out.println("assigned");
asscnt++;
} else {
for (int empID : empIDs) {
employees[empID].time = 0;
}
System.out.println("not assigned");
}
}
if (asscnt == 0) {
}
return asscnt;
}
public boolean assign(ArrayList<Integer> empIDs, int jobID)
throws IOException {
String urlString = "http://game.coderunner.jp/assign?task="
+ String.valueOf(jobID);
urlString = urlString + "&worker=";
for (int i = 0; i < empIDs.size(); i++) {
if (i > 0) {
urlString = urlString + ",";
}
urlString = urlString + String.valueOf(empIDs.get(i));
}
urlString = urlString + "&token=IN5EA6ODQ6U25TRTZUUA1LAFCFSXZMOO";
System.out.println(urlString);
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
return true;
}
System.out.println("Fail!!!!!!");
return false;
}
public ArrayDeque<Integer> companyUpdateQuery() throws IOException {
URL url = new URL(
"http://game.coderunner.jp/getinfo?token=IN5EA6ODQ6U25TRTZUUA1LAFCFSXZMOO");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStreamReader isr = new InputStreamReader(
connection.getInputStream(), StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(isr);
ArrayDeque<Integer> list = new ArrayDeque<>();
String line = null;
while ((line = reader.readLine()) != null) {
String[] lStrings = line.split(" ");
for (int i = 0; i < lStrings.length; i++) {
list.add(Integer.parseInt(lStrings[i]));
}
}
return list;
}
return new ArrayDeque<Integer>();
}
}
class JobQue implements Comparable<JobQue> {
int id, amount, time, kind, reward;
double cospa;
public JobQue(int id, int amount, int time, int kind, int reward) {
this.id = id;
this.amount = amount;
this.time = time;
this.kind = kind;
this.reward = reward;
cospa = (double) reward / amount;
}
@Override
public int compareTo(JobQue o) {
if (o.cospa > this.cospa) {
return 1;
} else {
return -1;
}
}
}
class EmpQue implements Comparable<EmpQue> {
int id, speed;
public EmpQue(int id, int speed) {
this.id = id;
this.speed = speed;
}
@Override
public int compareTo(EmpQue o) {
return o.speed - this.speed;
}
}
class Employee {
int id, time, power;
int[] speed = new int[50];
public Employee(int[] s) {
for (int i = 0; i < speed.length; i++) {
speed[i] = s[i];
}
}
public void update(int id, int time, int power) {
this.id = id;
this.time = time;
this.power = power;
}
}
@kenkoooo
Copy link
Author

  • 前半はすぐ終わる仕事を受注しまくって社員全員に割り当てて教育しまくる。
  • 後半は外注の仕事をこなしまくる。
  • 受注は手動。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment