Many Formulas
思路:字符串最多 10 10 10位,所以直接所有加号在哪个位置
void solve() {
string s; cin >> s;
int n = s.size() - 1;
int ans = 0;
for (int i = 0; i < (1 << n); i ++) {
string str = s;
int c = 0;
for (int j = 0; j < n; j ++) {
if (i >> j & 1) {
str = str.substr(0, j + 1 + c) + '+' + str.substr(j + 1 + c);
c ++;
}
}
stack<int> stk;
bool ok = false;
for (int j = 0; j < str.size(); j ++) {
int k = j;
int sum = 0;
if (str[j] >= '0' && str[j] <= '9') {
while (str[k] >= '0' && str[k] <= '9' && k < str.size()) {
sum = sum * 10 + (str[k] - '0');
k ++;
}
j = k - 1;
stk.push(sum);
}
if (!ok) {
if (str[j] == '+') ok = true;
} else {
int x = stk.top(); stk.pop();
int y = stk.top(); stk.pop();
stk.push(x + y);
ok = false;
}
}
ans += stk.top();
}
cout << ans << '\n';
}
[Tak and Cards]http://162.14.124.219/contest/1010/problem/B()
思路:背包 d p dp dp, d p [ i ] [ j ] [ k ] dp[i][j][k] dp[i][j][k]表示前 i i i个物品中选 j j j个物品凑出总和为 k k k的方案数,可以将第一维优化,题目内存够,没必要,记得初始化选零个,体积为零时候的方案数,之后直接按照背包 d p dp dp进行转移即可
int dp[N][N][N * N];
void solve() {
int n, A; cin >> n >> A;
vector<int> a(n + 1);
for (int i = 1; i <= n; i ++) {
cin >> a[i];
}
int ans = 0;
for (int i = 0; i <= n; i ++) dp[i][0][0] = 1, dp[i][0][1] = dp[i][1][0] = 0;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= i; j ++) {
for (int k = 1; k <= 2500; k ++) {
dp[i][j][k] = dp[i - 1][j][k];
if (k >= a[i]) dp[i][j][k] += dp[i - 1][j - 1][k - a[i]];
}
}
}
for (int i = 1; i <= n; i ++) {
ans += dp[n][i][i * A];
}
cout << ans << '\n';
}
Wall
思路:求两个点之间的最短路,数据范围小,直接 O ( n 3 ) O(n^3) O(n3)的 f l o y d floyd floyd即可
`int g[11][11];
void solve() {
int n, m; cin >> n >> m;
vector<vector<int>> dp(11, vector<int> (11, INF));
for (int i = 0; i <= 9; i ++) {
for (int j = 0; j <= 9; j ++) {
cin >> dp[i][j];
}
}
for (int k = 0; k < 10; k ++) {
for (int i = 0; i < 10; i ++) {
for (int j = 0; j < 10; j ++) {
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}
}
}
int ans = 0;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j ++) {
int x; cin >> x;
if (x != -1 && x != 1) ans += dp[x][1];
}
}
cout << ans << '\n';
}
Coloring Edges on Tree
思路:直接 d f s dfs dfs就行,每次染色都从 1 1 1开始,只要和这个点的父节点练的那条边的颜色不一样就行
void solve() {
int n; cin >> n;
vector<vector<int>> g(n + 1);
vector<array<int, 2>> a(n + 1);
vector<int> fa(n + 1);
for (int i = 1; i < n; i ++) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
a[i] = {u, v};
}
int cnt = 0;
map<array<int, 2>, int> mp;
auto dfs = [&](auto && dfs, int u, int f) -> void {
int idx = 0, ii = mp[{f, u}];
for (auto v : g[u]) {
if (v == f) continue;
idx ++;
if (idx == ii) idx ++;
mp[{u, v}] = idx;
dfs(dfs, v, u);
}
cnt = max(idx, cnt);
};
dfs(dfs, 1, 0);
cout << cnt << '\n';
for (int i = 1; i < n; i ++) {
cout << mp[ {a[i][0], a[i][1]}] << '\n';
}
}
Fault-tolerant Network
思路:本质上要让所有电脑联通,只需要将第一行的 1 1 1和 n n n两个点和下面一行连起来,第二行的 1 1 1和 n n n两个点和第一行连起来就行,先分别求出来四个顶点连到对应行的最小值,要注意有可能第一行和第二行的直接连接端点的情况
void solve() {
cnt ++;
int n; cin >> n;
vector<int> a(n + 1), b(n + 1);
for (int i = 1; i <= n; i ++) {
cin >> a[i];
}
for (int i = 1; i <= n; i ++) {
cin >> b[i];
}
int ans = abs(a[1] - b[1]) + abs(a[n] - b[n]), t;
int r1, r2, r3, r4;
r1 = r2 = r3 = r4 = INF;
for (int i = 1; i <= n; i ++) r1 = min(r1, abs(a[1] - b[i])), r2 = min(r2, abs(a[n] - b[i]));
for (int i = 1; i <= n; i ++) r3 = min(r3, abs(b[1] - a[i])), r4 = min(r4, abs(b[n] - a[i]));
ans = min(ans, r1 + r2 + r3 + r4);
ans = min(ans, abs(a[1] - b[1]) + r2 + r4);
ans = min(ans, abs(a[n] - b[n]) + r1 + r3);
ans = min(ans, abs(a[1] - b[n]) + abs(a[n] - b[1]));
ans = min(ans, abs(a[1] - b[n]) + r2 + r3);
ans = min(ans, abs(a[n] - b[1]) + r1 + r4);
cout << ans << '\n';
}
Nearest Excluded Points
思路:先找出距离已知点为 1 1 1的点,记录答案,放到队列里面,然后按照正常的 b f s bfs bfs更新就行,只有遇到这个点没有被记录答案并且是题目给的点,就更新答案,放到队列里面,这个点的答案就是由前面拓展点的答案,因为 b f s bfs bfs第一次遇见一定是最短的
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
void solve() {
int n; cin >> n;
map<pair<int, int>, int> vis;
map<pair<int, int>, pair<int, int>> ans;
vector<pair<int, int>> a(n + 1);
for (int i = 1; i <= n; i ++) {
cin >> a[i].first >> a[i].second;
vis[ {a[i].first, a[i].second}] = 1;
}
queue<pair<int, int>> q;
for (int i = 1; i <= n; i ++) {
for (int j = 0; j < 4; j ++) {
int x = a[i].first + dx[j];
int y = a[i].second + dy[j];
if (!vis.count({x, y})) {
q.push({a[i].first, a[i].second});
ans[ {a[i].first, a[i].second}] = {x, y};
}
}
}
while (q.size()) {
auto [x, y] = q.front();
q.pop();
for (int j = 0; j < 4; j ++) {
int tx = x + dx[j];
int ty = y + dy[j];
if (vis.count({tx, ty}) && !ans.count({tx, ty})) {
ans[ {tx, ty}] = ans[ {x, y}];
vis[ {tx, ty}] = 1;
q.push({tx, ty});
}
}
}
for (int i = 1; i <= n; i ++) {
auto [t1, t2] = ans[ {a[i].first, a[i].second}];
cout << t1 << ' ' << t2 << '\n';
}
}
Vacation Query
思路:类似线段树维护最大字段和,这个题的区间修改是把区间进行取反操作,但我们不用真的进行取反操作,只用记录每个区间里连续 0 0 0, 1 1 1的最大值就行,进行取反的时候交换这个区间里面维护 0 0 0, 1 1 1数量的变量的值就相当于取反了,这个思路太巧秒了
template<class T>
struct SegmentTree {
#define ls u << 1
#define rs u << 1 | 1
struct Info {
int l, r;
int op;
T mx0, mx1;
T lsum0, lsum1;
T rsum0, rsum1;
};
vector<Info> tr;
vector<T> a;
SegmentTree(const vector<T> &init) {
int n = init.size() - 1;
tr.resize(n * 4 + 1);
a = init;
build(1, 1, n);
}
Info merge(Info &u, Info l, Info r) {
int lvl = l.r - l.l + 1;
int rvl = r.r - r.l + 1;
u.mx0 = max({l.mx0, r.mx0, l.rsum0 + r.lsum0});
u.mx1 = max({l.mx1, r.mx1, l.rsum1 + r.lsum1});
if (lvl == l.lsum0) u.lsum0 = l.lsum0 + r.lsum0;
else u.lsum0 = l.lsum0;
if (lvl == l.lsum1) u.lsum1 = l.lsum1 + r.lsum1;
else u.lsum1 = l.lsum1;
if (rvl == r.rsum0) u.rsum0 = l.rsum0 + r.rsum0;
else u.rsum0 = r.rsum0;
if (rvl == r.lsum1) u.rsum1 = l.rsum1 + r.rsum1;
else u.rsum1 = r.rsum1;
return u;
}
void calc(Info &u) {
swap(u.mx0, u.mx1);
swap(u.lsum0, u.lsum1);
swap(u.rsum0, u.rsum1);
u.op ^= 1;
}
void pushup(int u) {
// debug1(u);
tr[u] = merge(tr[u], tr[ls], tr[rs]);
}
void pushdown(int u) {
auto [_, __, op, _1, _2, _3, _4, _5, _6] = tr[u];
if (op) {
calc(tr[ls]);
calc(tr[rs]);
tr[u].op ^= 1;
}
}
void build(int u, int l, int r) {
if (!a[l]) tr[u] = {l, r, 0, 1, 0, 1, 0, 1, 0};
else tr[u] = {l, r, 0, 0, 1, 0, 1, 0, 1};
if (l == r) return ;
int mid = l + r >> 1;
build(ls, l, mid);
build(rs, mid + 1, r);
pushup(u);
}
void modify(int u, int l, int r) {
if (tr[u].l >= l && tr[u].r <= r) {
calc(tr[u]);
return ;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) modify(ls, l, r);
if (r > mid) modify(rs, l, r);
pushup(u);
}
Info query(int u, int l, int r) {
if (tr[u].l >= l && tr[u].r <= r) return tr[u];
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (r <= mid) return query(ls, l, r);
if (l > mid) return query(rs, l, r);
Info t = merge(t, query(ls, l, r), query(rs, l, r));
pushup(u);
return t;
}
};
void solve() {
int n, q; cin >> n >> q;
string s; cin >> s;
vector<int> a(n + 1);
for (int i = 0; i < s.size(); i ++) {
a[i + 1] = (s[i] == '1');
}
SegmentTree<int> sg(a);
while (q --) {
int c, l, r; cin >> c >> l >> r;
if (c == 1) {
sg.modify(1, l, r);
} else {
cout << sg.query(1, l, r).mx1 << '\n';
}
}
}