Codeforces Round # 633 (Div. 1) Solution (ABC)

Contest link: https://codeforces.com/contest/1338

A. Powered Addition

Lemma: A positive integer \ (a \) can be converted into any number within \ ([a, a + 2 ^ k-1] \) in \ (k \) seconds

Since it will eventually become a non-decreasing sequence, we only need to take the maximum value of ( minimum padding amount of each element) \ (S \) , so that the minimum integer power of 2 greater than \ (S \) is \ (2 ^ k \ ) , According to the lemma, the answer is \ (k \) qwq

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0);
const int N=200010; typedef long long ll; const int inf=~0u>>2; ll read(){ll x; if(scanf("%lld",&x)==-1)exit(0); return x;}
#define int ll
int a[N];
int ans,n;
signed main(){
	int T; cin>>T;
	while(T--){
		cin>>n;
		repeat(i,0,n)cin>>a[i];
		int x=-inf;
		ans=0;
		repeat(i,0,n){
			if(a[i]<x)ans=max(ans,x-a[i]);
			x=max(x,a[i]);
		}
		int cnt=0;
		while(ans)ans>>=1,cnt++;
		cout<<cnt<<endl;
	}
	return 0;
}

B. Edge Weight Assignment

A little trick is the xor sum of the path between the two nodes, that is, the xor sum of the path xor and the path from the two nodes to the root

We take a node with a degree of 1 as the root. The advantage is that there is no need to verify the path of any two leaves. Just verify that the xor sum of all leaves to the root is 0 (according to the tips)

The first subtask is very simple, the answer can only be 1 or 3, divided into two cases (see if the root degree is 1)

If the degree of the root is 1, the depth of all leaves is even, the answer is 1

If the degree of the root is not 1, the depth parity of all leaves is the same, the answer is 1

When it comes to the second subtask, after one operation is fierce, it is found that in most cases, any two edge weights are not equal, but if many leaves are connected to a node, the edge weights of these edges are forced to be equal. can

In addition, if the node with degree 1 is taken as the root, since this node is originally a leaf, special judgment is required, otherwise, no special judgment is required

I took the node with degree 1 as the root here

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0);
const int N=200010; typedef long long ll; const int inf=~0u>>2; ll read(){ll x; if(scanf("%lld",&x)==-1)exit(0); return x;}
//#define int ll
vector<int> a[N];
int rt,ans;
int deg[N],cnt[N],depth[N];
int flag=true;
void dfs(int x,int fa){
	for(auto p:a[x])
	if(p!=fa){
		depth[p]=depth[x]+1;
		dfs(p,x);
		cnt[x]+=deg[p]==1;
	}
	if(deg[x]==1 && depth[x]%2==1)flag=false;
	if(depth[x]==1)ans-=cnt[x];
	else ans-=max(cnt[x]-1,0);
}
signed main(){
	int n=read();
	repeat(i,0,n-1){
		int x=read()-1,y=read()-1;
		a[x].push_back(y); a[y].push_back(x);
		deg[x]++,deg[y]++;
	}
	repeat(i,0,n){
		if(deg[i]==1){rt=i; break;}
	}
	dfs(rt,-1);
	ans+=n-1;
	cout<<(flag?1:3)<<' '<<ans<<endl;
	return 0;
}

C. Perfect Triples

I will list a few first (binary)

1 10 11
100 1000 1100
101 1010 1111
110 1011 1101
111 1001 1110
10000 100000 110000
10001 100010 110011
10010 100011 110001
10011 100001 110010
10100 101010 111110
...

What did you find?

That ’s right, I call it 0231 law (fog), that is, if the number of the first column is determined, the number of the second column can be obtained by the following method: first converted to hexadecimal, then \ ([0,1,2 , 3] \) correspondingly becomes \ ([0,2,3,1] \)

It's perfect, you know, the number of the first column is very regular (a lot of continuous), the number of the third column is obtained from the first column xor the second column, so the title of the water is actually placed at the position of div1C

Variable names are really hard to get, so the spicy chicken code warns

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
int cansel_sync=(ios::sync_with_stdio(0),cin.tie(0),0);
const int N=200010; typedef long long ll; const int inf=~0u>>2; ll read(){ll x; if(scanf("%lld",&x)==-1)exit(0); return x;}
#define int ll
int ss[4]={0,2,3,1};
int f(int x){
	if(x==0)return 0;
	return f(x/4)*4+ss[x%4];
}
signed main(){
	int T=read();
	while(T--){
		int n=read()-1; if(n<3){cout<<n+1<<endl; continue;}
		int p=n/3;
		int x;
		for(x=1;p>=0;p-=x,x*=4);
		x/=4; p+=x;
		if(n%3==0){cout<<p+x<<endl; continue;}
		int t1=x+p;
		int t2=f(t1);
		if(n%3==1)cout<<t2<<endl;
		else cout<<(t1^t2)<<endl;
	}
	return 0;
}

Guess you like

Origin www.cnblogs.com/axiomofchoice/p/12688806.html