首页 > 其他分享 >Codeforces Round 899 (Div. 2)

Codeforces Round 899 (Div. 2)

时间:2023-09-26 18:46:07浏览次数:37  
标签:head int 899 ll Codeforces tot Div include fo

赛后四题

B题直接枚举不存在的元素即可
C题的trick有点像之前abc的某道题,对于奇数位置它一定可以贡献,对于偶数位置,如果它有数选了,那么它就能够贡献。

\(f[i]\)表示到前i个且至少选了一个的最大答案。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define fo(i,a,b) for (int (i)=(a);(i)<=(b);(i)++)
#define fd(i,b,a) for (int (i)=(b);(i)>=(a);(i)--)
#define mk(x,y) make_pair((x),(y))

using namespace std;
typedef double db;
typedef long long ll;
const int N = 2e5 + 5;
const ll inf = 1ll << 60;
ll a[N], n, ans, f[N];
void solve() {
	ans = 0;
	scanf("%lld", &n);

	fo(i, 0, n) f[i] = -inf;
	fo(i, 1, n) scanf("%lld", &a[i]);

	fo(i, 1, n) {
		if (i & 1) {
			f[i] = max(a[i], f[i - 1] + max(0ll, a[i]));
		}
		else {
			f[i] = max(0ll, f[i - 1] + max(0ll, a[i]));
		}

		ans = max(ans, f[i]);

	}
	printf("%lld\n", ans);

}
int main()
{

// #ifdef LOCAL
//  		freopen("in.txt", "r", stdin);
//     	freopen("out.txt", "w", stdout);
// #endif
	
	int T;
	scanf("%d", &T);
	while (T--) {
		solve();
	}

	return 0;
}

 

D题
首先考虑固定一个根

\[f[x]=\sum_{v\in son(x)} f[v]+[a[x] \neq a[v]]*(a[v] \oplus a[x])*size[v] \]

那么做一个换根dp即可

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define fo(i,a,b) for (int (i)=(a);(i)<=(b);(i)++)
#define fd(i,b,a) for (int (i)=(b);(i)>=(a);(i)--)
#define mk(x,y) make_pair((x),(y))
using namespace std;
typedef double db;
typedef long long ll;
const int N=2e5+5;
const ll mo=998244353;
ll f[N],g[N],s[N],a[N];
int tot,nex[N*2],head[N],to[N*2],n,x,y;

void add(int x,int y){
	to[++tot]=y; nex[tot]=head[x]; head[x]=tot;
}
void dfs(int x,int y){
	s[x]=1; f[x]=g[x]=0;
	for (int i=head[x];i;i=nex[i]){
		int v=to[i];
		if (v==y) continue;
		dfs(v,x);
		s[x]+=s[v];
		
		if (a[v]==a[x]) f[x]+=f[v];
		else f[x]+=f[v]+s[v]*(a[v]^a[x]);
	}
}
void dg(int x,int y){
	for (int i=head[x];i;i=nex[i]){
		int v=to[i];
		if (v==y) continue;
		if (a[x]==a[v]) {
			g[v]=g[x];
		}
		else {
			g[v]=g[x]+((ll)n-2ll*s[v])*(a[x]^a[v]);
		}
		
		dg(v,x);
	}
}
void solve(){
	scanf("%d",&n);
	tot=0;
	fo(i,1,n) head[i]=0;
	
	fo(i,1,n) scanf("%lld",&a[i]);
	fo(i,1,n-1){
		scanf("%d %d",&x,&y);
		add(x,y); add(y,x);
	}
	
	dfs(1,0);
	
	g[1]=f[1];
	dg(1,0);
	
	fo(i,1,n) printf("%lld ",g[i]);
	printf("\n");
}
int main()
{
//	freopen("data.in","r",stdin);

	int T;
	scanf("%d",&T);
	while (T--){
		solve();
	}
	
	return 0;
}

 

标签:head,int,899,ll,Codeforces,tot,Div,include,fo
From: https://www.cnblogs.com/ganking/p/17730904.html

