描述
Three friends gathered to play a few games of chess together.
In every game, two of them play against each other. The winner gets?2?points while the loser gets?0, and in case of a draw, both players get?1?point each. Note that the same pair of players could have played any non-negative number of times (possibly zero). It is also possible that no games were played at all.
You've been told that their scores after all the games were played were?p1?,?p2??and?p3?. Additionally, it is guaranteed that?p1?≤p2?≤p3??holds.
Find the maximum number of draws that could have happened and print it. If there isn't any way to obtain?p1?,?p2??and?p3??as a result of a non-negative number of games between the three players, print??1?instead.
輸入描述
Each test contains multiple test cases. The first line contains the number of test cases?t?(1≤t≤500). The description of the test cases follows.
The first line of each test case contains three integers?p1?,?p2??and?p3??(0≤p1?≤p2?≤p3?≤30) — the scores of the three players, sorted non-decreasingly.
輸出描述
For each testcase, print one number — the maximum possible number of draws that could've happened, or??1?if the scores aren't consistent with any valid set of games and results.
用例輸入 1?
7 0 0 0 0 1 1 1 1 1 1 1 2 3 3 3 3 4 5 1 1 10
用例輸出 1?
0 1 -1 2 -1 6 2
提示
In the first example, no games were played at all, so no draws could occur either.
For the second example, exactly one game occurred between the second and the third player and it ended in draw, so the answer is?1.
It's easy to see that there's no set of games achieving the scores in third example, so the answer for it is??1.
翻譯:
描述
三個朋友聚在一起下了幾盤棋。
在每場比賽中,他們中的兩個人相互對抗。獲勝者將獲得2積分,而失敗者獲得0,如果出現平局,雙方都會得到1點每個。請注意,同一對玩家可以玩任何非負數(可能為零)。也有可能根本沒有玩任何游戲。
你被告知,他們在所有比賽結束后的得分是p1?,p2?和p3?.此外,還保證p1?≤p2?≤p3?保持。
找到可能發生的最大抽獎次數并打印出來。如果沒有任何方法可以獲得p1?,p2?和p3?由于三名玩家之間的游戲數不為負數,請打印?1相反。
輸入描述
每個測試都包含多個測試用例。第一行包含測試用例的數量t?(1≤噸≤500).測試用例的描述如下。
每個測試用例的第一行包含三個整數p1?,p2?和p3??(0≤p1?≤p2?≤p3?≤30) — 三名玩家的分數,不遞減排序。
輸出描述
對于每個測試用例,打印一個數字 — 可能發生的最大繪制次數,或者?1如果分數與任何一組有效的游戲和結果不一致。
用例輸入 1?
7
0 0 0
0 1 1
1 1 1
1 1 2
3 3 3
3 4 5
1 1 10
用例輸出 1?
0
1
-1
2
-1
6
2
提示
在第一個示例中,根本沒有進行任何游戲,因此也不會發生平局。
對于第二個例子,第二位和第三位玩家之間恰好發生了一場比賽,并以平局告終,所以答案是1.
不難看出,在第三個例子中,沒有一組游戲能達到分數,所以答案是?1.
解題思路:
1. 當平局時每人加1分,也就是本來贏得2分平分了,所以無論怎么打,只要有答案總分數就是偶數
2.因為要計算平局最大數,所以就將最大的兩個-1,直到減不了為止
c++ 代碼如下:
#include <bits/stdc++.h>using namespace std;int main()
{int n;cin >> n;while(n--){int res = 0;int a,b,c;cin >> a >> b >> c;int sum = a + b + c;if(sum%2 != 0){cout << -1 << endl;}else{//初始化setmultiset<int> s;s.insert(a);s.insert(b);s.insert(c);//初始化循環auto t1 = --s.end();auto t2 = --s.end();--t2;int num1 = *t1;int num2 = *t2;while(num2 >=1 && num1 >= 1){//更改元素--num1;--num2;s.erase(--s.end());s.erase(--s.end());s.insert(num1);s.insert(num2);//結果計數++res;//更新數據t1 = --s.end();t2 = --s.end();--t2;num1 = *t1;num2 = *t2;}cout << res << endl;}}
}