Codeforces Round #837 (Div. 2)

ABC略.

D.

题意:给定一棵n个节点的无向树,每个节点一个字符,定义d(u,v)为u到v路径上字符形成的字符串,并定义回文子序列为一个串回文的子序列,求树上任意路径的最长回文子序列长度.

这道题放在水平上一个O(n^2)的区间dp就完事了,考虑转移,发现水平上转移的关键在于两个端点之间的前驱和后继,于是在树上我们记录每条路径的端点前驱和后继即可.

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define ll long long
#define int ll
#define PII pair<int, int>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int N = 2e3 + 10;
int n, pre[N][N], f[N][N];
string s;
vector<int> g[N];
void find_pre(int x, int fa, int root) {
	pre[root][x] = fa;
	for (int y : g[x]) {
		if (y == fa) continue;
		find_pre(y, x, root);
	}	
}
int dfs(int l, int r) {
	if (l == r) return 1;
	if (pre[l][r] == l) return 1 + (s[l] == s[r]);
	if (f[l][r] != -1) return f[l][r];
	int res = max(dfs(l, pre[l][r]), dfs(pre[r][l], r));
	if (s[l] == s[r]) res = max(res, dfs(pre[r][l], pre[l][r]) + 2);
	return f[l][r] = res;
}
void solve() {
	cin >> n >> s;
	s = "?" + s;
	for (int i = 1; i <= n; ++i) {
		g[i].clear();
		for (int j = 1; j <= n; ++j) {
			f[i][j] = -1;
		}
	}
	for (int i = 1; i <= n - 1; ++i) {
		int x, y;
		cin >> x >> y;
		g[x].emplace_back(y), g[y].emplace_back(x);
	}
	for (int i = 1; i <= n; ++i) find_pre(i, 0, i);
	int ans = 1;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			ans = max(ans, dfs(i, j));
		}
	}
	cout << ans << "\n";
}
signed main() {
    IOS;
    int T = 1;
    cin >> T;
    while (T--) solve();
}

E.

题意:给定一个n*m的矩阵,矩阵上有'.''m''#'三种方格,第一种可以随便用,第二种只能用一次,第三种不能用,满足以上条件建造一个最大的'H'型建筑,求出最大占地格子.

 先处理出每个点向上下经过0,1个'm'最多能走的格子数,然后枚举每一行,对每一行枚举左右端点即可.

