Accoders 5.13 NOI模拟
以为NOI赛制是只测样例,一直在卡一些根本不需要卡的东西,然后暴力就写不完了。
problemsetter
如果a有偶数拆两半就行。
否则a都是奇数,我们把每个
因为a都是偶数,一定都是偶环。也就是说我们要找两个大小相等的集合,和也相等,这个折半一下哈希就行,复杂度
还有一个
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#define For(i, a, b) for (int i = a, i##end = b; i <= i##end; ++i)
#define rFor(i, b, a) for (int i = b, i##end = a; i >= i##end; --i)
#define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to)
typedef long long ll;
using std::cin;
using std::cout;
using std::endl;
typedef std::pair<int, int> pii;
#define fi first
#define se second
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
T myrand(T l, T r) {
return std::uniform_int_distribution<T>(l, r)(rnd);
}
void close_stdio() {
std::ios::sync_with_stdio(false);
std::cerr << "DO NOT use scanf/printf!\n";
}
const int kN = 35;
const int kS = (1 << 20) + 5;
int n, a[kN], ord[kN];
ll b[kN], sl[kS], sr[kS];
struct Hasher {
size_t operator()(const std::pair<ll, int> &x) const {
return std::hash<ll>()(x.fi) ^ std::hash<int>()(x.se);
}
};
std::unordered_map<std::pair<ll, int>, pii, Hasher> vis;
pii ans[kN];
int main() {
double tim = clock();
// freopen("in", "r", stdin);
freopen("problemsetter.in", "r", stdin);
freopen("problemsetter.out", "w", stdout);
scanf("%d", &n);
For(i, 0, n - 1) scanf("%d", &a[i]);
For(i, 0, n - 1) if (!(a[i] & 1)) {
printf("Yes\n");
For(j, 0, n - 1) {
if (j != i)
printf("%lld ", (ll)a[j] - a[i] / 2);
else
printf("%d ", a[i] / 2);
}
putchar('\n');
For(j, 0, n - 1) printf("%d %d\n", j + 1, i + 1);
return 0;
}
int lsz = n / 2, rsz = n - n / 2;
if (rsz)
--rsz, ++lsz;
if (rsz)
--rsz, ++lsz;
if (rsz)
--rsz, ++lsz;
For(msk, 1, (1 << lsz) - 1) { sl[msk] = sl[msk ^ (msk & -msk)] + a[__builtin_ctz(msk)]; }
vis[std::make_pair(0, 0)] = pii(0, 0);
int visc = 0;
For(msk, 1, (1 << rsz) - 1) {
sr[msk] = sr[msk ^ (msk & -msk)] + a[__builtin_ctz(msk) + lsz];
vis[std::make_pair(sr[msk], __builtin_popcount(msk))] = pii(msk << lsz, 0);
for (int x = msk; x; x = (x - 1) & msk) {
++visc;
vis[std::make_pair(sr[msk ^ x] - sr[x],
(int)__builtin_popcount(msk ^ x) - (int)__builtin_popcount(x))] =
pii((msk ^ x) << lsz, x << lsz);
}
}
// fprintf(stderr, "visc = %d vis %u\n", visc, vis.size());
// return 0;
int plus = -1, minus = -1;
For(msk, 1, (1 << lsz) - 1) {
if (clock() - tim > 1.9 * CLOCKS_PER_SEC)
break;
if (vis.count(std::make_pair(-sl[msk], -(int)__builtin_popcount(msk)))) {
pii t = vis[std::make_pair(-sl[msk], -(int)__builtin_popcount(msk))];
plus = msk | t.fi, minus = t.se;
break;
}
for (int x = msk; x; x = (x - 1) & msk) {
if (vis.count(std::make_pair(sl[x] - sl[msk ^ x],
(int)__builtin_popcount(x) - (int)__builtin_popcount(msk ^ x)))) {
pii t = vis[std::make_pair(sl[x] - sl[msk ^ x],
(int)__builtin_popcount(x) - (int)__builtin_popcount(msk ^ x))];
plus = (msk ^ x) | t.fi, minus = x | t.se;
break;
}
}
if (~plus)
break;
}
if (plus == -1) {
printf("No");
return 0;
}
// fprintf(stderr, "plus=%d minus=%d\n", plus, minus);
int cur = 0;
ll curs = 0;
while (plus) {
if (curs >= 0) {
int x = -1, y = -1;
for (int u = plus; u; u -= (u & -u)) {
for (int v = minus; v; v -= (v & -v))
if (a[__builtin_ctz(u)] - a[__builtin_ctz(v)] < 0) {
x = __builtin_ctz(u), y = __builtin_ctz(v);
break;
}
if (~x)
break;
}
if (x == -1) {
x = __builtin_ctz(plus), y = __builtin_ctz(minus);
}
ord[cur++] = x, ord[cur++] = y;
curs += a[x] - a[y];
plus ^= 1 << x, minus ^= 1 << y;
} else {
int x = -1, y = -1;
for (int u = plus; u; u -= (u & -u)) {
for (int v = minus; v; v -= (v & -v))
if (a[__builtin_ctz(u)] - a[__builtin_ctz(v)] > 0) {
x = __builtin_ctz(u), y = __builtin_ctz(v);
break;
}
if (~x)
break;
}
// assert(~x);
if (x == -1) {
x = __builtin_ctz(plus), y = __builtin_ctz(minus);
}
ord[cur++] = x, ord[cur++] = y;
curs += a[x] - a[y];
plus ^= 1 << x, minus ^= 1 << y;
}
// assert(curs <= 4e9 + 7);
}
// fprintf(stderr, "cur=%d\n", cur);
curs = 0;
b[0] = 0;
For(i, 0, cur - 2) {
curs = a[ord[i]] - curs;
b[i + 1] = curs;
}
For(i, 0, cur - 1) ans[ord[i]] = pii(i + 1, (i + 1) % cur + 1);
For(i, 0, n - 1) if (!ans[i].fi) {
b[cur++] = a[i];
ans[i] = pii(1, cur);
}
// assert(cur == n);
printf("Yes\n");
For(i, 0, cur - 1) printf("%lld ", b[i]);
putchar('\n');
For(i, 0, n - 1)
printf("%d %d\n", ans[i].fi, ans[i].se) /*, assert(a[i] == b[ans[i].fi - 1] + b[ans[i].se - 1])*/;
return 0;
}
/* PAY ATTENTION TO: */
/* 1. Memory Limit, Array Size */
/* 2. Integer Overflow */
/* 3. Multi-test */
/* 4. Potential Constant-speedup */
/* Stay organized and write things down. */
colorful
有了这个
可以枚举集合划分,但是其实我们不需要枚举集合划分,而是状压dp就能做到
下面是考场上写的乱七八糟不知道为什么能过的东西。
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a, i##end = b; i <= i##end; ++i)
#define rFor(i, b, a) for (int i = b, i##end = a; i >= i##end; --i)
#define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to)
typedef long long ll;
using std::cin;
using std::cout;
using std::endl;
typedef std::pair<int, int> pii;
#define fi first
#define se second
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T myrand(T l, T r) {
return std::uniform_int_distribution<T>(l, r)(rnd);
}
void close_stdio() {
std::ios::sync_with_stdio(false);
std::cerr << "DO NOT use scanf/printf!\n";
}
const int kN = 2e5 + 5;
const int kK = 25;
const int kB = 250;
const int kInf = 0x3f3f3f3f;
int n, m, k, lim, c[kN];
std::vector<int> G[kN], ch[kN];
std::vector<pii> e[kN];
bool cf[kN];
int dfn[kN], low[kN], st[kN], tot = 0, top = 0, clk = 0, fa[kN];
void tarjan(int u, int f) {
dfn[u] = low[u] = ++clk;
st[++top] = u;
for (int v : G[u]) if (v ^ f) {
if (!dfn[v]) {
tarjan(v, u);
low[u] = std::min(low[u], low[v]);
if (low[v] >= dfn[u]) {
++tot;
do {
ch[tot].push_back(st[top]);
fa[st[top]] = tot;
}
while (st[top--] != v);
ch[u].push_back(tot), fa[tot] = u;
}
}
else low[u] = std::min(low[u], dfn[v]);
}
}
typedef std::vector<int> vi;
std::vector<vi> sp[10];
void dfs(vi cur, int lim, int cc) {
if (cur.size() == lim) {
sp[lim].push_back(cur);
return;
}
cur.push_back(++cc);
dfs(cur, lim, cc);
--cc, cur.pop_back();
for (int i = 0; i < cur.size(); ++i) {
cur.push_back(cur[i]);
dfs(cur, lim, cc);
cur.pop_back();
}
}
void getsp() {
For(cur, 1, lim) {
dfs(vi(), cur, 0);
}
}
int d[kN][kK], g[kK], id[kN], dsu[10];
int getf(int x) {
return x == dsu[x] ? x : (dsu[x] = getf(dsu[x]));
}
void merge(int x, int y) {
x = getf(x), y = getf(y);
dsu[x] = y;
}
void handle(int u) { // u is square
int sz = ch[u].size() + 1;
for (int i = 0; i < ch[u].size(); ++i) id[ch[u][i]] = i;
id[fa[u]] = ch[u].size();
memset(g, 0x3f, sizeof(g));
for (int i = 0; i < sp[sz].size(); ++i) {
vi tmp = sp[sz][i];
For(j, 0, ch[u].size()) dsu[j] = j;
int t = 0;
for (auto x : e[u]) if (tmp[id[x.fi]] == tmp[id[x.se]]) {
if (c[x.fi] && c[x.se] && c[x.fi] != c[x.se]) {
t = kInf;
break;
}
merge(id[x.fi], id[x.se]);
}
if (t) continue;
for (int x : ch[u]) if (cf[x] && tmp[id[x]] == tmp[sz - 1]) {
if (c[x] && c[fa[u]] && c[x] != c[fa[u]]) {
t = kInf;
break;
}
merge(id[x], sz - 1);
}
if (t) continue;
For(j, 0, sz - 2) if (getf(j) == j && getf(sz - 1) != j) {
int mnv = kInf;
For(x, 1, k) {
int ts = 1;
For(y, 0, sz - 2) if (getf(y) == j) {
ts += d[ch[u][y]][x];
--ts;
if (ts > n) break;
}
if (ts <= n) mnv = std::min(mnv, ts);
}
t += mnv;
if (t > n) {
t = kInf;
break;
}
}
if (t > n) continue;
For(j, 1, k) if (!c[fa[u]] || c[fa[u]] == j) {
int ex = 0;
for (int x : ch[u]) if (getf(id[x]) == getf(sz - 1)) {
ex += d[x][j];
--ex;
if (ex > n) break;
}
g[j] = std::min(g[j], t + ex);
}
}
}
void dp(int u) { // u is round
if (ch[u].empty()) {
For(i, 1, k) if (c[u] == 0 || i == c[u])
d[u][i] = 1;
return;
}
for (int v : ch[u]) for (int w : ch[v]) dp(w);
For(i, 1, k) if (c[u] == 0 || i == c[u]) d[u][i] = 1;
for (int v : ch[u]) {
handle(v);
int sz = ch[v].size() + 1;
For(i, 1, k) if (d[u][i] <= n && g[i] <= n) d[u][i] += g[i];
}
}
int main() {
freopen("colorful.in", "r", stdin);
freopen("colorful.out", "w", stdout);
scanf("%d%d%d%d", &n, &m, &k, &lim);
For(i, 1, n) scanf("%d", &c[i]);
For(i, 1, m) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v), G[v].push_back(u);
}
tot = n;
tarjan(1, 0);
For(u, 1, n) {
for (int v : G[u]) {
if (fa[u] == fa[v]) {
e[fa[u]].emplace_back(u, v);
}
else if (v == fa[fa[u]]) cf[u] = true;
}
}
getsp();
memset(d, 0x3f, sizeof(d));
dp(1);
int ans = kInf;
For(i, 1, k) ans = std::min(ans, d[1][i]);
printf("%d", ans);
return 0;
}
/* PAY ATTENTION TO: */
/* 1. Memory Limit, Array Size */
/* 2. Integer Overflow */
/* 3. Multi-test */
/* 4. Potential Constant-speedup */
/* Stay organized and write things down. */
invfunc
这个东西比较回文,这是一个想法。
有个关键的性质是:对于一个大的合法串和它包含的一个小的合法串,把这个小的合法串对称过去,得到的还是合法串,并且字符种类数不变。
那么就能跑一个类似manacher的东西:枚举所有中轴计算贡献,把当前中轴延当前有端点最靠右的中轴对称过去,同时继承cnt和sum。我们还需在添删字符的过程中维护这两个量。
只需预处理前驱和后继就能做到i + r[i] > id + r[id],需要令r[i] = id + r[id] - i)删字符的次数是
这个其实直接做复杂度就是对的。我们只保留最靠右的id,那么在删字符的过程中,id - r[id]会不断往右移,所以总共是
考场的时候想到了回文,但是卡在了dp的思路上,没有发掘和回文有关的别的东西。
不过既然暴力都没时间写那也就没有什么好说的。
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = a, i##end = b; i <= i##end; ++i)
#define rFor(i, b, a) for (int i = b, i##end = a; i >= i##end; --i)
#define eFor(i, u, v) for (int i = head[u], v = e[i].to; i; i = e[i].next, v = e[i].to)
typedef long long ll;
using std::cin;
using std::cout;
using std::endl;
typedef std::pair<int, int> pii;
#define fi first
#define se second
std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T myrand(T l, T r) {
return std::uniform_int_distribution<T>(l, r)(rnd);
}
void close_stdio() {
std::ios::sync_with_stdio(false);
std::cerr << "DO NOT use scanf/printf!\n";
}
const int kN = 6e5 + 5;
const int P = 998244353;
int n, m, a[kN], nxt[kN], pre[kN], tmp[kN], pw[kN], r[kN], tot[kN], sum[kN];
void addl(int &tot, int x, int r, int v) {
if (x && nxt[x] > r) tot += v;
}
void addr(int &tot, int x, int l, int v) {
if (x <= n * 2 && pre[x] < l) tot += v;
}
bool check(int l, int r) {
if (l < 1 || r > n * 2 + 1) return false;
if (nxt[l] < r && a[r] != a[l + r - nxt[l]]) return false;
if (pre[r] > l && a[l] != a[l + r - pre[r]]) return false;
return true;
}
int main() {
freopen("invfunc.in", "r", stdin);
freopen("invfunc.out", "w", stdout);
scanf("%d%d", &n, &m);
For(i, 1, n) scanf("%d", &a[i]);
pw[0] = 1;
For(i, 1, m) pw[i] = (ll)m * pw[i - 1] % P;
int ans = 0;
rFor(i, n, 1) a[i * 2] = a[i];
For(i, 1, n * 2 + 1) if (i & 1) a[i] = m + 1;
For(i, 1, n * 2 + 1) {
pre[i] = tmp[a[i]];
tmp[a[i]] = i;
}
memset(tmp, 0x3f, sizeof(tmp));
rFor(i, n * 2 + 1, 1) {
nxt[i] = tmp[a[i]];
tmp[a[i]] = i;
}
tot[1] = 1;
for (int i = 2, id = 1; i <= n * 2; ++i) {
if (i <= id + r[id]) {
r[i] = r[id * 2 - i];
tot[i] = tot[id * 2 - i];
sum[i] = sum[id * 2 - i];
while (r[i] + i > id + r[id]) {
if ((id + r[i]) & 1) (sum[i] += P - pw[m + 1 - tot[i]]) %= P;
addl(tot[i], id * 2 - i - r[i], id * 2 - i + r[i], -1);
addr(tot[i], id * 2 - i + r[i], id * 2 - i - r[i] + 1, -1);
--r[i];
}
}
else r[i] = 0, tot[i] = 1, sum[i] = 0;
while (check(i - r[i] - 1, i + r[i] + 1)) {
addl(tot[i], i - r[i] - 1, i + r[i], 1);
addr(tot[i], i + r[i] + 1, i - r[i] - 1, 1);
++r[i];
if ((i + r[i]) & 1) (sum[i] += pw[m + 1 - tot[i]]) %= P;
}
// fprintf(stderr, "r[%d] = %d tot=%d sum = %d\n", i, r[i], tot[i], sum[i]);
if (i + r[i] >= id + r[id]) id = i;
(ans += sum[i]) %= P;
}
printf("%d", ans);
return 0;
}
/* PAY ATTENTION TO: */
/* 1. Memory Limit, Array Size */
/* 2. Integer Overflow */
/* 3. Multi-test */
/* 4. Potential Constant-speedup */
/* Stay organized and write things down. */