int solve(void) { IIVec q; q.push_back(II(sx, sy)); for (int t = 1; q.size() > 0; ++t) { IIVec next; for (int i = 0; i < (int)q.size(); ++i) { const int dx[4] = { -1, 0, 1, 0 }; const int dy[4] = { 0, -1, 0, 1 }; for (int d = 0; d < 4; ++d) { int x = q[i].first + dx[d], y = q[i].second + dy[d]; if (x >= 0 && y >= 0 && x < W && y < H && ((1 << (t % 4)) & z[y][x]) == 0) { if (x == gx && y == gy) { return t; } if (!vis[t % 4][y][x] && maze[y][x] == '.') { vis[t % 4][y][x] = 1; next.push_back(II(x, y)); } } } } q = next; } return -1; }
int get_dist(const II &st, const II &ed) { if (st == ed) { return -1; } int d[64][64]; memset(d, -1, sizeof(d)); II dirs[4] = {II(-1,0),II(1,0),II(0,-1),II(0,1)}; IIVec q; d[st.second][st.first] = 0; q.push_back(st); while (q.size() > 0) { IIVec nq; int i, j; for (i = 0; i < (int)q.size(); ++i) { const II ¤t = q[i]; if (current == ed) { return d[current.second][current.first]; } for (j = 0; j < 4; ++j) { int x = current.first + dirs[j].first; int y = current.second + dirs[j].second; if (x >= 0 && x < W && y >= 0 && y < H && d[y][x] < 0 && fld[y][x] != '#') { nq.push_back(II(x, y)); d[y][x] = d[current.second][current.first] + 1; } } } q = nq; } return -1; }
double maxExpectation(int M, vector <int> day, vector <int> win, vector <int> gain) { Max = M; IIVec Days; int i; for (i = 0; i < (int)day.size(); ++i) { if (win[i] > 0 && gain[i] > 0) { Days.push_back(II(day[i], i)); } } sort(Days.begin(), Days.end()); _day.clear(); _win.clear(); _gain.clear(); memset(memo, 0, sizeof(memo)); for (i = 0; i < (int)Days.size(); ++i) { _day.push_back(Days[i].first); _win.push_back(win[Days[i].second] * 0.01); _gain.push_back(gain[Days[i].second]); } if (Days.size() <= 0 || M <= _day[0]) { return M; } return rec(0, M - _day[0]); }
int maxSongs(vector <int> duration, vector <int> tone, int T) { int ans = 0; IIVec v; for (int i = 0; i < (int)tone.size(); ++i) { v.push_back(II(tone[i], duration[i])); } sort(v.begin(), v.end()); int dp[64][64]; memset(dp, 0x3f, sizeof(dp)); for (int i = 0; i < (int)v.size(); ++i) { for (int j = 0; j < i; ++j) { for (int k = 1; k <= i; ++k) { dp[i][k + 1] = min(dp[i][k + 1], dp[j][k] + abs(v[i].first - v[j].first) + v[i].second); if (dp[i][k + 1] <= T) { ans = max(ans, k + 1); } } } dp[i][1] = v[i].second; if (v[i].second <= T) { ans = max(ans, 1); } } return ans; }
vector <int> construct(vector <int> x) { IIVec v; for (int i = 0; i != x.size(); ++i) { v.push_back(II(x[i], i)); } sort(v.begin(), v.end()); vector <int> ans; for (int i = 0; i != v.size(); ++i) { if (v[i].first > (i + 1)) { ans.resize(1); ans[0] = -1; break; } for (int j = 0; j < (v[i].first - 1); ++j) { ans.push_back(v[i].second); ans.push_back(v[j].second); } } return ans; }
bool solve(int N, int M, IIVec &walls, int &flavors, IntVec &pillars) { IntSet V[2000]; // vertexes in a room IntSet F[2000]; // flavors in a room IntVec C[2001]; // vertex connection int i, j; for (i = 0; i < N; ++i) { V[0].insert(i); } int rooms = 1; for (i = 0; i < N; ++i) { C[i].push_back((i-1+N)%N); C[i].push_back((i+1)%N); } for (i = 0; i < (int)walls.size(); ++i) { int s = min(walls[i].first, walls[i].second) - 1; int e = max(walls[i].first, walls[i].second) - 1; for (j = 0; j < rooms; ++j) { if (V[j].count(s) > 0 && V[j].count(e) > 0) { break; } } if (j >= rooms) { // error return false; } IntSet a, b; a.insert(s); a.insert(e); b.insert(s); b.insert(e); bool f = false; IntSet::const_iterator it; for (it = V[j].begin(); it != V[j].end(); ++it) { if (!f) { a.insert(*it); } else { b.insert(*it); } if (*it == s || *it == e) { f = !f; } } V[j] = a; V[rooms++] = b; C[s].push_back(e); C[e].push_back(s); } flavors = N; for (i = 0; i < rooms; ++i) { flavors = min((int)V[i].size(), flavors); } // fill the first room { int flavor = 1; IntSet::const_iterator it; for (it = V[0].begin(); it != V[0].end(); ++it) { int pillar = *it; if (flavor <= flavors) { set_flavor(rooms, V, F, pillars, pillar, flavor); } else { // fill with different color IntSet s; for (i = 0; i < (int)C[pillar].size(); ++i) { s.insert(pillars[C[pillar][i]]); } for (i = 1; ; ++i) { if (s.count(i) <= 0) { set_flavor(rooms, V, F, pillars, pillar, i); break; } } } ++flavor; } } int filled_rooms, room; for (filled_rooms = 1; filled_rooms < N; ++filled_rooms) { bool found = false; for (room = 0; room < rooms; ++room) { int c = 0; IntSet::const_iterator it; for (it = V[room].begin(); it != V[room].end(); ++it) { if (pillars[*it] > 0) { ++c; } } if (c >= 2 && c < (int)V[room].size()) { found = true; break; } } if (!found) { break; } fill_room(rooms, flavors, V, F, C, pillars, room); } for (i = 0; i < rooms; ++i) { if ((int)F[i].size() < flavors) { return false; } } for (i = 0; i < N; ++i) { if (pillars[i] <= 0 || pillars[i] > flavors) { return false; } } return true; }