Codeforces Round #656 (Div. 3) A. Three Pairwise Maximums

A. Three Pairwise Maximums

题目链接-A. Three Pairwise Maximums
Codeforces Round #656 (Div. 3) A. Three Pairwise Maximums_第1张图片
Codeforces Round #656 (Div. 3) A. Three Pairwise Maximums_第2张图片
题目大意
给你三个正整数x、y和z,请你找到正整数a,b和c,使得 x = m a x ( a , b ) , y = m a x ( a , c ) x=max(a,b),y=max(a,c) x=max(ab)y=max(ac) z = m a x ( b , c ) z=max(b,c) z=max(bc),或者确定不可能找到这样的 a , b a,b ab c c c(你可以以任意顺序输出 a , b , c a,b,c a,b,c)

解题思路

  • 因为 x = m a x ( a , b ) , y = m a x ( a , c ) x=max(a,b),y=max(a,c) x=max(ab)y=max(ac) z = m a x ( b , c ) z=max(b,c) z=max(bc),我们可以得出 x , y , z x,y,z x,y,z中至少有两个数是相等的,且一定大于等于另一个数
  • 如果x==y则说明 a a a为最大值,此时需要满足a>=z,如果不满足该条件,则无解,因为 z = m a x ( b , c ) z=max(b,c) z=max(bc),我们不能确定 b , c b,c b,c谁比较大,所以我们就假设两个数一样的即可。
  • 记住输出时不能输出 b , c b,c b,c z z z z − 1 z-1 z1,因为 a , b , c a,b,c a,b,c需要是正整数,如果 z = 1 z=1 z=1,此时的 z − 1 z-1 z1是不满足条件的
  • x==z时和y==z时同理
  • 具体操作见代码

附上代码

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double e=exp(1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=2e5+10;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	int t;
	cin>>t;
	while(t--){
		int a=0,b=0,c=0,x,y,z;
		cin>>x>>y>>z;
		if(x==y) a=x;
		else if(y==z) c=y;
		else if(x==z) b=x;
		if((a&&a>=z)||(b&&b>=y)||(c&&c>=x)){
			cout<<"YES"<<endl;
			if(a) cout<<a<<" "<<z<<" "<<z<<endl;
			else if(b) cout<<b<<" "<<y<<" "<<y<<endl;
			else if(c) cout<<c<<" "<<x<<" "<<x<<endl;
		}
		else
			cout<<"NO"<<endl;
	}
    return 0;
}

你可能感兴趣的:(codeforces)