#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 = 410;
const int dd[][4] = {
	1, 0, 0, 0,
	0, 1, 0, 0,
	0, 0, 1, 0,
	0, 0, 0, 1
};
int n, m, lstu[N][N][2], lstd[N][N][2], ss[N][N], sm[N][N];
char g[N][N];
signed main() {
	qio >> n >> m;
	for (int i = 1; i <= n; ++i) qio >> (g[i] + 1);
	for (int i = 1; i <= m; ++i) {
		lstu[0][i][0] = lstu[0][i][1] = 0;
		for (int j = 1; j <= n; ++j) {
			if (g[j][i] == '#') {
				lstu[j][i][0] = lstu[j][i][1] = j;
			}else if (g[j][i] == 'm') {
				lstu[j][i][1] = lstu[j - 1][i][0], lstu[j][i][0] = j;
			}else {
				lstu[j][i][0] = lstu[j - 1][i][0], lstu[j][i][1] = lstu[j - 1][i][1];
			}
		}
		lstd[n + 1][i][0] = lstd[n + 1][i][1] = n + 1;
		for (int j = n; j >= 1; --j) {
			if (g[j][i] == '#') {
				lstd[j][i][0] = lstd[j][i][1] = j;
			}else if (g[j][i] == 'm') {
				lstd[j][i][1] = lstd[j + 1][i][0], lstd[j][i][0] = j;
			}else {
				lstd[j][i][0] = lstd[j + 1][i][0], lstd[j][i][1] = lstd[j + 1][i][1];
			}
		}
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			ss[i][j] = ss[i][j - 1] + (g[i][j] == '#');
			sm[i][j] = sm[i][j - 1] + (g[i][j] == 'm');
		}
	}
	int ans = 0;
	for (int i = 2; i <= n - 1; ++i) {
		for (int l = 1; l <= m - 1; ++l) {
			for (int r = l + 2; r <= m; ++r) {
				if (g[i][l] == '#' || g[i][r] == '#' || ss[i][r - 1] - ss[i][l] >= 1 || sm[i][r - 1] - sm[i][l] >= 2) {
					continue;	
				}
				int cntm = sm[i][r - 1] - sm[i][l];
				int nowlu = lstu[i][l][0] + 1, nowld = lstd[i][l][0] - 1, nowru = lstu[i][r][0] + 1, nowrd = lstd[i][r][0] - 1;
				int u = max(nowlu, nowru), d = min(nowld, nowrd);
				if (u < i && d > i) {
					ans = max(ans, 2 * (d - u + 1) + r - l - 1);
				}
				if (cntm == 0) {
					int x[4] = {};
					for (int j = 0; j < 4; ++j) {
						for (int k = 0; k < 4; ++k) {
							x[k] = dd[j][k];
						}
						nowlu = lstu[i][l][x[0]] + 1, nowld = lstd[i][l][x[1]] - 1, nowru = lstu[i][r][x[2]] + 1, nowrd = lstd[i][r][x[3]] - 1;
						u = max(nowlu, nowru), d = min(nowld, nowrd);
						if (u < i && d > i) {
							ans = max(ans, 2 * (d - u + 1) + r - l - 1);
						}
					}
					if (g[i][l] == 'm') {
						nowlu = lstu[i][l][1] + 1, nowld = lstd[i][l][1] - 1, nowru = lstu[i][r][0] + 1, nowrd = lstd[i][r][0] - 1;
						u = max(nowlu, nowru), d = min(nowld, nowrd);
						if (u < i && d > i) {
							ans = max(ans, 2 * (d - u + 1) + r - l - 1);
						}
					}
					if (g[i][r] == 'm') {
						nowlu = lstu[i][l][0] + 1, nowld = lstd[i][l][0] - 1, nowru = lstu[i][r][1] + 1, nowrd = lstd[i][r][1] - 1;
						u = max(nowlu, nowru), d = min(nowld, nowrd);
						if (u < i && d > i) {
							ans = max(ans, 2 * (d - u + 1) + r - l - 1);
						}
					}
				}
			}
		}
	}
	cout << ans << "\n";
}

F.

题意:给定一个数组a[1~n],询问q次,求出[l,r]中出现次数为奇数的最小的数.

出现次数为奇数我们想到异或,如果我们对每个数随机分配一个哈希值,区间异或和为0说明每个数都出现了偶数次,如果不为0说明都为奇数次,注意不分配哈希值直接算异或和可能会出错,因为区间异或和为0有可能是几个数异或起来为0,但是加上随机异或值出错概率很小,于是我们用主席树维护异或和然后主席树上二分即可.

#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 = 2e6 + 10;
map<int, int> mp;
int n, a[N], root[N], tot, q;
struct rec {
	int lc, rc, val;
	#define l(p) tr[p].lc
	#define r(p) tr[p].rc
}tr[N * 5];
void clone(int &p) {
	tr[++tot] = tr[p];
	p = tot;
}
void pushup(int p) {
	tr[p].val = tr[l(p)].val ^ tr[r(p)].val;
}
void modify(int &p, int l, int r, int x, int k) {
	clone(p);
	if (l == r) {
		tr[p].val ^= k;
		return;
	}
	int mid = l + r >> 1;
	if (x <= mid) modify(l(p), l, mid, x, k);
	else modify(r(p), mid + 1, r, x, k);
	pushup(p);
}
int ask(int v1, int v2, int l, int r) {
	if (l == r) return l;
	int v = tr[l(v1)].val ^ tr[l(v2)].val;
	int mid = l + r >> 1;
	if (v != 0) return ask(l(v1), l(v2), l, mid);
	else return ask(r(v1), r(v2), mid + 1, r);
}
signed main() {
	IOS;
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		if (!mp.count(a[i])) mp[a[i]] = rnd();
		root[i] = root[i - 1];
		modify(root[i], 0, inf, a[i], mp[a[i]]);
	}
	int lst = 0;
	cin >> q;
	while (q--) {
		int l, r;
		cin >> l >> r;
		l = l ^ lst;
		r = r ^ lst;
		lst = ask(root[l - 1], root[r], 0, inf);
		if (lst == inf) lst = 0;
		cout << lst << "\n";
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值