OpenAllHours
暴力 。
#include <bits/stdc++.h>
class OpenAllHours {
int toInt(const std::string &s) {
int hh = 10 * (s[0] - '0') + s[1] - '0';
int mm = 10 * (s[3] - '0') + s[4] - '0';
return hh * 60 + mm;
}
public :
std::string verify(int N, std::vector<std::string> openingTime, std::vector<std::string> closingTime) {
bool ok[1440] {};
for (int i = 0; i < N; ++i) {
int s = toInt(openingTime[i]);
int t = toInt(closingTime[i]);
for (int x = s; x != t; x = (x + 1) % 1440) ok[x] = true;
}
for (int i = 0; i < 1440; ++i)
if (!ok[i]) return "incorrect";
return "correct";
}
};
IncreasingJumps
两步可以移动 的距离,总共不会超过 步。
#include <bits/stdc++.h>
class IncreasingJumps {
public :
std::vector<int> jump(std::vector<int> frogs) {
int n = frogs.size();
std::vector<int> ans;
for (int i = 0; i < n; ++i) {
while (frogs[i] < i) {
ans.push_back(-(i + 1));
ans.push_back(i + 1);
++frogs[i];
}
while (frogs[i] > i) {
ans.push_back(i + 1);
ans.push_back(-(i + 1));
--frogs[i];
}
}
return ans;
}
};
TwoRobots
BFS,时间复杂度 。
#include <bits/stdc++.h>
class TwoRobots {
static constexpr int N = 40;
using Point = std::pair<int, int>;
#define x first
#define y second
struct State { Point a, b; };
static constexpr Point moves[] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
int n, m;
int dis[N][N][N][N];
bool isLegal(const Point &a) { return 0 <= a.x && a.x < n && 0 <= a.y && a.y < m; }
public :
int move(std::vector<std::string> plan) {
n = plan.size();
m = plan[0].size();
std::memset(dis, -1, sizeof(dis));
std::queue<State> que;
Point sa, ea, sb, eb;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (plan[i][j] == 'A') sa = {i, j};
else if (plan[i][j] == 'a') ea = {i, j};
else if (plan[i][j] == 'B') sb = {i, j};
else if (plan[i][j] == 'b') eb = {i, j};
}
}
dis[sa.x][sa.y][sb.x][sb.y] = 0;
que.push({sa, sb});
while (!que.empty()) {
const auto s = que.front();
const auto &a = s.a, b = s.b;
que.pop();
if (a == ea && b == eb) return dis[a.x][a.y][b.x][b.y];
for (const auto &ma : moves) {
const Point na{a.x + ma.x, a.y + ma.y};
if (!isLegal(na) || plan[na.x][na.y] == '#') continue;
for (const auto &mb : moves) {
const Point nb{b.x + mb.x, b.y + mb.y};
if (!isLegal(nb) || plan[nb.x][nb.y] == '#') continue;
if (na == nb) continue;
if (na == b && nb == a) continue;
if (dis[na.x][na.y][nb.x][nb.y] == -1) {
dis[na.x][na.y][nb.x][nb.y] = dis[a.x][a.y][b.x][b.y] + 1;
que.push({na, nb});
}
}
}
}
return -1;
}
};
constexpr TwoRobots::Point TwoRobots::moves[];IncreasingJumpsDiv1
大体思路是每个点尽可能用最接近的一步,剩下的步数每两步移动 的距离用来调整。我选择把数放在 ,并且逆序,这样是为了让距离相同的位置数变少,最坏情况下可能要进行约 次调整,即 步。
#include <bits/stdc++.h>
class IncreasingJumpsDiv1 {
public :
std::vector<int> jump(std::vector<int> frogs) {
int n = frogs.size();
std::vector<int> p(n);
std::iota(p.begin(), p.end(), 0);
std::sort(p.begin(), p.end(), [&](int i, int j) { return frogs[i] > frogs[j]; });
std::vector<int> pos(n);
for (int i = 0; i < n; ++i) pos[p[i]] = 1250 + i;
std::sort(p.begin(), p.end(), [&](int i, int j) { return std::abs(pos[i] - frogs[i]) < std::abs(pos[j] - frogs[j]); });
int x = 1;
int j = 0, k = 0;
std::vector<int> ans;
while (k < n) {
if (k < j && (j == n || x + 2 <= std::abs(pos[p[j]] - frogs[p[j]]))) {
if (frogs[p[k]] < pos[p[k]]) {
ans.push_back(-(p[k] + 1));
ans.push_back(p[k] + 1);
++frogs[p[k]];
} else {
ans.push_back(p[k] + 1);
ans.push_back(-(p[k] + 1));
--frogs[p[k]];
}
x += 2;
} else if (x + 3 <= std::abs(pos[p[j]] - frogs[p[j]])) {
if (frogs[p[j]] < pos[p[j]]) {
ans.push_back(-(p[j] + 1));
ans.push_back(p[j] + 1);
++frogs[p[j]];
} else {
ans.push_back(p[j] + 1);
ans.push_back(-(p[j] + 1));
--frogs[p[j]];
}
x += 2;
} else {
if (frogs[p[j]] < pos[p[j]]) {
ans.push_back(p[j] + 1);
frogs[p[j]] += x;
} else {
ans.push_back(-(p[j] + 1));
frogs[p[j]] -= x;
}
++x;
++j;
}
while (k < j && pos[p[k]] == frogs[p[k]]) ++k;
}
return ans;
}
};
EllysFlags
轮廓线 DP,只需要记录每个格子是 “W” 或一个 “WG” 或两个 “WG” 或其他。
时间复杂度为 。似乎按照斜线进行 DP 可以做到 。
#include <bits/stdc++.h>
class EllysFlags {
static constexpr int Inf = 1e9;
public :
int getMax(std::vector<std::string> paper) {
int n = paper.size(), m = paper[0].size();
std::vector<int> dp[2];
int cur = 0;
dp[0].assign(1 << (2 * m), -Inf);
dp[0][0] = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cur ^= 1;
dp[cur].assign(1 << (2 * m), -Inf);
for (int mask = 0; mask < (1 << (2 * m)); ++mask) {
if (dp[!cur][mask] < 0) continue;
char ch = paper[i][j];
int nMask = mask & ~(3 << (2 * j));
int val = dp[!cur][mask];
if (ch == 'W') dp[cur][nMask | 3 << (2 * j)] = std::max(dp[cur][nMask | 3 << (2 * j)], val);
int cnt = ((mask >> (2 * j) & 3) == 3) + (j > 0 && (mask >> (2 * j - 2) & 3) == 3);
if (ch == 'W' || ch == 'G') dp[cur][nMask | cnt << (2 * j)] = std::max(dp[cur][nMask | cnt << (2 * j)], val);
cnt = 0;
int x = mask >> (2 * j) & 3;
if (x != 3) cnt += x;
if (j > 0) {
x = mask >> (2 * j - 2) & 3;
if (x != 3) cnt += x;
}
if (ch == 'W' || ch == 'R') dp[cur][nMask] = std::max(dp[cur][nMask], val + cnt);
}
}
}
return *std::max_element(dp[cur].begin(), dp[cur].end());
}
};