Codeforces Round 896 (Div. 2)

Dashboard - Codeforces Round 896 (Div. 2) - Codeforces

A. Make It Zero

If you think about it, you will find that if the length is an even number, you can perform two operations. The first time will change all the numbers into the same number, and XOR these same numbers. Since the length is an even number, the final result will be 0. If the length is an odd number, just perform 4 operations. The first operation is to change the first n - 1 numbers to 0. Finally, there is one number that has not changed to 0. You can combine that number with the previous 0 to form an even number. Transform

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
void solve()
{
	int n, a[N], l[N], r[N], ans, cnt;
	ans = 0, cnt = 0;
	cin >> n;
	for(int i = 1; i <= n; i ++)cin >> a[i];
	if((n % 2) == 0)
	{
		cout << 2 << '\n';
		cout << 1 << ' ' << n << '\n';
		cout << 1 << ' ' << n << '\n';
	}
	else
	{
		cout << 4 << '\n';
		cout << 1 << ' ' << n - 1 << '\n';
		cout << 1 << ' ' << n - 1 << '\n';
		cout << n - 1 << ' ' << n << '\n';
		cout << n - 1 << ' ' << n << '\n';
	}
}
int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	while(t --)
	{
		solve();
	}
	return 0;
}

B. 2D Traveling

It can be seen that if there is no big city, the minimum cost from a to b is a to b. If there is a big city between ab, the minimum cost from a to b is the cost from a to the big city plus the cost from b to the big city plus the cost from a to b. Take the minimum cost

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10, inf = 0x3f3f3f;
pair<ll, ll> p[N];
#define x first
#define y second 
void solve()
{
	int n, k, a, b;
	cin >> n >> k >> a >> b;
	for(ll i = 1; i <= n; i ++)
	{
		cin >> p[i].x >> p[i].y;
 	}
 	ll dis1 = 1e18, dis2 = 1e18;
 	for(ll i = 1; i <= k; i ++)
 	{
 		dis1 = min(dis1, abs(p[i].x - p[a].x) + abs(p[i].y - p[a].y));
		dis2 = min(dis2, abs(p[i].x - p[b].x) + abs(p[i].y - p[b].y));
	}

	cout << min(dis1 + dis2, abs(p[a].x - p[b].x) + abs(p[a].y - p[b].y)) << '\n';
}
int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	while(t --)
	{
		solve();
	}
	return 0;
 } 

C. Fill in the Matrix

From the meaning of the question, we know that each row is fully arranged, and each column has a number that cannot appear. To stagger these numbers, you can shift the numbers in one column to the left at a time. If the answer value has been realized, you can just write the other numbers randomly, especially It is judged that when m == 1, the minimum value of this column is also 0, and there is no result, so 0 is output.

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
void solve()
{
	int n, m, a[N];
	cin >> n >> m;
	if(m == 1)
	{
		cout << 0 << '\n';
		for(int i = 1; i <= n; i ++)
		{
			for(int j = 1; j <= m; j ++)
			{
				cout << 0 << ' ';
			}
			cout << '\n';
		}
		return;
	}
		int ans = min(n + 1, m);
		cout << ans << '\n';
		int cnt = 0;
		for(int i = 0; i < m; i ++)a[i] = i;
		int k = n;
		while(k --)
		{
			for(int i = cnt; i < m; i ++)
			{
				cout << a[i] << ' ';
			}
			for(int j = 0; j < cnt; j ++)
			{
				cout << a[j] << ' ';
			}
			cout << '\n';
			if(cnt < m - 2)cnt ++;
		}
	
}
int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	while(t --)
	{
		solve();
	}
	return 0;
}

D1. Candy Party (Easy Version)

Everyone needs to give and get candies, and everyone's final number of candies needs to be the same.

If everyone has the same number of candies, then this number is the average. If the number of candies is the average at the beginning, there is no need to change, because the candies can be given out and given back, and there will still be no change in the end.

Let sum be the sum of the entire array. Only when sum % n == 0, there is a legal solution. Let avg be the average. Let d = vag - x be the difference between this number and the average. Since we Everyone needs to take a candy and everyone has to give a candy, so for everyone we will definitely change as, so in order for x to become vag, it must be satisfied , and such an answer is unique, ( The results obtained by subtracting two quadratic powers will never be the same.

Since everyone will receive candies from exactly one person and give candies to exactly one person, we assume that if a certain person needs to receive x candies and give x candies to achieve the average, then there must be A person who gives a candy corresponds to a person who receives a candy.

For each element x, we find the corresponding (j, k) that satisfies the above equation, and count the odd-even occurrences of each quadratic power. If a quadratic power appears an odd number of times, it is illegal.

Partially passed code: (not sure what the problem is there)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 +10;
ll n, a[N], cnt[65];
void solve()
{
	ll sum = 0;
	cin >> n;
	for(ll i = 1; i <= n; i ++)
	{
		cin >> a[i];
		sum += a[i];
	}
	if(sum % n)
	{
		cout << "NO" << '\n';
		return;	
	}
	ll vag = sum / n, flag = 0;
	for(ll i = 1; i <= n; i ++)
	{
		ll st = 0;
		for(ll j = 0; j <= 60; j ++)
		{
			for(ll k = 0; k <= 60; k ++)
			{
				if(a[i] + (1ll << j) - (1ll << k) == vag)
				{
					st = 1,cnt[j] ^= 1, cnt[k] ^= 1;
				}
			}
		}
		if(!st)flag = 1;
	}
	for(ll i = 0; i <= 60; i ++)
	{
		if(cnt[i] > 0)flag = 1;
		cnt[i] = 0;
	}
	if(flag)cout << "NO" << '\n';
	else cout << "YES" << '\n';
}
int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	ll t;
	cin >> t;
	while(t --)
	{
		solve();
	}
	return 0;
}

おすすめ

転載: blog.csdn.net/m0_75087931/article/details/132810972