CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes) A~E

A. Good Pairs

题意
给一个数组,要求在其中选出两个数,满足以下公式
∣ a i − a k ∣ + ∣ a k − a j ∣ = ∣ a i − a j ∣ |a_i - a_k| + |a_k - a_j| = |a_i - a_j| aiak+akaj=aiaj
i,j为选出的数的下标,对于所以的 1 ≤ k ≤ n 1\le k\le n 1kn都满足,输出两数下标。
思路
刚开始猜了一下选最大和最小的,答案也确实如此,要想左边的式子等于右边的,那么就要把k消掉,很明显把绝对值符号去掉,两个 a k a_k ak可以直接抵消,那么只需保证 a i a_i ai最大 a j a_j aj最小。

// Problem: A. Good Pairs
// Contest: Codeforces - CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes!)
// URL: https://codeforces.com/contest/1656/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms

#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int mod=1000000007 , N = 1e5 + 10;
const double eps=1e-8;
int n;
struct node
{
	int x,id;
}a[N];
bool cmp(node a,node b)
{
	return a.x < b.x;
}
int main(){
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i = 1 ; i <= n ; i ++ )	cin>>a[i].x , a[i].id = i;
		sort(a+1,a+n+1,cmp);
		cout<<a[1].id<<" "<<a[n].id<<"\n";
	}
	return 0;
} 

B. Subtract Operation

题意
给一个长度为 n n n的数组和一个正整数 k k k,每次操作可以删掉数组中的一个数,然后把数组中的其他数的值都减去这个数,进行 n − 1 n-1 n1次操作,问是否能得到 k k k
思路
对于任意三个数的情况, a i , a j , a k a_i,a_j,a_k ai,aj,ak,减掉任意一个数即剩余 a i − a k , a j − a k a_i-a_k,a_j-a_k aiak,ajak,然后剩余两数相减 a i − a j a_i-a_j aiaj,发现剩余的并没有 a k a_k ak的存在,也就是无关删除过程,那么对于第 n − 1 n-1 n1次操作,要求剩下的两个数相减得到k,只需要找是否存在两数的差值为k即可。

// Problem: B. Subtract Operation
// Contest: Codeforces - CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes!)
// URL: https://codeforces.com/contest/1656/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms

#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int mod=1000000007 , N = 2e5 + 10;
const double eps=1e-8;
int t,n,k;
int a[N];
int main(){
	cin>>t;
	while(t--)
	{
		cin>>n>>k;
		map<int,int> mp;
		for(int i = 0 ; i < n ; i ++ )	cin>>a[i],mp[a[i]] ++ ;
		sort(a,a+n);
		bool flag = false;
		for(int i = 0 ; i < n ; i ++ )
		{
			if(mp[k+a[i]])	
			{
				flag = true;
				break;
			}
		}
		if(flag)	cout<<"YES\n";
		else cout<<"NO\n";
	}
	return 0;
}

C. Make Equal With Mod

题意:
给一个长度为n的数组,每次对于所有的数都进行一次取模操作,取模的值要大于等于2,问是否能经过一系列操作后使数组中的所有数都相等。
思路
关键问题在于对1和0的处理,假如数组中不存在1的情况,可以每次模上数组中最大的数,这样最后得到全部为0,对于既存在1又存在0的情况,显然是无法达到要求的,因为1和0都无法通过取模再次变换了,对于存在1而不存在0的情况,可以对每个数从大到小,依次模上比它大1的数,可以将数组全部化为1,那么就不能又两个数差值为1的情况出现。

// Problem: C. Make Equal With Mod
// Contest: Codeforces - CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes!)
// URL: https://codeforces.com/contest/1656/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms

#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int mod=1000000007 , N = 1e5 + 10;
const double eps=1e-8;
int a[N];
int main(){
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		bool flag = false;
		for(int i = 1 ; i <= n ; i ++ )
		{
			cin>>a[i];
			if(a[i] == 1)	flag = 1;	
		}
		sort(a+1,a+n+1);
		if(flag && a[1] == 0)	
		{
			cout<<"NO\n";
			continue;
		}
		if(a[1] == a[n])	
		{
			cout<<"YES\n";
			continue;
		}
		if(flag)
		{
			bool flag1 = false;
			for(int i = n ; i > 1 ; i -- )
			{
				if(a[i] - a[i-1] == 1)
				{
					flag1 = true;
					break;
				}
			}	
			if(flag1)	cout<<"NO\n";
			else cout<<"YES\n";
		}
		else cout<<"YES\n";
	}
	return 0;
}

