Page List

Search on the blog

2011年1月12日水曜日

Hacker WorldCup 2011 -the qualification round-

I got the result of the qualification round of Hacker World Cup 2011, run by facebook, which determines who is the best hacker in the world.

It seems I'm qualified to advance to round 1, with the perfect score :D

The problems are kind of easy.
One can be solved just by using next_permutation() in STL.
Another can be solved by implementing just as it was described in the problem.
The other is a little difficult. It's DP problem, which is similar to the Pascal's Triangle.

Here, I'll post my solution to these problems.

Double Square:

int main() {
ifstream in(INPUT);
ofstream out(OUTPUT);

int n;
long long int x;
in >> n;

REP(i, n) {
in >> x;
int ret = 0;

for (long long int j = 0; j*j*2 <= x; j++) {
long long int y = x - j*j;
long long int y2 = sqrt(y) + .5;
if (y2 * y2 == y)
++ret;
}
out << ret << endl;
}

cout << "done." << endl;
return 0;
}


Peg Game:



double dp[256], dpt[256];
char bd[256][256];

void solve(int r, int c) {
REP(i, r) {
memset(dpt, 0, sizeof(dpt));
REP(j, 2*(c-1)+1) {
if (bd[i][j] == '.')
dpt[j] += dp[j];
else {
if (j - 1 < 0 + (i%2 != 0))
dpt[j+1] += dp[j];
else if (j + 1 >= 2*(c-1)+1-(i%2 != 0))
dpt[j-1] += dp[j];
else {
dpt[j-1] += dp[j] * .5;
dpt[j+1] += dp[j] * .5;
}
}
}
memcpy(dp, dpt, sizeof(dp));
}
}

int main() {
ifstream in(INPUT);
ofstream out(OUTPUT);

int n;
in >> n;

REP(i, n) {
int r, c, k, m;
in >> r >> c >> k >> m;

REP(ii, r) {
if (ii % 2) REP(jj, 2*(c-2)+1) {
bd[ii][jj] = (jj % 2) ? '.' : 'x';
}
else REP(jj, 2*(c-1)+1) {
bd[ii][jj] = (jj % 2) ? '.' : 'x';
}
}

REP(j, m) {
int ri, ci;
in >> ri >> ci;
bd[ri][2*ci] = '.';
}

REP(j, r) {
if (j % 2) {
char tmp[256];
tmp[0] = '.';
REP(jj, 2*(c-2)+1)
tmp[jj+1] = bd[j][jj];
tmp[2*(c-2)+2] = '.';
memcpy(bd[j], tmp, sizeof(bd[j]));

}
}

double prb = .0;
int ret = -1;
REP(p, c-1) {
memset(dp, 0, sizeof(dp));

dp[2*p+1] = 1.;
solve(r, c);

if (dp[2*k+1] > prb)
prb = dp[2*k+1], ret = p;
}

char rprb[16];
sprintf(rprb, "%.6lf", prb);
out << ret << " " << rprb << endl;
}
cout << "done." << endl;
return 0;
}


Studious Student:

int main() {
ifstream in(INPUT);
ofstream out(OUTPUT);

int n, m;
in >> n;

REP(i, n) {
in >> m;

vector<string>ss;
string str, ret = "";
REP(j, m) {
in >> str;
ss.PB(str);
}
sort(ALL(ss));

do {
string ptr = "";
EACH(itr, ss)
ptr += *itr;

if (ret == "" || ret > ptr)
ret = ptr;

} while (next_permutation(ALL(ss)));

out << ret << endl;
}

cout << "done." << endl;

return 0;
}

I'm shooting for advancing to round2 !!

0 件のコメント:

コメントを投稿