Codeforces Round #665 (Div. 2) A ~ C
A. Distance and Axis
题意 :
在x正方向上给定一个点A的坐标和一个距离k, 问能否找到一个B点使得 |dAB - dOB| = k. (B 的坐标为整数)
思路 :
设A(y, 0), B(x, 0).
分情况 :
 (1) 若 y < k, 直接让A运动到k, 此时B点取为0点.
 (2) 若 y >= k, 需要满足 y - 2x = k, 但是注意x必须取到整数.
AC代码 :
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
int a, b;
 
void run() {
    int ans;
    if (a >= b) {
        int t = a - b;
        if (t & 1) ans = 1;
        else ans = 0;
    } else ans = b - a;
    cout << ans << endl;
}
 
int main() {
	IO;
	int _;
	cin >> _;
	while (_--) {
		cin >> a >> b;
		run();
	}
	return 0;
}
B. Ternary Sequence
题意 :
给出A, B两个n = 3的数组描述两个序列, 每个数组中的元素都表示对应序列中0, 1, 2的个数, 两个序列内部可以随意排列, 定义一个新序列c, ci 定义如下 :
\[C_i =\left\{
\begin{array}{rcl}
a_ib_i    &      & {a_i      >      b_i}\\
0         &      & {a_i = b_i}\\
-a_ib_i   &      & {a_i < b_i}\\
\end{array} \right.
\]
求\(\sum_{i=1}^n\)Ci 的最大值.
思路 :
贪心 : 让A中尽量多的2能与B中的1组合, 让A中尽量少的1与B中的2组合.
AC代码 :
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
int a[3];
int b[3];
 
int main() {
	IO;
	int _;
	cin >> _;
	while (_--) {
		for (int i = 0; i < 3; ++i) cin >> a[i];
		for (int i = 0; i < 3; ++i) cin >> b[i];
		ll ans = 0;
		int t = min(a[2], b[1]);
		a[2] -= t;
		b[1] -= t;
		ans += t * 2;
        
        t = max(b[2] - a[0] - a[2], 0);
        t = min(t, a[1] - b[0] - b[1]);
        t = max(0, t);
        ans -= t * 2;
 
		cout << ans << endl;
	}
	return 0;
}
C. Mere Array
题意 :
对于给定的序列, 如果gcd(ai, aj) = min{a1, a2, a3, ......an}, 那么ai, aj可交换, 问能否使序列变成不减序列.
思路 :
通过最小值m, 相当于所有m的倍数都可以任意交换, 于是可以将m的倍数都取出排好序再放回原序列, 看看是否满足条件即可.
AC代码 :
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 1e5 + 7;
int a[N];
int st[N];
int main() {
	IO;
	int _;
	cin >> _;
	while (_--) {
		memset(st, 0, sizeof st);
		int n, flag = 1, m = inf;
		cin >> n;	
		for (int i = 1; i <= n; ++i) {
			cin >> a[i];
			if (a[i] < a[i - 1]) flag = 0;
			m = min(m, a[i]);
		}
		if (flag) {
			puts("YES");
			continue;
		}
		vector<int> v;
		for (int i = 1; i <= n; ++i) 
			if (a[i] % m == 0) {
				st[i] = true;
				v.pb(a[i]);
			}
		sort(v.begin(), v.end());
		int cnt = 0;
		for (int i = 1; i <= n; ++i) 
			if (st[i]) a[i] = v[cnt++];
			
		flag = 1;
		for (int i = 1; i <= n; ++i)
			if (a[i] < a[i - 1]) {
				flag = 0;
				break;
			}
			
	   
		if (flag) puts("YES");
		else puts("NO");
	}
	return 0;
}

                
            
        
浙公网安备 33010602011771号