1. 程式人生 > 實用技巧 >Codeforces Round #665 Div.2 (CF1401)

Codeforces Round #665 Div.2 (CF1401)

A:

在一維座標的\(OX\)象限中,給定\(A,k\),可以讓\(A\)左移或右移1個單位任意次,求能找到點\(B\)、使得\(|OB-AB|=k\)的最小移動次數。


\(A < k\)時,需要將\(A\)移至\(k\),此時\(B=0\)\(B=k\),所以答案為\(k-n\)

\(A >= k\)時,化簡\(|OB-AB|=k\)\((A-B)-(B-O)=k\),即\(B=(A-k)/2\),當\(A, k\)奇偶性不同時,\(B\)為小數,這是需要把\(A\)右移一單位,答案為\(1\),否則答案為\(0\)

Code:

/*
	ID: Loxilante
	Time: 2020/08/21
	Prog: CF1401A
	Lang: cpp
*/
#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3")
#endif
#include <bits/extc++.h>
#define rep(i, l, r) for(int i = l; i < r; i++)
#define hrp(i, l, r) for(int i = l; i <= r; i++)
#define rev(i, r, l) for(int i = r; i >= l; i--)
#define ms(n, t) memset(n, t, sizeof(n))
#define pb push_back
#define int ll
#ifndef JOEON
#define D(...) 97
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template<typename tn = int> inline tn next(void) { tn k; cin>>k; return k; }
signed main(void)
{
	clock_t Begin = clock();

	#ifdef JOEON
//		freopen("C:\\Users\\Joeon\\Desktop\\IN.txt", "r", stdin);
//		freopen("C:\\Users\\Joeon\\Desktop\\OUT.txt", "w", stdout);
	#endif
	
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int T = next();
	
	clock_t InputFinished = clock();
	
	while(T--)
	{
		int n, k;
		cin>>n>>k;
		if (k > n) cout<<k-n<<endl;
		else cout<<(n%2 != k%2)<<endl;
	}
	
	clock_t End = clock();
	
	D((double)(End-Begin)/CLOCKS_PER_SEC);
	D((double)(End-InputFinished)/CLOCKS_PER_SEC);
	
	return 0;
}


B:

給你兩個長度相等的數列\(a, b\),數列元素順序可以隨意調換,\(a\)數列有且僅有\(a0, a1, a1\)\(0, 1, 2\)\(b\)數列有且僅有\(b0, b1, b2\)\(0, 1, 2\)。現有一個\(c\)序列,\(c_i=\)

  1. \(a_i*b_i (a_i > b_i)\)
  2. \(0 (a_i = b_i)\)
  3. \(-a_i*b_i (a_i < b_i)\)

求最大的$ \sum _{i=1} ^{n} c_i$。


這是一個明顯的貪心題,把\(b2\)\(a0\)\(a2\)抵消,如果剩下\(a2\)就輸出\(min\{a2, b1\}\)

,如果剩下\(b2\)就輸出\(-2*b2\)

Code:

/*
	ID: Loxilante
	Time: 2020/08/14
	Prog: CF1401B
	Lang: cpp
*/
#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3")
#endif
#include <bits/extc++.h>
#define rep(i, l, r) for(int i = l; i < r; i++)
#define hrp(i, l, r) for(int i = l; i <= r; i++)
#define rev(i, r, l) for(int i = r; i >= l; i--)
#define ms(n, t) memset(n, t, sizeof(n))
#define pb push_back
#define int ll
#ifndef JOEON
#define D(...) 97
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template<typename tn = int> inline tn next(void) { tn k; cin>>k; return k; }
signed main(void)
{
	clock_t Begin = clock();

	#ifdef JOEON
//		freopen("C:\\Users\\Joeon\\Desktop\\IN.txt", "r", stdin);
//		freopen("C:\\Users\\Joeon\\Desktop\\OUT.txt", "w", stdout);
	#endif
	
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int T = next<int>();
	
	clock_t InputFinished = clock();
	
	while(T--)
	{
		int a0, a1, a2, b0, b1, b2;
		cin>>a0>>a1>>a2>>b0>>b1>>b2;
		if (a0 > b2) a0 -= b2, b2 = 0;
		else b2 -= a0, a0 = 0;
		if (b2)
		{
			if (a2 > b2) a2 -= b2, b2 = 0;
			else b2 -= a2, a2 = 0;
		}
		if (a2) cout<<min(a2, b1)*2<<endl;
		else cout<<-2*b2<<endl;
	}
	
	clock_t End = clock();
	
	D((double)(End-Begin)/CLOCKS_PER_SEC);
	D((double)(End-InputFinished)/CLOCKS_PER_SEC);
	
	return 0;
}


C:

給你一個序列\(a\),其中最小的元素的值為\(a_min\),若\(gcd(a_i, a_j) = a_{min}\),則可交換\(a_i, a_j\)


把序列\(a\)先排好序變成序列\(b\),如果\(a_i ≡ b_i (mod a_{min})\),則可以交換,否則輸出NO。

Code:

/*
	ID: Loxilante
	Time: 2020/08/21
	Prog: CF1401C
	Lang: cpp
*/
#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3")
#endif
#include <bits/extc++.h>
#define rep(i, l, r) for(int i = l; i < r; i++)
#define hrp(i, l, r) for(int i = l; i <= r; i++)
#define rev(i, r, l) for(int i = r; i >= l; i--)
#define ms(n, t) memset(n, t, sizeof(n))
#define pb push_back
#define int ll
#ifndef JOEON
#define D(...) 97
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template<typename tn = int> inline tn next(void) { tn k; cin>>k; return k; }
const int U = 1e6;
int w[U], p[U];
signed main(void)
{
	clock_t Begin = clock();

	#ifdef JOEON
//		freopen("C:\\Users\\Joeon\\Desktop\\IN.txt", "r", stdin);
//		freopen("C:\\Users\\Joeon\\Desktop\\OUT.txt", "w", stdout);
	#endif
	
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int T = next();
	
	clock_t InputFinished = clock();
	
	while(T--)
	{
		int l = next();
		bool yes = 1;

		rep(i, 0, l) cin>>w[i], p[i] = w[i];

		rep(i, 1, l) if (w[i] < w[i-1]) {yes = 0; break;}
		if (yes) { cout<<"YES"<<endl; continue; }

		sort(p, p+l);

		int minn = p[0], no = 0, t;
		rep(i, 0, l)
		{
			t = __gcd(p[i], w[i]);
			if (t == p[i]) continue;
			if (t % minn != 0) { no = 1; break; }
		}
		if (no) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}
	
	clock_t End = clock();
	
	D((double)(End-Begin)/CLOCKS_PER_SEC);
	D((double)(End-InputFinished)/CLOCKS_PER_SEC);
	
	return 0;
}

D:

\(\color{red}{PigeonGugugu}\)