D. K-good

题意:给一个正整数n,需要找一个整数k( 2 ≤ k 2\le k 2k),使由k个正整数相加的值等于n,且这些数对于k的取模的值都不相同。
思路:显然这 k k k个数对 k k k的值就是区间 [ 0 , k − 1 ] [0,k-1] [0,k1]的所有数,那么每个数都可以分解为 a i = m ∗ k + a i 模 k a_i = m*k + a_i 模 k ai=mk+aik,因为 a i a_i ai不为0,那么m至少取1,
那么可以得到等式
n = m ∗ k + k ∗ ( k − 1 ) 2 n = m*k + \frac{k*(k-1)}{2} n=mk+2k(k1) , (m不等同于上面的m)
2 ∗ n = k ∗ ( 2 ∗ m + k − 1 ) 2*n = k*(2*m + k-1) 2n=k(2m+k1)
等式右边,当k取偶数时,括号内取奇数,当k取奇数时,括号内取偶数,
而且我们知道,括号内的值要大于k,
同样等式左边可变为 2 ∗ 2 x ∗ 奇 数 2*2^x*奇数 22x,到此我们就可知k的值就在 2 ∗ 2 x 2*2^x 22x 奇 数 奇数 中取一个较小值。

// Problem: D. K-good
// Contest: Codeforces - CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes!)
// URL: https://codeforces.com/contest/1656/problem/D
// Memory Limit: 256 MB
// Time Limit: 3000 ms

#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int mod=1000000007;
const double eps=1e-8;
int main(){
	int t;
	cin>>t;
	while(t--)
	{
		ll n;
		cin>>n;
		ll k = 1;
		while(n % 2 == 0)	n /= 2 , k *= 2;
		if(n == 1)
		{
			cout<<"-1\n";
			continue;
		}
		if(k * 2 < n)	cout<<k*2<<"\n";
		else cout<<n<<"\n";
	}
	return 0;
}

E. Equal Tree Sums

题意:给定一棵无向无根树,也就是无环的连通图,每个节点有一个权值,现在我们要对节点分配一个权值,使得无论删除哪一个点,剩余的每个连通块权值之和相等。
思路;从一个点开始dfs,染白色,每个白点的相临点染黑色,同理,黑点的相邻点是白色,权值是它的度,那么整棵树就是0,假如删掉这个点,变成了多个连通块,这些连通块的值都是统一的1或-1.
一些证明还没想明白,待补。
这里给一个ben佬的讲解,讲得很好,推荐大家去听听。
https://www.bilibili.com/video/BV1JZ4y1z7v8?p=5

// Problem: E. Equal Tree Sums
// Contest: Codeforces - CodeTON Round 1 (Div. 1 + Div. 2, Rated, Prizes!)
// URL: https://codeforces.com/contest/1656/problem/E
// Memory Limit: 256 MB
// Time Limit: 1000 ms

#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int mod=1000000007 , N = 1e5 + 10;
const double eps=1e-8;
vector<int> Edge[N];
int f[N];
int n;
void dfs(int x,int pre,int val)
{
	f[x] = Edge[x].size() * val;
	for(auto v : Edge[x])
	{
		if(v == pre)	continue;
		dfs(v,x,-val);
	}
}
int main(){
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n; 
		for(int i = 1 ; i <= n ; i ++ ) Edge[i].clear();
		for(int i = 1 ; i < n ; i ++ )
		{
			int a,b;
			cin>>a>>b;
			Edge[a].push_back(b);
			Edge[b].push_back(a);
		}
		dfs(1,0,1);
		for(int i = 1 ; i <= n ; i ++ )	cout<<f[i]<<" ";
		cout<<"\n";
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值