100 + 20 + 5 + 25 = 150 居然又没挂分!(主要是本来就没什么分可以挂的
A - 剖分
40分钟打完,105分钟才调过,第一次见到这么恶心的 看完题之后有一种树形dp的感觉,但不知道怎么做 于是先把样例画出来手玩一下,发现了一些东西:
- 每条路径的两端都不能有边相连
- 每一个子树的根,要么是从叶子一路连上来,以后再连到顶上,要么是从下面经过它再下去
突然有了一点点感觉,所以先设状态为: ,表示以为根的子树中,是一个还要连上去的点从下面连上来经过再连下去 转移就是:
(其它子节点的的累乘)
(也就是枚举了从哪个子节点连上来)
(其它子节点的的累乘)
(也就是枚举了从哪个点上来又从哪个点下去)
我们发现这样会T,于是整了一个小小的优化:累乘直接全部乘好,转移的时候除以选到的子节点的。 如果会除以0就搞前后缀乘 然后我们发现这样搞连样例都过不了。。。 手玩一下样例,又发现了一个: 如果是1的情况,并且它有不止1个子节点,而且其它的子节点也是1的情况,那它就必须继续往上连。 然后就直接炸掉了:) 但是好像还没有完全炸掉。烧烤了几分钟之后决定修改状态为: 为(0、1不变)2表示(和1一样)但是加了一个限制就是:下面的除了连上来的子节点状态都是0。 转移就在代码里面(因为剩下的就是修改和1h多的调试所以不细讲了)
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 1e5 + 5;
LL n, f[N][3], mod = 1e9 + 7, tmp[N], s[N][2];
vector <int> v[N];
LL qpow (LL x, LL y) {
LL ret = 1;
for (; y; y >>= 1) {
if (y & 1) {
(ret *= x) %= mod;
}
(x *= x) %= mod;
}
return ret;
}
void dfs (int x, int fa) {
if (v[x].size() == 1 && x != 1) {
f[x][1] = 1;
f[x][0] = 0;
f[x][2] = 1;
return;
}
LL sum = 1, sum1 = 0;
for (int j = 0; j < v[x].size(); j++) {
int i = v[x][j];
if (i == fa) continue;
dfs(i, x);
sum *= (f[i][0] + f[i][2]) % mod;
sum %= mod;
sum1 += f[i][2];
sum %= mod;
}
s[0][0] = s[0][1] = 1;
for (int j = 0; j < v[x].size(); j++) {
int i = v[x][j];
s[j + 1][0] = s[j][0] * (i == fa ? 1 : f[i][0]) % mod;
}
for (int j = v[x].size() - 1; j >= 0; j--) {
int i = v[x][j];
s[v[x].size() - j][1] = s[v[x].size() - j - 1][1] * (i == fa ? 1 : f[i][0]) % mod;
if (i == fa) continue;
f[x][2] += f[i][1] * s[j][0] % mod * s[v[x].size() - j - 1][1] % mod;
f[x][2] %= mod;
}
for (int i : v[x]) {
if (i == fa) continue;
tmp[i] = sum * qpow((f[i][0] + f[i][2]) % mod, mod - 2) % mod * f[i][1] % mod;
f[x][1] += tmp[i];
f[x][1] %= mod;
}
for (int i : v[x]) {
if (i == fa) continue;
f[x][0] += f[i][1] * (f[x][1] - tmp[i] + mod) % mod * qpow((f[i][0] + f[i][2]) % mod, mod - 2) % mod;
f[x][0] %= mod;
}
f[x][0] *= qpow(2, mod - 2);
f[x][0] %= mod;
f[x][1] += s[v[x].size()][0];
f[x][1] %= mod;
f[x][2] += s[v[x].size()][0];
f[x][2] %= mod;
}
int main () {
cin >> n;
if (n == 1) {
cout << 1;
return 0;
}
for (int i = 1, x, y; i < n; i++) {
cin >> x >> y;
v[x].emplace_back(y);
v[y].emplace_back(x);
}
dfs(1, 1);
cout << (f[1][0] + f[1][2]) % mod;
return 0;
}写得好shit啊qwq 但是为什么有人都能过?dmy连菊花图都不放一个的吗。。。数据人死掉了:)
B - 取模
居然有20分是白送的!(我也只会这么不要脸地拿白送的分了
一看就是数学题,不做了!(所以noip的T2都是数学吗
烧烤了一会之后想到了一些没用的东西:
- 如果m比最大的a、b都大的时候是最大的,就是-1
- 如果,可以
- 可能跟有关系???
然后完全没思路了。不管了,先把送给我的分拿了再说:) 直接枚举m就能拿到了。20分!(想起上一场T4状压dp拼尽全力只有5分
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 1e5 + 5;
int n, ans1, ans2, mx;
struct aa {
int a, b;
} a[N];
int main () {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].a >> a[i].b;
mx = max({mx, a[i].a, a[i].b});
}
if (n <= 5000 && mx <= 5000) {
mx++;
for (int i = 1, cnt; i <= mx; i++) {
cnt = 0;
for (int j = 1; j <= n; j++) {
if (a[j].a % i < a[j].b % i) {
cnt++;
}
}
ans1 = max(ans1, cnt);
}
for (int i = 1; i <= mx; i++) {
int cnt = 0;
for (int j = 1; j <= n; j++) {
if (a[j].a % i < a[j].b % i) {
cnt++;
}
}
if (cnt == ans1) {
// cout << i << ' ' << cnt << '\n';
ans2 += i;
}
if (i == mx && cnt == ans1) ans2 = -1;
}
cout << ans1 << ' ' << ans2;
return 0;
}
return 0;
}C - 金币
又送5分! 感觉是dp,果然是dp,然而没写出来qwq 没给这题很多时间,只把送的拿了就走了 因为所以只能单买,输出就有5分了:)
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int M = 1e5 + 5, N = 410;
LL m, n, t, a[M], ans;
int main () {
cin >> m >> n >> t;
for (int i = 1; i <= m; i++) {
cin >> a[i];
}
if (n == 0) {
for (int i = 1; i <= m; i++) {
ans += a[i] * t;
}
cout << ans;
return 0;
}
return 0;
}D - 画图
T4居然还能有25分qwq 首先是,用bfs直接暴力 然后是也就是一条,所以可以用并查集维护每个连通块,在每个块的根上面记录颜色、最左边的端点、最右边的端点,然后根据题意合并就好了 其它的想了一下,没搞出来
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2e5 + 5;
int n, m, dx[] = {0, 0, 1, -1}, dy[] {1, -1, 0, 0}, fa[N];
struct aa {
int a, l, r;
} a[N];
vector <int> v[N];
void bfs (int x, int y, int z) {
queue <pair <int, int>> q;
q.push({x, y});
int tmp = v[x][y];
v[x][y] = z;
while (q.size()) {
int x = q.front().first, y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
if (v[xx][yy] != tmp) continue;
v[xx][yy] = z;
q.push({xx, yy});
}
}
}
int fd (int x) {
return fa[x] ? fa[x] = fd(fa[x]) : x;
}
int main () {
cin >> n >> m;
if (n != 1) {
for (int i = 1; i <= n; i++) {
v[i].emplace_back(0);
for (int j = 1, x; j <= m; j++) {
cin >> x;
v[i].emplace_back(x);
}
}
int q;
for (cin >> q; q--;) {
int x, y, z;
cin >> x >> y >> z;
if (v[x][y] == z) continue;
bfs(x, y, z);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << v[i][j] << ' ';
}
cout << '\n';
}
return 0;
} else {
n = m;
a[0].a = -1;
for (int i = 1; i <= n; i++) {
cin >> a[i].a;
if (a[i].a == a[i - 1].a) {
fa[i] = fd(i - 1);
a[fa[i]].r = i;
} else {
a[i].l = i;
a[i].r = i;
}
// cout << fd(i) << ' ' << a[fd(i)].a << ' ' << a[fd(i)].l << ' ' << a[fd(i)].r << '\n';
}
int q;
for (cin >> q; q--;) {
int x, y, z;
cin >> x >> y >> z;
x = y;
x = fd(x);
a[x].a = z;
int ll = a[x].l, rr = a[x].r;
if (a[x].l > 1 && a[fd(a[x].l - 1)].a == z) {
ll = a[fd(a[x].l - 1)].l;
fa[x] = fd(a[x].l - 1);
x = fa[x];
a[x].r = rr;
}
if (a[x].r < n && a[fd(a[x].r + 1)].a == z) {
fa[x] = fd(a[x].r + 1);
x = fa[x];
a[x].l = ll;
}
}
for (int i = 1; i <= n; i++) {
cout << a[fd(i)].a << ' ';
}
return 0;
}
return 0;
}我丢我bfs和并查集这么点东西居然写了快100行。。。 虽然T1调太久(2h才开始看T2),但还好调出来了,T2T3都送了分,出题人爸爸我爱你:) (上一个总结还说出题人死掉了来着 据说下一场是S的模拟(但这两场虽然是noip也写的S)也就是说,下一场写的会是J模拟吗:) 如果J的模拟上不了300是不是就没救了:)