There are N characters, each with an attack and defense power.
The ability value of a character is the sum of the squares of its attack and defense powers. Output the attack and defense values of the character with the second-largest ability value.
Test cases ensure that the ability values of each character are distinct.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line contains an integer N (3 ≤ N ≤ 20), indicating the number of characters. Following are N lines, each containing two integers ai and bi (1 ≤ ai, bi ≤ 100), representing the attack and defense values of the i-th character. | Output two integers, representing the attack and defense values of the character with the second-highest ability value. |
3 3 1 5 2 1 4 | 1 4 |
6 6 6 1 3 8 6 5 4 2 8 7 2 | 6 6 |
5 34 35 84 32 39 79 59 89 59 31 | 84 32 |
Thought Process
You can store the sum of squares of a and b in a vector or array and sort it. Additionally, because there won't be any duplicate sum of squares, you can use a map to store a and b, where the key is the sum of squares of a and b, and the value is a pair of integers (a,b). When outputting, select the second-largest sum of squares and output its corresponding a and b from the map.
Sample Code-ZeroJudge M931: Game Cast
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
vector<int>num;
map<int, pair<int, int>>MAP;
for (int i = 0; i<N; i++)
{
int a, b;
cin >> a >> b;
int tmp = (a*a) + (b*b);
num.push_back(tmp);
MAP[tmp].first = a;
MAP[tmp].second = b;
}
sort(num.begin(), num.end());
int tmp = num[num.size()-2];
cout << MAP[tmp].first << " " << MAP[tmp].second << "\n";
}
//ZeroJudge M931
//Dr. SeanXD