Educational Codeforces Round 141 (Rated for Div. 2)

D.

 题意:给定长度n的a[1~n],操作n-2次,第i次令a[i+2]=a[i+2]-a[i+1],a[i]=a[i]+a[i+1]或者a[i+2]=a[i+2]+a[i+1],a[i]=a[i]-a[i+1].求最后产生的数组的种类数.

首先考虑dp,若i同时接受i-1和i+1的作用,这样转移行不通,但是实际上我们只需要考虑i-1对i的贡献即可,枚举i的值x,设f[i][x]为操作1~i-1执行完毕后第i位的值为x的方案数.f[i][x]转移到f[i+1][a[i+1]+x]或f[i+1][a[i+1]-x].

#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0) 
#define ll long long 
// #define double long double
#define ull unsigned long long 
#define PII pair<int, int> 
#define PDI pair<double, int> 
#define PDD pair<double, double> 
#define debug(a) cout << #a << " = " << a << endl 
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end() 
#define mem(x, y) memset((x), (y), sizeof(x)) 
#define lbt(x) (x & (-x)) 
#define SZ(x) ((x).size()) 
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
const int base = 90000, N = 310, MOD = 998244353;
int n, a[N], f[N][base * 2 + 10];
int t(int x) {
	if (x < 0) return -x + base;
	return x;
}
signed main() {
	IOS;
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	f[2][a[2]] = 1;
	for (int i = 2; i <= n - 1; ++i) {
		for (int j = -base; j <= base; ++j) {
			if (j == 0) {
				(f[i + 1][t(a[i + 1])] += f[i][t(j)]) %= MOD;
			}else { 
				if (a[i + 1] + j >= -base && a[i + 1] + j <= base) {
					(f[i + 1][t(a[i + 1] + j)] += f[i][t(j)]) %= MOD;
				}
				if (a[i + 1] - j >= -base && a[i + 1] - j <= base) {
					(f[i + 1][t(a[i + 1] - j)] += f[i][t(j)]) %= MOD;
				}
			}
		}
	}
	int ans = 0;
	for (int i = -base; i <= base; ++i) {
		(ans += f[n][t(i)]) %= MOD;
	}
	cout << ans << "\n";
}

E.

题意:n个贵物,AB两人攻击鬼物,A击杀需要a[i]次,B需要b[i]次,对于一只贵物,A先攻击k次,求出A击杀所有贵物k所有可能的取值(1<=k<=n).

可以用整除分块搞,但是会超时.正难则反,我们用前缀和处理出a[i] > b[i]的区间覆盖,如果i*k落在这个区间,那么这个k值不可取.

整除分块(TLE):

#include <bits/stdc++.h>
// #define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0) 
#define ll long long 
// #define double long double
#define ull unsigned long long 
#define PII pair<int, int> 
#define PDI pair<double, int> 
#define PDD pair<double, double> 
#define debug(a) cout << #a << " = " << a << endl 
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end() 
#define mem(x, y) memset((x), (y), sizeof(x))
#define lbt(x) (x & (-x)) 
#define SZ(x) ((x).size()) 
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
// namespace nqio { const unsigned R = 4e5, W = 4e5; char* a, * b, i[R], o[W], * c = o, * d = o + W, h[40], * p = h, y; bool s; struct q { void r(char& x) { x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++; } void f() { fwrite(o, 1, c - o, stdout); c = o; } ~q() { f(); }void w(char x) { *c = x; if (++c == d) f(); } q& operator >>(char& x) { do r(x); while (x <= 32); return *this; } q& operator >>(char* x) { do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this; } template<typename t> q& operator>>(t& x) { for (r(y), s = 0; !isdigit(y); r(y)) s |= y == 45; if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this; } q& operator <<(char x) { w(x); return *this; }q& operator<< (char* x) { while (*x) w(*x++); return *this; }q& operator <<(const char* x) { while (*x) w(*x++); return *this; }template<typename t> q& operator<< (t x) { if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p); return *this; } }qio; }using nqio::qio;
using namespace std;
mt19937 rnd(random_device{}());

