1. 程式人生 > 實用技巧 >Codeforces Round 665 賽後解題報告(暫A-B)

Codeforces Round 665 賽後解題報告(暫A-B)

Codeforces Round 665 賽後解題報告

A. Distance and Axis

我們設 \(B\) 點 座標為 \(x(x\leq n)\)。由題意我們知道

\[\mid(n-x)-x\mid=k \]

\[\mid n-2\cdot x \mid=k \]

因此 \(n,k\) 同奇偶。我們分類來討論,如果 \(n<k\),那麼最優方案一定是讓 \(k'=n,x=n\)。這一定是最小的,因為再要滿足條件就 \(k'>n\) 了,肯定不優。若 \(n\geq k\)。則我們只需要滿足同奇偶的條件,判斷一下即可

//Don't act like a loser.
//You can only use the code for studying or finding mistakes
//Or,you'll be punished by Sakyamuni!!!
#include<bits/stdc++.h>
#define int long long
using namespace std;

int read() {
	char ch=getchar();
	int f=1,x=0;
	while(ch<'0'||ch>'9') {
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=x*10+ch-'0';
		ch=getchar();
	}
	return f*x;
}

int n,k;

signed main() {
	int T=read();
	
	while(T--) {
		n=read();k=read();
		
		if(n<k) {
			cout<<k-n<<endl;
		}
		else {
			cout<<(n-k)%2<<endl;
		}
	}
	return 0;
}

B. Ternary Sequence

這個題就是直接貪心討論即可。

我們來分析一下,其實這個題目 \(c_i\) 的計算根本沒那麼麻煩.我們只需貪心計算。我們先儘可能讓 \(b\) 中的 \(2\) 產生最小負面影響。我們第一選擇是拿 \(a\) 中的 \(0\) 一個一個去送死抵消。再拿 \(2\) 去抵消。最後才是 \(1\) 因為只有此時每一對都會產生 \(-2\) 的負面 BUFF。然後我們儘可能多的讓 \(a\) 中的 \(2\) 產生正面 BUFF,和 \(b\) 中的 \(1\) 一個一個配對,剩下的隨意排布都不會產生貢獻或負貢獻,演算法就結束了。

//Don't act like a loser.
//You can only use the code for studying or finding mistakes
//Or,you'll be punished by Sakyamuni!!!
#include<bits/stdc++.h>
#define int long long
using namespace std;

int read() {
	char ch=getchar();
	int f=1,x=0;
	while(ch<'0'||ch>'9') {
		if(ch=='-')
			f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=x*10+ch-'0';
		ch=getchar();
	}
	return f*x;
}

int x[2],y[2],z[2];

signed main() {
	int T=read();
	while(T--) {
		for(int i=0;i<=1;i++) {
			x[i]=read();y[i]=read();z[i]=read();
		}
		
		int ans=0;
		
		int tmp=min(z[1],x[0]);
		z[1]-=tmp;
		x[0]-=tmp;
		if(z[1]) {
			int t1=min(z[1],z[0]);
			z[1]-=t1;
			z[0]-=t1;
			if(z[1]) {
				ans-=z[1]*2;
				y[0]-=z[1];
			}
		}
		
		ans+=min(z[0],y[1])*2;
		
		cout<<ans<<endl;
	}
	return 0;
}