来自学长的馈赠7

暴力基本打满了

A. count

比较容易发现一个大小如果能分,那么方案唯一,于是可以枚举因子\(check\)

但是由于\(cdsidi\)造了点极限数据,所以\(dfs\)写法不能过,可以写成\(bfs\),或者用下面这个貌似正确,但是不能理性证明的东西

当一个大小\(x\)合法,必然有\(>=n/x\)\(size\)\(x\)的倍数,感性理解就是倍数可以不停地砍掉\(x\),个数保证了一定能砍完整个树

code
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 1000005;
inline int read(){
	int x = 0; char c = getchar();
	while(c < '0' || c > '9')c = getchar();
	do{x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}while(c <= '9' && c >= '0');
	return x;
}
int head[maxn], tot, n, size[maxn], fa[maxn];
struct edge{
	int to,net;
}e[maxn << 1 | 1];
void add(int u, int v){
	e[++tot].net = head[u];
	head[u] = tot;
	e[tot].to = v;
}
void dfs(int x){
	size[x] = 1;
	for(int i = head[x]; i; i = e[i].net){
		int v = e[i].to;
		if(v == fa[x])continue;
		fa[v] = x;
		dfs(v);
		size[x] += size[v];
	}
}
int cnt[maxn];
int main(){
	n = read();
	for(int i = 1; i < n; ++i){
		int u = read(), v = read();
		add(u, v); add(v, u);
	}
	if(n == 1){printf("1\n");return 0;}
	dfs(1);
	for(int i = 1; i <= n; ++i)++cnt[size[i]];
	int ans = 0;
	for(int i = 1; i <= n; ++i){
		if(n % i)continue;
		int res = 0;
		for(int j = i; j <= n; j += i)res += cnt[j];
		if(res >= n / i) ++ans;
	}
	
	printf("%d\n",ans);
	return 0;
}
// int dfs2(int x,int len){
// 	if(size[x] == len) return 0;
// 	if(size[x] < len) return size[x];
// 	int rt = 1;
// 	for(int i = head[x]; i; i = e[i].net){
// 		int v = e[i].to;
// 		if(v == fa[x])continue;
// 		rt += dfs2(v, len);
// 		if(rt > len)return rt;
// 	}
// 	if(rt == len)rt = 0;
// 	return rt;
// }
// int ans = 2, m = sqrt(n);
	// for(int i = 2; i <= m; ++i){
	// 	if(n % i == 0){
	// 		if(dfs2(1, i) == 0) ++ans;
	// 		if(i * i != n && dfs2(1, n / i) == 0) ++ans;
	// 	}
	// }

B. 序列

发现当\(i\)\(j\)满足条件时有\(a[j] - a[i] >= j - i\)\(a[j] - j >= a[i] - i\)然后可以跑\(lis\),用总数减去长度

code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 55;
inline int read(){
	int x = 0; char c = getchar();
	while(c < '0' || c > '9')c = getchar();
	do{x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}while(c <= '9' && c >= '0');
	return x;
}
int n, a[maxn], b[maxn];
struct tree{
	int t[maxn];
	int lowbit(int x){return x & -x;}
	void add(int x, int val){
		while(x <= n){
			t[x] = max(t[x], val);
			x += lowbit(x);
		}
	}
	int query(int x){
		int ans = 0;
		while(x){
			ans = max(ans, t[x]);
			x -= lowbit(x);
		}
		return ans;
	}
}T;
bool cmp(int x, int y){
	return a[x] < a[y];
}
int main(){
	n = read();
	for(int i = 1;i <= n; ++i)a[i] = read();
	for(int i = 1;i <= n; ++i)a[i] = a[i] - i;
	for(int i = 1;i <= n; ++i)b[i] = a[i];
	sort(b + 1, b + n + 1); int cnt = unique(b + 1, b + n + 1) - b;
	for(int i = 1; i <= n; ++i)a[i] = lower_bound(b + 1, b + cnt + 1, a[i]) - b;
	int ans = 0;
	for(int i = 1; i <= n; ++i){
		int now = T.query(a[i]) + 1;
		ans = max(ans, now);
		T.add(a[i], now);
	}
	printf("%d\n",n - ans);
	return 0;
}

C. 最远点对

线段树维护区间最远点对,考虑合并两个区间,新的两个最远点对一定是原来最远点对中的两个,具体证明不太会,可以感性理解下。。

