CF366C Dima and Salad
题意
个水果,每个水果有两个属性
- 美味值
- 卡路里
选用至少 个水果
要求
求最大美味值
算法分析
-
选择物品求最大价值,考虑 背包
-
花费(消耗背包容量)
推导:
-
价值
-
细节 , 也就是说一个花费和为 的状态可能由负数转移而来,方便下标存储整体右移 个单位,将起点挪至
code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 110;
const int M = 1e5+100;
int a[N], b[N], w[N], v[N], dp[N][M * 2];
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
cin >> b[i];
w[i] = a[i];
v[i] = b[i] * k - a[i];
// v[i] = a[i] - b[i] * k;
}
// for (int i = 0; i < N; i++)
// for (int j = 0; j < M * 2; j++) dp[i][j] = -1;
memset(dp, -1, sizeof(dp));
dp[0][M] = 0;
for (int i = 1; i <= n; i++)
for (int j = M * 2 - 1; j >= 0; j--) {
if (j - v[i] >= M * 2 - 1 || j - v[i] < 0) continue;
if (dp[i - 1][j - v[i]] == -1) dp[i][j] = dp[i - 1][j];
else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - v[i]] + w [i]);
}
if (dp[n][M]) cout << dp[n][M];
else cout << -1;
return 0;
}P9649 [SNCPC2019] Coolbits
题意
个区间
从每个区间中选择一个整数,求最大与值
算法分析
- 按位与的性质:当前为全为 即为 ,任意一个为 则为
- 对于区间 ,若左边界 第 位为 ,那区间内的所有值第 位都应是
- 若所有区间都满足左端 的第 位为 ,则第 位可以填
- 更新左端点为第 位为 ,其余更低位设为 ,其意义为最小合法左端点
- 持续判断所有位,累加答案即可
code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
int L[N], R[N], n;
ll ans;
int check(int x, int i) {
if (!((x >> i) & 1)) x = ((x >> i) | 1) << i;
return x;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> L[i] >> R[i];
for (int i = 30; i >= 0; i--) {
ll d = 1ll << i;
bool f = 0;
for (int j = 1; j <= n; j++)
if (check(L[j], i) > R[j]) {
f = 1;
break;
}
if (f) continue;
for (int j = 1; j <= n; j++) L[j] = check(L[j], i);
ans |= d;
}
cout << ans;
return 0;
}
Mex Tree Gym - 103941J
题意
定义 为当前连通块未出现过最小自然数
给定一棵树,问 恰好等于 的最大连通块大小
算法分析
- 对于根节点 他的 含义为不包含 的最大连通块大小,即删去 的剩余连通块最大大小,也就是最大子树大小
- 对于其他节点 ,有解情况下就是总结点数减去子树 大小
- 无解情况:由于 是排列,不会出现相同数值,从 往下排节点数值只会更大 若从 往下走变小,即构成 的连通块必定包含 此情况无解
- 对于每一个 构成与 的最小连通块,点集为 的路径上的点(链),所以对于有解情况直接沿父亲
vis染色 - 从小到大枚举节点,若 已被染色则无解
code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6 + 10;
vector<int> g[N];
int a[N], sz[N], f[N];
bool vis[N];
void dfs(int x, int fa) {
f[x] = fa;
sz[x] = 1;
for (auto v : g[x]) {
if (v != fa) {
dfs(v, x);
sz[x] += sz[v];
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
a[x] = i;
}
for (int i = 2; i <= n; i++) {
int x;
cin >> x;
g[i].push_back(x);
g[x].push_back(i);
}
dfs(a[0], 0);
int cur = a[0];
vis[cur] = 1;
int res = 0;
for (auto v : g[cur]) res = max(res, sz[v]);
cout << res << " ";
for (int i = 1; i < n; i++) {
int x = a[i];
if (vis[x]) cout << 0 << " ";
else {
int t = x;
while (!vis[t]) {
vis[t] = 1;
t = f[t];
}
cout << n - sz[x] << " ";
}
}
cout << n;
return 0;
}
AT_abc237_g [ABC237G] Range Sort Query
题意
给定一个由 组成的长度为 的排列 ,以及一个整数
- 当 时:将 按升序排序
- 当 时:将 按降序排序
按顺序依次处理所有查询后,请输出最终排列中满足 的
算法分析
-
排序操作并不好直接进行局部懒标记操作
-
注意到关键词 排列 和单点查询(可离线)
-
性质:对于小于 的数在序列的任意一个地方效果都是等价的(升序为例,置于 ),大于同理
-
于是我考虑将
- 的数染成
- 的数染成
- 的数染成
-
这样的三色排列的排序(升序为例)就直接按照 全排前面,再是 ,后面全排 的顺序进行区间赋值即可,降序同理
-
最后便利 个数,单点查询是否存在等于 的()输出下标即可
-
细节:由于整个序列只有一个等于 的位置,需要特判需必须要对这种位置单点修改(下表是否需要向后推一位)WA 75pts
node res = query(x, y, 1, 1, n);
// if (!res.b) continue;
if (opt == 1) {
update(x, x + res.a - 1, 1, 1, n, 0);
if (res.b) {
update(x + res.a, x + res.a, 1, 1, n, 1);
update(x + res.a + 1, y, 1, 1, n, 2);
} else update(x + res.a, y, 1, 1, n, 2);
} else {
update(x, x + res.c - 1, 1, 1, n, 2);
if (res.b) {
update(x + res.c, x + res.c, 1, 1, n, 1);
update(x + res.c + 1, y, 1, 1, n, 0);
} else update(x + res.c, y, 1, 1, n, 0);
}code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6 + 10;
struct node {
int a, b, c, tag;
} tree[N];
int n, X, a[N];
void push_up(int p) {
int l = p << 1, r = p << 1 | 1;
tree[p].a = tree[l].a + tree[r].a;
tree[p].b = tree[l].b + tree[r].b;
tree[p].c = tree[l].c + tree[r].c;
}
void build(int p, int pl, int pr) {
tree[p].tag = -1;
if (pl == pr) {
if (a[pl] == 0) tree[p].a = 1;
else if (a[pl] == 1) tree[p].b = 1;
else tree[p].c = 1;
return;
}
int mid = (pl + pr) >> 1;
build(p << 1, pl, mid);
build(p << 1 | 1, mid + 1, pr);
push_up(p);
}
void addtag(int p, int pl, int pr, int d) {
tree[p].tag = d;
if (d == 0) {
tree[p].a = pr - pl + 1;
tree[p].b = tree[p].c = 0;
} else if (d == 1) {
tree[p].b = pr - pl + 1;
tree[p].a = tree[p].c = 0;
} else {
tree[p].c = pr - pl + 1;
tree[p].a = tree[p].b = 0;
}
}
void push_down(int p, int pl, int pr) {
if (tree[p].tag != -1) {
int mid = (pl + pr) >> 1;
addtag(p << 1, pl, mid, tree[p].tag);
addtag(p << 1 | 1, mid + 1, pr, tree[p].tag);
tree[p].tag = -1;
}
}
void update(int L, int R, int p, int pl, int pr, int d) {
if (L > R) return;
if (L <= pl && pr <= R) {
addtag(p, pl, pr, d);
return;
}
push_down(p, pl, pr);
int mid = (pl + pr) >> 1;
if (L <= mid) update(L, R, p << 1, pl, mid, d);
if (R > mid) update(L, R, p << 1 | 1, mid + 1, pr, d);
push_up(p);
}
node query(int L, int R, int p, int pl, int pr) {
// cout << "PaN" << endl;
if (L > R) return (node){0, 0, 0, 0};
if (L <= pl && pr <= R) return tree[p];
push_down(p, pl, pr);
int mid = (pl + pr) >> 1;
node res = {0, 0, 0, 0};
if (L <= mid) {
node tmp = query(L, R, p << 1, pl, mid);
res.a += tmp.a; res.b += tmp.b; res.c += tmp.c;
}
if (R > mid) {
node tmp = query(L, R, p << 1 | 1, mid + 1, pr);
res.a += tmp.a; res.b += tmp.b; res.c += tmp.c;
}
return res;
}
int main() {
// freopen("sample.in", "r", stdin);
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int q;
cin >> n >> q >> X;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (x < X) a[i] = 0;
else if (x == X) a[i] = 1;
else a[i] = 2;
}
//cout<<1<<"11111 "<<1<<endl;
build(1, 1, n);
while (q--) {
int opt, x, y;
cin >> opt >> x >> y;
node res = query(x, y, 1, 1, n);
// if (!res.b) continue;
if (opt == 1) {
update(x, x + res.a - 1, 1, 1, n, 0);
if (res.b) {
update(x + res.a, x + res.a, 1, 1, n, 1);
update(x + res.a + 1, y, 1, 1, n, 2);
} else update(x + res.a, y, 1, 1, n, 2);
} else {
update(x, x + res.c - 1, 1, 1, n, 2);
if (res.b) {
update(x + res.c, x + res.c, 1, 1, n, 1);
update(x + res.c + 1, y, 1, 1, n, 0);
} else update(x + res.c, y, 1, 1, n, 0);
}
}
for (int i = 1; i <= n; i++)
if (query(i, i, 1, 1, n).b == 1) {
cout << i;
return 0;
}
return 0;
}