1005 - Out of Control
题目大意
你有一个长度为 n n n 的序列,你每次可以向一个栈内放一个数,如果栈不为空并且栈顶的数大于你放的数,那么你放的数会变成栈顶的数再放入,问当栈的大小为 1 1 1 到 n n n 时分别有几种情况
解题思路
考虑dp, f i , j f_{i,j} fi,j 表示放了 i i i 个数,最大的数为 j j j 的方案数
通过离散化处理,记录原数的大小 a i a_i ai 和 数量 b i b_i bi,这样 j j j 只要枚举下标即可
初始化显然 f 1 , j = 1 f_{1,j}=1 f1,j=1
对于 f i , j f_{i,j} fi,j,它可以从 f i − 1 , 1 f_{i-1,1} fi−1,1 到 f i − 1 , j f_{i-1,j} fi−1,j 转移而来
f i , j = ∑ k = 1 j f i , k f_{i,j}=\sum_{k=1}^jf_{i,k} fi,j=∑k=1jfi,k,前提条件是存在可填的数,即 ∑ k = 1 j b [ k ] ≥ i \sum_{k=1}^jb[k]\ge i ∑k=1jb[k]≥i
只要顺带处理一下前缀和就可以啦
code
cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 3e3 + 9;
const int MOD = 1e9 + 7;
int t, n, x[N], a[N], b[N], num, lst;
long long f[N][N];
int main() {
scanf("%d", &t);
while (t --) {
scanf("%d", &n);
for (int i = 1; i <= n; ++ i) scanf("%d", &x[i]);
sort(x + 1, x + n + 1);
num = lst = 0;
for (int i = 1; i <= n; ++ i)
if (x[i] != x[i - 1])
b[num] = i - lst, lst = i, a[++ num] = x[i];
b[num] = n - lst + 1;
for (int i = 1; i <= num; ++ i) b[i] += b[i - 1];
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= num; ++ j) {
if (i == 1) f[i][j] = 1;
else if (b[j] > i) f[i][j] = f[i - 1][j];
}
for (int j = 1; j <= num; ++ j) {
f[i][j] = (f[i][j] + f[i][j - 1]) % MOD;
}
}
for (int i = 1; i <= n; ++ i) {
printf("%lld\n", f[i][num]);
}
for (int i = 1; i <= n; ++ i)
for (int j = 1; j <= num; ++ j)
f[i][j] = 0;
}
return 0;
}
1009 - Operation Hope
题意、思路待补
code
cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
struct lol {int x, id;} e[3][N * 2];
int t, n, a[3][N * 2], hd[3], tl[3], vis[N * 2], q[N * 2], num, f[N * 2], ans;
bool cmp(lol a, lol b) {return a.x < b.x;}
int getx(int x, int p, int k) {
if (hd[k] <= tl[k]) {
int y = e[k][hd[k]].x, id = e[k][hd[k]].id;
if (y < x - p) {++ hd[k]; return id;}
y = e[k][tl[k]].x; id = e[k][tl[k]].id;
if (y > x + p) {-- tl[k]; return id;}
}
return 0;
}
void dfs(int x, int p) {
vis[x] = 1;
for (int k = 0; k < 3; ++ k) while (1) {
int y = getx(a[k][x], p, k);
if (!y) break;
if (y <= n) if (!vis[y + n]) dfs(y + n, p); else;
else if (!vis[y - n]) dfs(y - n, p); else;
}
q[++ num] = x;
}
void dfs1(int x, int p) {
vis[x] = 0; f[x] = ans;
for (int k = 0; k < 3; ++ k) while (1) {
int y = getx(a[k][x <= n ? x + n : x - n], p, k);
if (!y) break;
if (vis[y]) dfs1(y, p);
}
}
int chk(int p) {
num = ans = 0;
for (int i = 1; i <= 2 * n; ++ i) vis[i] = 0;
for (int k = 0; k < 3; ++ k) hd[k] = 1, tl[k] = 2 * n;
for (int k = 0; k < 3; ++ k)
for (int i = 1; i <= 2 * n; ++ i)
if (!vis[i]) dfs(i, p);
for (int k = 0; k < 3; ++ k) hd[k] = 1, tl[k] = 2 * n;
for (int k = 0; k < 3; ++ k)
for (int i = num; i >= 1; -- i)
if (vis[q[i]]) ++ ans, dfs1(q[i], p);
for (int i = 1; i <= n; ++ i) if (f[i] == f[i + n]) return 0;
return 1;
}
int main() {
scanf("%d", &t);
while (t --) {
scanf("%d", &n);
for (int i = 1; i <= n; ++ i) {
for (int j = 0; j < 3; ++ j)
scanf("%d", &a[j][i]),
e[j][i].x = a[j][i],
e[j][i].id = i;
for (int j = 0; j < 3; ++ j)
scanf("%d", &a[j][i + n]),
e[j][i + n].x = a[j][i + n],
e[j][i + n].id = i + n;
}
for (int k = 0; k < 3; ++ k)
sort(e[k] + 1, e[k] + 2 * n + 1, cmp);
int l = 0, r = 1e9;
while (l <= r) {
int mid = (l + r) >> 1;
if (chk(mid)) r = mid - 1;
else l = mid + 1;
}
printf("%d\n", l);
}
return 0;
}