Skip to content

Instantly share code, notes, and snippets.

@ongspxm
Created April 1, 2018 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ongspxm/728f900ac50d9c5dbe15fb7220bce02d to your computer and use it in GitHub Desktop.
Save ongspxm/728f900ac50d9c5dbe15fb7220bce02d to your computer and use it in GitHub Desktop.
2018 codejam practice
import java.util.*;
class Solution{
void run(){
Scanner cin = new Scanner(System.in);
int T = cin.nextInt();
for(int t=0; t<T; t++){
int N = cin.nextInt(),
K = cin.nextInt();
int base = 1;
int level = 1;
while(K>base){
K -= base;
base *= 2;
level += 1;
}
Stack<Integer> stack = new Stack<>();
for(int l=0; l<level; l++){
if(K>(base/2)){
K -= base/2;
stack.push(1);
}else{
stack.push(0);
}
base /= 2;
}
int a=N, b=N;
while(!stack.isEmpty()){
int left = a-1;
if(stack.pop()==1){
left = b-1;
}
a=(left+1)/2;
b=left/2;
}
System.out.printf("Case #%d: %d %d\n", t+1, a, b);
}
cin.close();
}
public static void main(String[] args){
Solution sol = new Solution();
sol.run();
}
}
import java.util.*;
class Solution{
class Party implements Comparable<Party>{
int cnt;
char party;
Party(int cnt, char party){
this.cnt = cnt;
this.party = party;
}
@Override
public int compareTo(Party party){
int test = this.cnt - party.cnt;
if(test==0){
return this.party - party.party;
}
return test;
}
public String toString(){
return this.party+" "+this.cnt;
}
}
void run(){
Scanner cin = new Scanner(System.in);
char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
int T = cin.nextInt();
for(int t=0; t<T; t++){
int N = cin.nextInt();
int total = 0;
String ans = "";
PriorityQueue<Party> parties = new PriorityQueue<>(Comparator.reverseOrder());
for(int n=0; n<N; n++){
int size = cin.nextInt();
total += size;
parties.offer(new Party(size, alphabet[n]));
}
while(total>0){
Party test = parties.poll();
test.cnt -= 1; total -= 1;
ans += test.party;
if(test.cnt>0) parties.offer(test);
if(parties.size()!=2 || total%2==1){
test = parties.poll();
test.cnt -= 1; total -= 1;
ans += test.party;
if(test.cnt>0) parties.offer(test);
}
if(total>0){
ans += " ";
}
}
System.out.printf("Case #%d: %s\n", t+1, ans);
}
cin.close();
}
public static void main(String[] args){
Solution sol = new Solution();
sol.run();
}
}
import java.util.*;
class Solution{
void run(){
Scanner cin = new Scanner(System.in);
int T = cin.nextInt();
for(int t=0; t<T; t++){
int D = cin.nextInt(),
N = cin.nextInt();
float max=-1;
for(int n=0; n<N; n++){
int K = cin.nextInt(),
S = cin.nextInt();
float test = D-K;
test /= S;
if(max<0 || test>max) max=test;
}
System.out.printf("Case #%d: %6f\n", t+1, D/max);
}
cin.close();
}
public static void main(String[] args){
Solution sol = new Solution();
sol.run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment