Official

B - Various distances Editorial by evima


Use the formulae provided in the problem statement and output the result. In most languages, the operations finding values such as the absolute value, the square, or the maximum value, are provided as standard functions, so take a look at their usages.

Sample Code (C++)

#include <bits/stdc++.h>
using namespace std;
 
int main(){
	cout << fixed << setprecision(15);

	int n;
	cin >> n;
	vector<int>x(n);
	for(int i=0;i<n;i++)cin >> x[i];

	long ans1=0;
	for(int i=0;i<n;i++)ans1+=abs(x[i]);
	cout << ans1 << endl;

	long ans2=0;
	for(int i=0;i<n;i++)ans2+=abs(x[i])*(long)abs(x[i]);
	cout << sqrt(ans2) << endl;

	int ans3=0;
	for(int i=0;i<n;i++)ans3=max(ans3,abs(x[i]));
	cout << ans3 << endl;
}

Be careful of overflows and precision errors (output formats).

About overflows

A 32-bit signed integer (e.g. the int type in C/C++ in the AtCoder environment) cannot represent values more than (or equal to) \(2^{31} \fallingdotseq 2.1\times 10^9\). Instead, one should use 64-bit signed integer (e.g. the long type in C/C++ in the AtCoder environment).

C++ quiz: among the four outputs below, how many of them correctly outputs “10000000000”?

#include <bits/stdc++.h>
using namespace std;

int main(){
	cout << (100000 * 100000) << endl;
	int x=100000;
	cout << (x*x) << endl;
	long y=x*x;
	cout << y << endl;
	long z=x*(long)x;
	cout << z << endl;	
}

About precision errors

A double-precision floating-point number (e.g. the double type) has a precision of 15 to 16 digits in its decimal notation. In most cases you do not have to worry about precision errors, but be careful.

Sample Problem 1

Sample Problem 2

Additionally, even if the calculation result itself is correct, you may still get a WA verdict because of lack of the number of digits of output. Check how you can specify the number of digits of output.

#include <bits/stdc++.h>
using namespace std;

int main(){
	cout << 0.1234567890123;  // In AtCoder environment, it shows 0.123457
}

posted:
last update: