Hatena::Grouptopcoder

TopCoderの問題を解く

解いた問題の一覧表

2009-08-30

InstantRunoff

| 17:56

問題文, SRM 175

選挙で、誰が過半数の票を獲得するか。

166.88/550 (1 failed)

class InstantRunoff {
public:
    string outcome(string candidates, vector <string> ballots) {
        const int numCandidates = candidates.size();
        const int numVoters = ballots.size();
        vector<int> idx(numVoters, 0);
        vector<char> failed;
        for (int i = 0; i < numCandidates; ) {
            map<char,int> votes;
            for (int k = 0; k < numCandidates; k++) 
                if (find(failed.begin(),failed.end(),candidates[k]) == failed.end())
                    votes[candidates[k]] = 0;
            for (int j = 0; j < numVoters; j++) {
                while (votes.find(ballots[j][idx[j]]) == votes.end()) idx[j]++;
                votes[ballots[j][idx[j]]]++;
            }
            int minimum = INT_MAX;
            for (map<char,int>::const_iterator v = votes.begin();
                    v != votes.end(); v++) {
                if (v->second > numVoters/2)
                    return string(1,v->first);
                if (v->second < minimum)
                    minimum = v->second;
            }
            for (map<char,int>::const_iterator v = votes.begin();
                    v != votes.end(); v++) {
                if (v->second == minimum) {
                    failed.push_back(v->first);
                    i++;
                    for (int j = 0; j < numVoters; j++) {
                        if (v->first == ballots[j][idx[j]])
                            idx[j]++;
                    }
                }
            }
        }
        return "";
    }
};

Matching

| 16:40

問題文, SRM 176

全て同じか、全て違う。

347.03/500

const static string memory[4][3] = {
    { "CIRCLE", "DIAMOND", "SQUIGGLE" },
    { "BLUE", "GREEN", "RED" },
    { "EMPTY", "SOLID", "STRIPED" },
    { "ONE", "THREE", "TWO" } };
    
class Matching {
public:
    vector <string> findMatch(vector <string> first, vector <string> second) {
        vector<string> third(4);
        for (int i = 0; i < 4; i++) 
            third[i] = getSymbol(first[i], second[i], i);
        return third;
    }
private:
    string getSymbol(string f, string s, const int kind) {
        if (f == s) return f;
        if (f > s) swap(f, s);
        if (f == memory[kind][0]) {
            if (s == memory[kind][1]) return memory[kind][2];
            else return memory[kind][1];
        } else {
            return memory[kind][0];
        }
    }
};

RGBColor

| 15:54

問題文, SRM 176

補色を返す。

224.33/250

class RGBColor {
public:
    vector <int> getComplement(vector <int> rgb) {
        vector <int> result(3);
        result[0] = 255 - rgb[0];
        result[1] = 255 - rgb[1];
        result[2] = 255 - rgb[2];
        if (abs(rgb[0]-result[0])<=32 && abs(rgb[1]-result[1])<=32 
                && abs(rgb[2]-result[2])<=32) {
            result[0] = (rgb[0]+128 <= 255) ? rgb[0]+128: rgb[0]-128;
            result[1] = (rgb[1]+128 <= 255) ? rgb[1]+128: rgb[1]-128;
            result[2] = (rgb[2]+128 <= 255) ? rgb[2]+128: rgb[2]-128;
        }
        return result;
    }
};

BadClock

| 15:23

問題文, SRM 172

何時間後に2つの時計が同時刻を指すか。

class BadClock {
public:
    double nextAgreement(string trueTime, string skewTime, int hourlyGain) {
        int X = getTime(trueTime);
        int Y = getTime(skewTime);
        if (hourlyGain < 0) {
            hourlyGain = -hourlyGain;
            swap(X, Y);
        }
        while (X < Y) X += 12*60*60;
        return (double) (X - Y) / hourlyGain;
    }
private:
    int getTime(const string& time) {
        int hh, mm, ss;
        sscanf(time.c_str(), "%d:%d:%d", &hh, &mm, &ss);
        return (hh*60 + mm)*60 + ss;
    }
};

Table

| 14:44

問題文, SRM 157

テーブルの形式を変換。

366.23/500

class Table {
public:
    vector <string> layout(vector <string> tbl) {
        const int rows = tbl.size();
        vector <string> result(rows, string(51, '?'));
        for (int r = 0; r < rows; r++) {
            istringstream iss(tbl[r]);
            int colspan, rowspan;
            char content, pad;
            int c = 0;
            while (iss >> pad >> colspan >> pad >> rowspan 
                     >> pad >> content >> pad) {
                while (result[r][c] != '?') c++;
                for (int rr = r; rr < r+rowspan; rr++)
                    for (int cc = c; cc < c+colspan; cc++)
                        result[rr][cc] = content;
                c = c + colspan;
            }
        }
        const int cols = result[0].find("?");
        for (int r = 0; r < rows; r++)
            result[r] = result[r].substr(0, cols);
        return result;
    }
};

MineField

| 11:43

問題文, SRM 169

マインスイーパ。

225.69/250

class MineField {
public:
    vector <string> getMineField(string mineLocations) {
        const static int SIZE = 9;
        vector <string> result(SIZE,string(SIZE,'0'));
        istringstream iss(mineLocations);
        char pad;
        int y, x;
        while (iss >> pad >> y >> pad >> x >> pad)
            result[y][x] = 'M';
        const static int d[8][2] = {
            {-1,-1}, {-1, 0}, {-1, 1},
            { 0,-1},          { 0, 1},
            { 1,-1}, { 1, 0}, { 1, 1} };
        for (y = 0; y < SIZE; y++)
            for (x = 0; x < SIZE; x++) {
                if (result[y][x] == 'M') continue;
                int counter = 0;
                for (int i = 0; i < 8; i++) {
                    int dy = y + d[i][0];
                    int dx = x + d[i][1];
                    if (0<=dy&&dy<SIZE && 0<=dx&&dx<SIZE 
                            && result[dy][dx] == 'M')
                        counter++;
                }
                result[y][x] += counter;
            }
        return result;
    }
};

UserId

| 11:21

問題文, SRM 164

ユーザIDを決める。

207.86/300

class UserId {
public:
    string id(vector <string> inUse, string first, string middle, string last) {
        first = truncate(first);
        middle = truncate(middle);
        last = truncate(last);
        if ((first == "BAD DATA" || middle == "BAD DATA" || last == "BAD DATA") ||
                first.length() < 2 || last.length() < 2)
            return "BAD DATA";

        string candidate = first[0] + last;
        if (candidate.length() > 8) candidate = candidate.substr(0,8);
        if (find(inUse.begin(),inUse.end(),candidate) == inUse.end())
            return candidate;
        if (middle.length() >= 1) {
            candidate = first[0] + string(1,middle[0]) + last;
            if (candidate.length() > 8) candidate = candidate.substr(0,8);
            if (find(inUse.begin(),inUse.end(),candidate) == inUse.end())
                return candidate;
        }
        for (int i = 1; i <= 99; i++) {
            candidate = first[0] + last;
            if (candidate.length() > 6) candidate = candidate.substr(0,6);
            ostringstream oss;
            oss << candidate << setw(2) << setfill('0') << i;
            candidate = oss.str();
            if (find(inUse.begin(),inUse.end(),candidate) == inUse.end())
                return candidate;
        }
        return "BAD DATA";
    }
private:
    string truncate(const string& s) {
        string ret;
        for (int i = 0; i < s.length(); i++) {
            if (isalpha(s[i])) ret += tolower(s[i]);
            else if (s[i] != '\'' && s[i] != ' ') return "BAD DATA";
        }
        return ret;
    }
};