const int N = 2e5 + 10;
int n, a[N], b[N];
void solve() {
	cin >> n;
	for (int i = 1; i <= n; ++i) cin >> a[i];
	for (int i = 1; i <= n; ++i) cin >> b[i];
	vector<int> s(n + 10, 0);
	for (int i = 1; i <= n; ++i) {
		for (int l = 1, r; l <= n; l = r + 1) {
			int x = (a[i] + l - 1) / l;
			if (x == 1) r = n;
			else r = (a[i] - 1) / (x - 1);
			int r_ = min(x == 1 ? n : (b[i] - 1) / (x - 1), r);
			// cout << l << " " << r_ << "\n";
			if (r_ >= l) ++s[l], --s[r_ + 1];
		}
		// cout << "\n";
	}
	vector<int> ans;
	for (int i = 1; i <= n; ++i) {
		s[i] += s[i - 1];
		// cout << s[i] << " ";
		if (s[i] == n) {
			ans.emplace_back(i);
		}
	}
	cout << ans.size() << "\n";
	for (int x : ans) {
		cout << x << " ";
	}
	cout << "\n";
}
signed main() {
	IOS;
	int T;
	cin >> T;
	while (T--) solve();
}

正解:

#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0) 
#define ll long long 
// #define double long double
#define ull unsigned long long 
#define PII pair<int, int> 
#define PDI pair<double, int> 
#define PDD pair<double, double> 
#define debug(a) cout << #a << " = " << a << endl 
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end() 
#define mem(x, y) memset((x), (y), sizeof(x)) 
#define lbt(x) (x & (-x)) 
#define SZ(x) ((x).size()) 
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
const int N = 2e5 + 10;
int n, s[N], a[N], b[N];
void solve() {
	cin >> n;
	for (int i = 1; i <= n; ++i) s[i] = 0;
	for (int i = 1; i <= n; ++i) cin >> a[i];
	for (int i = 1; i <= n; ++i) cin >> b[i];
	for (int i = 1; i <= n; ++i) {
		if (a[i] > b[i]) {
			++s[b[i]], --s[a[i]];
		}
	}
	for (int i = 1; i <= n; ++i) {
		s[i] += s[i - 1];
	}
	vector<int> ans;
	for (int k = 1; k <= n; ++k) {
		bool ok = 1;
		for (int i = 1; i * k <= n; ++i) {
			if (s[i * k]) {
				ok = 0;
				break;
			}
		}
		if (ok) {
			ans.emplace_back(k);
		}
	}
	cout << ans.size() << "\n";
	for (int x : ans) {
		cout << x << " ";
	}
	if (ans.size()) cout << "\n";
}
signed main() {
	IOS;
	int T;
	cin >> T;
	while (T--) solve();
}

F.

 题意:给定数组a[n],b[n],可以执行以下操作:选择一个下标i,交换a[i]和a[x],与b[i]和b[y],其中a[x] = b[y] = i.求最小交换次数以及其中一个方案.

看见交换,我们套路地考虑置换环,一个数组交换完之后,置换环数+1,多出来的那个环形成自环,一个大小为n的置换环只需要交换n-1次,最后一次是自环,所以一个置换环可以选一个位置不交换.我们可以求出两个数组总共的交换次数n,然后减去相同位置可以节省的次数.

考虑网络流,对两个数组求出置换环然后编号,对于位置i,位置i在第一个数组中所处的编号为x,第二个数组为y,x->i, i->y, 然后S->第一个数组的置换环,第二个数组的置换环->T.

