我们可以根据样例三来推出一条规律,对 ,, 中的任意两个数进行操作(这里我假设为 ,)可以得到如下的新数列:,,。 仔细观察一下可以发现,对于 ,(或者 ,)这两个数,它们之间的差值扩大了 3。那么应该可以将其中较小的数变为 0,并使第三个数变大 2。 观察样例一、二可以知道,只要三个数中的任意两个数相等,那么就可以完成问题。 联系之前得到的规律可以推导:
只要三个数模 3 有两个同余,就可以得到答案
那么思路就十分简单了,找余数,再以两个数中的较大数为代价,最后在所有代价中找最小值就可以了。
Code
#include<bits/stdc++.h>
using namespace std;
#define V vector
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T;
cin>>T;
while(T--){
int r,g,b;
V<int>a(3);
cin>>a[0]>>a[1]>>a[2];
sort(a.begin(),a.end());//规定大小更好判断
r=a[0],g=a[1],b=a[2];
V<int>ans;
int r1=r%3,r2=g%3,r3=b%3;
if(r1==r2) ans.push_back(g);//同余
if(r2==r3) ans.push_back(b);
if(r1==r3) ans.push_back(b);
if(ans.empty()){//无同余
ans.push_back(-1);
}
sort(ans.begin(),ans.end());
cout<<ans[0]<<"\n";
}
return 0;
}
/*
1
1 3 4
*/