相关文章

  • Codeforces Round 738 (Div. 2) B. Mocha and Red and Blue
    给一个字符串,包含字符\(B\),\(R\),\(?\)。其中\(?\)可能为\(B\)或\(R\)。规定不完美数为字符串中相同字符连续出现的次数,询问一个字符串的最少可能不完美数。观察到\(BR\)或\(RB\)需要尽可能多,于是考虑尽可能让相邻字符不同。容易证明从左往右扫和从右往左扫贡献......
  • Codeforces Round 750 (Div. 2) B. Luntik and Subsequences
    给一个数组\(a_1,a_2,\cdots,a_n\),定义\(s=\sum_{i=1}^{n}a_i\)。询问有多少个\(a\)的子序列满足\(\suma_{i_k}=s-1\)。显然要选出一个\(1\)不加入子序列,任意一个\(0\)可以加入或不加入子序列。于是\(ans=\binom{cnt1}{1}\cdot2^{cnt0}\)。vi......
  • CF1882 div.2 做题记录
    A题面扫一遍,令\(b_i\rightarrowb_{i-1}+1\),若\(b_i=a_i\),\(b_i\rightarrowb_i+1\)。点击查看代码#include<bits/stdc++.h>#defineullunsignedlonglong#definelllonglong#definepiipair<int,int>#definepdipair<double,int>#definepb......
  • Codeforces Round 899 (Div. 2)
    CodeforcesRound899(Div.2)A.IncreasingSequence解题思路:从左往右一个个看,从1开始,如果当前位相同\(+2\),否则\(+1\)。代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;constintN=1e6+10;constintM=2*M;constintmod=1e9+......
  • Educational Codeforces Round 155 D (CF1879_D)
    题目大意给一个长度为\(n\)的数组,求\(\Sigma_{i=1}^{n}\Sigma_{j=i}^{n}区间异或和\times(j-i+1)\)其中\(n\leq3e5,~a[i]\leq1e9\)分析首先注意到由\(l\)到\(r\)的区间异或和可以转化为\(sum_{l-1}~XOR~sum_r\)因此,对于每一个点\(x\),无论它作为上述的\(sum......
  • Educational Codeforces Round 155 (Rated for Div. 2)
    EducationalCodeforcesRound155(RatedforDiv.2)A.Rigged!解题思路:若存在\(s[i]>=s[1]\)并且\(e[i]>=e[i]\),那么答案为\(-1\).否则,答案为\(s[1]\).代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;constintN=1e6+10;con......
  • 短视频app源码,自动滚动条挡住 div内容
    短视频app源码,自动滚动条挡住div内容<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html;charset=utf......
  • Codeforces463-E.Team Work-组合数、DP
    Codeforces463-E.TeamWork题意:求\[\sum_{i=1}^n\binom{n}{i}i^k\]其中\(1\leqn\leq10^9\),\(1\leqk\leq5000\)。题解:其实这个题\(k\)的数据范围就已经暗示了做法的复杂度——应该是要去考虑根据\(k\)去递推了。首先为了方便,\(i=0\)这一项完全可以补上,用类似生成函......
  • Educational Codeforces Round 155 (Rated for Div. 2)
    比赛链接A.Rigged!题目链接就是一个比较简单的模拟就可以解决,如何判断能不能第一只需要考虑比他力量大的耐力是不是也比他大就行,而只要比他大,他就不可能第一,否则输出他的力量作为标杆就行,这样也可以避免比他力量小的也可以举起来。#include<bits/stdc++.h>usingnamespaces......
  • 她是 Codeforces 第四名,也是知名视频平台bilibili的“网红”
    在2023年9月24日~9月25日举办的EducationalCodeforcesRound155(RatedforDiv.2)上,以优秀成绩拿下第四名仅学了ACM一年的Nanani,成为最夺目的选手之一。而且虽然是仅学了一年的选手,但她取得优异成绩后,不少网友并不感到陌生,纷纷留言:这不是bilibili上天天直播爆切神仙题的妹子......