/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codejam.common;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Lance Chen
 */
public class CodeHelper {

    private static String FILE_ROOT = "D:/workspace/googlecodejam/meta";

    public static List<List<String>> loadInputsExcludes(String fileName) {
        List<List<String>> result = new ArrayList<List<String>>();
        List<String> lines = FileUtil.readLines(FILE_ROOT + fileName);
        int index = 0;
        for (String line : lines) {
            if (index == 0) {
                index++;
                continue;
            }
            if (index % 2 == 1) {
                index++;
                continue;
            }
            List<String> dataList = new ArrayList<String>();
            String[] dataArray = line.split("\\s+");
            for (String data : dataArray) {
                if (data.trim().length() > 0) {
                    dataList.add(data);
                }
            }
            result.add(dataList);
            index++;
        }
        return result;
    }

    public static List<List<String>> loadInputs(String fileName) {
        List<List<String>> result = new ArrayList<List<String>>();
        List<String> lines = FileUtil.readLines(FILE_ROOT + fileName);
        for (String line : lines) {
            List<String> dataList = new ArrayList<String>();
            String[] dataArray = line.split("\\s+");
            for (String data : dataArray) {
                if (data.trim().length() > 0) {
                    dataList.add(data);
                }
            }
            result.add(dataList);
        }
        return result;
    }

    public static List<String> loadInputLines(String fileName) {
        return FileUtil.readLines(FILE_ROOT + fileName);
    }

    public static void writeOutputs(String fileName, List<String> lines) {
        FileUtil.writeLines(FILE_ROOT + fileName, lines);
    }
}

package com.menzus.gcj._2012.qualification.b;

import com.menzus.gcj.common.OutputProducer;

public class BOutputProducer implements OutputProducer<BInput, BOutputEntry> {

    @Override
    public BOutputEntry produceOutput(BInput input) {
	int avaragesAboveLimit = 0;
	int avaragesBelowLimitIfTheyWereSuprising = 0;
	for (int i = 0; i < input.getGooglerScores().length; i++) {
	    if (maximumNotSurprisingScoreOfTotalScore(input.getGooglerScores()[i]) >= input.getLimit()) {
		avaragesAboveLimit++;
	    } else if (maximumSurprisingScoreOfTotalScore(input.getGooglerScores()[i]) >= input.getLimit()) {
		avaragesBelowLimitIfTheyWereSuprising++;
	    }
	}
	BOutputEntry outputEntry = new BOutputEntry();
	outputEntry.setMaximumGooglersNumber(avaragesAboveLimit
		+ Math.min(avaragesBelowLimitIfTheyWereSuprising, input.getSurprisingTripletsNumber()));
	return outputEntry;
    }

    private int maximumNotSurprisingScoreOfTotalScore(int totalScore) {
	if (totalScore % 3 == 0) {
	    return totalScore / 3;
	} else {
	    return totalScore / 3 + 1;
	}
    }

    private int maximumSurprisingScoreOfTotalScore(int totalScore) {
	int maximumNotSurprisingScoreOfTotalScore = maximumNotSurprisingScoreOfTotalScore(totalScore);
	if (maximumNotSurprisingScoreOfTotalScore == 0) {
	    return maximumNotSurprisingScoreOfTotalScore;
	} else {
	    return maximumNotSurprisingScoreOfTotalScore + 1;
	}
    }
}