code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 100005;
inline int read(){
	int x = 0; char c = getchar();
	while(c < '0' || c > '9')c = getchar();
	do{x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}while(c <= '9' && c >= '0');
	return x;
}
struct edge{
	int to,val,net;
}e[maxn << 1 | 1];
int head[maxn], tot, n, rd[maxn], m;
inline void add(int u, int v, int w){
	e[++tot].net = head[u];
	head[u] = tot;
	e[tot].to = v;
	e[tot].val = w;
	++rd[v];
}
int sum[maxn], fa[maxn], son[maxn], size[maxn], top[maxn], dep[maxn], id[maxn];
void dfs1(int x){
	size[x] = 1;
	for(register int i = head[x]; i; i = e[i].net){
		int v = e[i].to;
		if(v == fa[x])continue;
		dep[v] = dep[x] + 1;
		fa[v] = x;
		sum[v] = sum[x] + e[i].val;
		dfs1(v);
		size[x] += size[v];
		if(size[son[x]] < size[v])son[x] = v; 
	}
}
void dfs2(int x, int tp){
	top[x] = tp;
	if(son[x])dfs2(son[x],tp);
	for(register int i = head[x]; i; i = e[i].net){
		int v = e[i].to;
		if(v == fa[x] || v == son[x])continue;
		dfs2(v, v);
	}
}
inline int dis(int u, int v){
	int x = u, y = v;
	while(top[u] != top[v]){
		if(dep[top[u]] < dep[top[v]])swap(u, v);
		u = fa[top[u]];
	}
	int lca = dep[u] > dep[v] ? v : u;
	return sum[x] + sum[y] - sum[lca] - sum[lca];
}
struct node{
	int l,r,dis;
	node(){l = r = dis = 0;}
	node(int l_, int r_, int dis_){
		l = l_; r = r_; dis = dis_;
	}
};
struct tree{
	node t[maxn << 2 | 1];
	node push_up(node ls, node rs){
		node x;
		int d3 = dis(ls.l, rs.l), d4 = dis(ls.l, rs.r), d5 = dis(ls.r, rs.l), d6 = dis(ls.r, rs.r);
		x = ls.dis > rs.dis ? ls : rs;
		if(x.dis < d3)x = node(ls.l, rs.l, d3);
		if(x.dis < d4)x = node(ls.l, rs.r, d4);
		if(x.dis < d5)x = node(ls.r, rs.l, d5);
		if(x.dis < d6)x = node(ls.r, rs.r, d6);
		return x;
	}
	void built(int x, int l, int r){
		if(l == r){
			t[x].l = t[x].r = l;
			t[x].dis = 0;
			return;
		}
		int mid = (l + r) >> 1;
		built(x << 1, l, mid);
		built(x << 1 | 1, mid + 1, r);
		t[x] = push_up(t[x << 1], t[x << 1 | 1]);
	}
	node query(int x, int l, int r, int L, int R){
		if(L <= l && r <= R)return t[x];
		int mid = (l + r) >> 1;
		if(L <= mid && R > mid)return push_up(query(x << 1, l, mid, L, R), query(x << 1 | 1, mid + 1, r, L, R));
		if(L <= mid)return query(x << 1, l, mid, L, R);
		if(R > mid)return query(x << 1 | 1, mid + 1, r, L, R);
	}
}t;
int main(){
	n = read();
	for(register int i = 1; i < n; ++i){
		int u, v, w;
		u = read(), v = read(), w = read();
		add(u, v, w);
		add(v, u, w);
	}
	dfs1(1); dfs2(1, 1);
	t.built(1, 1, n);
	m = read();
	for(int i = 1; i <= m; ++i){
		int a = read(), b = read(), c = read(), d = read();
		node x = t.query(1, 1, n, a, b);
		node y = t.query(1, 1, n, c ,d);
		int ans = max(max(dis(x.l, y.l), dis(x.l, y.r)), max(dis(x.r, y.l), dis(x.r, y.r)));
		printf("%d\n",ans);
	}
	return 0;
}

D. 春节十二响

又一次把正解抛弃

考虑两条链,贪心来搞就是同步取\(max\)合并,价值是较大的

在树上显然可以同理,注意过程需要启发式合并

code
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn = 200005;
inline int read(){
	int x = 0; char c = getchar();
	while(c < '0' || c > '9')c = getchar();
	do{x = (x << 3) + (x << 1) + (c ^ 48); c = getchar();}while(c <= '9' && c >= '0');
	return x;
}
int fa[maxn], m[maxn], p[maxn], dep[maxn], n;
priority_queue<int>yq[maxn];
int rd[maxn], ls[maxn], top;
queue<int>q;
void merge(int x, int y){
	if(yq[x].size() < yq[y].size())swap(yq[x],yq[y]);
	top = 0;
	while(yq[y].size()){
		ls[++top] = max(yq[x].top(), yq[y].top());
		yq[x].pop(); yq[y].pop();
	}
	for(int i = 1; i <= top; ++i)yq[x].push(ls[i]);
}
int main(){
	n = read();
	for(int i = 1; i <= n; ++i)m[i] = read();
	for(int i = 2; i <= n; ++i)fa[i] = read();
	for(int i = 2; i <= n; ++i)++rd[fa[i]];
	for(int i = 1; i <= n; ++i)if(rd[i] == 0)q.push(i);
	while(!q.empty()){
		int x = q.front(); q.pop();
		yq[x].push(m[x]);
		if(x == 1)continue;
		merge(fa[x], x);
		--rd[fa[x]]; if(rd[fa[x]] == 0)q.push(fa[x]);
	}
	ll ans = 0;
	while(yq[1].size())ans += yq[1].top(), yq[1].pop();
	printf("%lld\n",ans);
	return 0;
}
posted @ 2022-07-31 21:39  Chen_jr  阅读(26)  评论(0编辑  收藏  举报