CSES - Datatähti 2016 alku - Results
Submission details
Task:Bittipeli
Sender:Perdex
Submission time:2015-10-08 18:02:44 +0300
Language:Java
Status:COMPILE ERROR

Compiler report

input/Bittipeli.java:13: error: cannot find symbol
        IO io = new IO();
        ^
  symbol:   class IO
  location: class Bittipeli
input/Bittipeli.java:13: error: cannot find symbol
        IO io = new IO();
                    ^
  symbol:   class IO
  location: class Bittipeli
Note: input/Bittipeli.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

Code

package datatahti16;

import java.util.ArrayList;

public class Bittipeli{
    
    private static boolean solved = false;
    private static final ArrayList<Integer> ans = new ArrayList();
    private static final ArrayList<Boolean> bits = new ArrayList();
    private static int trueTotal;
    
    public static void main(String[] args){
        IO io = new IO();
        
        char[] c = io.next().toCharArray();
        
        boolean currentBool = c[0] == '1';
        int count = 1;
        
        boolean trueFound = false;
        int falseCount = 0, mostFalses = 0;
        trueTotal = 0;
        
        for(int i = 1; i < c.length; i++){
            if((c[i] == '1') == currentBool){
                count++;
            }else{
                currentBool = !currentBool;
                
                if(count != 1){
                    trueTotal++;
                    falseCount = 0;
                }else{
                    falseCount++;
                    mostFalses = Math.max(mostFalses, falseCount);
                }
                
                bits.add(count != 1);
                
                if(!trueFound && count != 1){
                    trueFound = true;
                    firstTrue = bits.size() - 1;
                }
                
                count = 1;
            }
        }
        if(!trueFound)
            firstTrue = bits.size();
        bits.add(count != 1);
        
        
        //bits initialized
        
        if(mostFalses < (double)bits.size()/2)
            take();
       
        if(ans.isEmpty()){
            io.print("QAQ");
        }else{
            io.println(ans.size());
            for(int i: ans)
                io.print(i + " ");
        }
        
        io.close();
    }
    
    static int firstTrue;
    
    private static void take(){
        
        if(firstTrue > (double)bits.size()/2)
            return;
        
        if(bits.size() == 1){
            if(bits.get(0)){
                ans.add(1);
                solved = true;
            }
            return;
        }
        
        int counter = 0;
        boolean firstFound = false;
        
        for(int i = firstTrue; i < bits.size() && !solved; i++){
            
            if(bits.get(i)){
                
                if(!firstFound){
                    firstTrue = Math.max(0, i - 1);
                    firstFound = true;
                }
                
                counter++;
                
                if((i == 0 && !bits.get(1)) || (i == bits.size() - 1 && !bits.get(bits.size()-2))){
                    bits.remove(i);
                    trueTotal--;
                    
                    ans.add(counter);
                    take();
                    if(!solved){
                        ans.remove(ans.size()-1);
                        bits.add(i, true);
                        trueTotal++;
                    }
                }else if(i != 0 && i != bits.size() - 1 && (!bits.get(i-1) || !bits.get(i+1))){
                    //took from middle
                    
                    boolean first = bits.get(i-1), second = bits.get(i+1);
                    bits.remove(i);
                    bits.remove(i);
                    bits.set(i-1, true);
                    
                    if(first || second)
                        trueTotal--;
                    
                    ans.add(counter);
                    take();
                    if(!solved){
                        ans.remove(ans.size()-1);

                        bits.set(i-1, first);
                        bits.add(i, true);
                        bits.add(i+1, second);
                        
                        if(first || second)
                            trueTotal++;
                    }
                }
            }
            
        }
        
        if(!solved && !(trueTotal > (double)bits.size() / 2)){
            int falseCounter = firstTrue;
            for(int i = firstTrue; i - falseCounter < (double)bits.size()/2; i++){
                if(bits.get(i)){
                    falseCounter = 0;
                }else{
                    falseCounter++;
                    if(falseCounter > (double)bits.size()/2)
                        return;
                }
            }
        }
        
        counter = 0;
        
        for(int i = firstTrue; i < bits.size() && !solved; i++){
            
            if(bits.get(i)){
                
                counter++;
                
                if((i == 0 && bits.get(1)) || (i == bits.size() - 1 && bits.get(bits.size()-2))){
                    bits.remove(i);
                    trueTotal--;
                    
                    ans.add(counter);
                    take();
                    if(!solved){
                        ans.remove(ans.size()-1);
                        bits.add(i, true);
                        trueTotal++;
                    }
                }else if(i != 0 && i != bits.size() - 1 && bits.get(i-1) && bits.get(i+1)){
                    //took from middle
                    
                    boolean first = bits.get(i-1), second = bits.get(i+1);
                    bits.remove(i);
                    bits.remove(i);
                    bits.set(i-1, true);
                    
                    if(first)
                        trueTotal--;
                    if(second)
                        trueTotal--;
                    
                    ans.add(counter);
                    take();
                    if(!solved){
                        ans.remove(ans.size()-1);

                        bits.set(i-1, first);
                        bits.add(i, true);
                        bits.add(i+1, second);
                        
                        if(first)
                            trueTotal++;
                        if(second)
                            trueTotal++;
                    }
                }
            }
            
        }
        
    }//take
}