#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0) 
#define ll long long 
// #define double long double
#define ull unsigned long long 
#define PII pair<int, int> 
#define PDI pair<double, int> 
#define PDD pair<double, double> 
#define debug(a) cout << #a << " = " << a << endl 
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end() 
#define mem(x, y) memset((x), (y), sizeof(x)) 
#define lbt(x) (x & (-x)) 
#define SZ(x) ((x).size()) 
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
// namespace nqio { const unsigned R = 4e5, W = 4e5; char* a, * b, i[R], o[W], * c = o, * d = o + W, h[40], * p = h, y; bool s; struct q { void r(char& x) { x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++; } void f() { fwrite(o, 1, c - o, stdout); c = o; } ~q() { f(); }void w(char x) { *c = x; if (++c == d) f(); } q& operator >>(char& x) { do r(x); while (x <= 32); return *this; } q& operator >>(char* x) { do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this; } template<typename t> q& operator>>(t& x) { for (r(y), s = 0; !isdigit(y); r(y)) s |= y == 45; if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this; } q& operator <<(char x) { w(x); return *this; }q& operator<< (char* x) { while (*x) w(*x++); return *this; }q& operator <<(const char* x) { while (*x) w(*x++); return *this; }template<typename t> q& operator<< (t x) { if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p); return *this; } }qio; }using nqio::qio;
using namespace std;
const int N = 2e5 + 10;
int n, a[N];
int dep[N], f[N][25];
vector<int> g[N];
void dfs(int x, int fa, int depth) {
	dep[x] = depth;
	f[x][0] = fa;
	for (int i = 1; (1ll << i) <= depth; ++i) {
		f[x][i] = f[f[x][i - 1]][i - 1];
	}
	for (int y : g[x]) {
		if (y == fa) {
			continue;
		}
		dfs(y, x, depth + 1);
	}
}
int LCA(int x, int y) {
	if (dep[x] < dep[y]) swap(x, y);
	while (dep[x] > dep[y]) x = f[x][__lg(dep[x] - dep[y])];
	if (x == y) return x;
	for (int i = __lg(dep[x]); i >= 0; --i) {
		if (f[x][i] != f[y][i]) {
			x = f[x][i], y = f[y][i];
		}
	}
	return f[x][0];
}
struct Node {
	int x, y, len;
	bool operator< (const Node &i) {
		return len < i.len;
	}
}tr[N * 4];
Node calc(int x, int y) {
	return {x, y, dep[x] + dep[y] - 2 * dep[LCA(x, y)] + a[x] + a[y]};
}
Node merge(Node A, Node B) {
	Node t[] = {A, B, calc(A.x, B.x), calc(A.x, B.y), calc(A.y, B.x), calc(A.y, B.y)};
	return *max_element(t, t + 6);
}
void build(int p, int l, int r) {
	if (l == r) {
		return tr[p] = {l, l, a[l] * 2}, void(0);
	}
	int mid = (l + r) >> 1;
	build(p * 2, l, mid), build(p * 2 + 1, mid + 1, r);
	tr[p] = merge(tr[p * 2], tr[p * 2 + 1]);
}
void modify(int p, int l, int r, int x) {
	if (l == r) {
		return tr[p] = {x, x, a[x] * 2}, void(0);
	}
	int mid = (l + r) >> 1;
	if (x <= mid) modify(p * 2, l, mid, x);
	else modify(p * 2 + 1, mid + 1, r, x);
	tr[p] = merge(tr[p * 2], tr[p * 2 + 1]);
}
signed main() {
	IOS;
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	for (int i = 1; i <= n - 1; ++i) {
		int x, y;
		cin >> x >> y;
		g[x].emplace_back(y);
		g[y].emplace_back(x);
	}
	dfs(1, -1, 1);
	build(1, 1, n);
	int q;
	cin >> q;
	while (q--) {
		int x, y;
		cin >> x >> y;
		a[x] = y;
		modify(1, 1, n, x);
		cout << max((tr[1].len + 1) / 2, max(a[tr[1].x], a[tr[1].y])) << "\n";
	}
}

G.

 题意:给定一棵n个点的无根树,每个点有点权a[x],定义树的半径max(w_v(u)),w_v(u)=d_v(u)+a(u).每次修改树的一个点的权值.求树的半径.

求树的半径有点困难,不妨求树的直径,然后转化为半径.有以下的性质:如果两颗树通过一条边连接起来,那么新的树的直径一定是两棵树原本的直径或者两条直径拼接起来的,我们只需要枚举原来树的直径的四个端点就可以求出新的直径.这启发我们用线段树去维护直径.求出直径之后半径=max(直径/2,a[i]).

