Explanation
Time Complexity:
This problem asks us to find the number that should be added to another number so that it is strictly greater than 2 other numbers. In other words, letting be the values of the given numbers and be the number that will be added, we get the following equation:
Solving this, we get . With this, we can calculate for each of , , and . However, it is important to note that if , then we should output , since the value to add can't be negative.
Implementation
C++
#include <bits/stdc++.h>using namespace std;int main() {int t;cin >> t;for (int tc = 0; tc < t; tc++) {int a, b, c;cin >> a >> b >> c;
Java
import java.io.*;import java.util.*;public class Election {public static void main(String[] args) throws IOException {Kattio io = new Kattio();int t = io.nextInt();for (int tc = 0; tc < t; tc++) {int a = io.nextInt();int b = io.nextInt();
Python
for _ in range(int(input())):a, b, c = map(int, input().split())maxa = max(b, c)maxb = max(a, c)maxc = max(a, b)print(max(0, maxa + 1 - a), max(0, maxb + 1 - b), max(0, maxc + 1 - c))
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!