#include <bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0) 
#define ll long long 
// #define double long double
#define ull unsigned long long 
#define PII pair<int, int> 
#define PDI pair<double, int> 
#define PDD pair<double, double> 
#define debug(a) cout << #a << " = " << a << endl 
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end() 
#define mem(x, y) memset((x), (y), sizeof(x)) 
#define lbt(x) (x & (-x)) 
#define SZ(x) ((x).size()) 
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
// namespace nqio { const unsigned R = 4e5, W = 4e5; char* a, * b, i[R], o[W], * c = o, * d = o + W, h[40], * p = h, y; bool s; struct q { void r(char& x) { x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++; } void f() { fwrite(o, 1, c - o, stdout); c = o; } ~q() { f(); }void w(char x) { *c = x; if (++c == d) f(); } q& operator >>(char& x) { do r(x); while (x <= 32); return *this; } q& operator >>(char* x) { do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this; } template<typename t> q& operator>>(t& x) { for (r(y), s = 0; !isdigit(y); r(y)) s |= y == 45; if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this; } q& operator <<(char x) { w(x); return *this; }q& operator<< (char* x) { while (*x) w(*x++); return *this; }q& operator <<(const char* x) { while (*x) w(*x++); return *this; }template<typename t> q& operator<< (t x) { if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p); return *this; } }qio; }using nqio::qio;
using namespace std;
const int N = 2e5 + 10;
int n, a[N];
int dep[N], f[N][25];
vector<int> g[N];
void dfs(int x, int fa, int depth) {
	dep[x] = depth;
	f[x][0] = fa;
	for (int i = 1; (1ll << i) <= depth; ++i) {
		f[x][i] = f[f[x][i - 1]][i - 1];
	}
	for (int y : g[x]) {
		if (y == fa) {
			continue;
		}
		dfs(y, x, depth + 1);
	}
}
int LCA(int x, int y) {
	if (dep[x] < dep[y]) swap(x, y);
	while (dep[x] > dep[y]) x = f[x][__lg(dep[x] - dep[y])];
	if (x == y) return x;
	for (int i = __lg(dep[x]); i >= 0; --i) {
		if (f[x][i] != f[y][i]) {
			x = f[x][i], y = f[y][i];
		}
	}
	return f[x][0];
}
struct Node {
	int x, y, len;
	bool operator< (const Node &i) {
		return len < i.len;
	}
}tr[N * 4];
Node calc(int x, int y) {
	return {x, y, dep[x] + dep[y] - 2 * dep[LCA(x, y)] + a[x] + a[y]};
}
Node merge(Node A, Node B) {
	Node t[] = {A, B, calc(A.x, B.x), calc(A.x, B.y), calc(A.y, B.x), calc(A.y, B.y)};
	return *max_element(t, t + 6);
}
void build(int p, int l, int r) {
	if (l == r) {
		return tr[p] = {l, l, a[l] * 2}, void(0);
	}
	int mid = (l + r) >> 1;
	build(p * 2, l, mid), build(p * 2 + 1, mid + 1, r);
	tr[p] = merge(tr[p * 2], tr[p * 2 + 1]);
}
void modify(int p, int l, int r, int x) {
	if (l == r) {
		return tr[p] = {x, x, a[x] * 2}, void(0);
	}
	int mid = (l + r) >> 1;
	if (x <= mid) modify(p * 2, l, mid, x);
	else modify(p * 2 + 1, mid + 1, r, x);
	tr[p] = merge(tr[p * 2], tr[p * 2 + 1]);
}
signed main() {
	IOS;
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	for (int i = 1; i <= n - 1; ++i) {
		int x, y;
		cin >> x >> y;
		g[x].emplace_back(y);
		g[y].emplace_back(x);
	}
	dfs(1, -1, 1);
	build(1, 1, n);
	int q;
	cin >> q;
	while (q--) {
		int x, y;
		cin >> x >> y;
		a[x] = y;
		modify(1, 1, n, x);
		cout << max((tr[1].len + 1) / 2, max(a[tr[1].x], a[tr[1].y])) << "\n";
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值