Problem:
年齢の差 (Age Difference)
問題文
JOI 市には
1
から
N
までの番号が付けられた
N
人の住民がおり，住民
i
(
1 ≦ i ≦ N
) の年齢は
A
i
歳である．
JOI 市の住民の年齢
A
1
, A
2
, …, A
N
が与えられる．
i = 1, 2, …, N
に対して，住民
i
と他の住民との年齢の差の最大値を求めるプログラムを作成せよ．
制約
2 ≦ N ≦ 250 000
．
0 ≦ A
i
≦ 10
9
(
1 ≦ i ≦ N
)．
入力される値はすべて整数である．
入力
入力は以下の形式で与えられる．
N
A
1
A
2
…
A
N
出力
N
行出力せよ．
i
行目 (
1 ≦ i ≦ N
) には，住民
i
と他の住民との年齢の差の最大値を出力せよ．
入出力例
入力例 1
3
13 15 20
出力例 1
7
5
7
住民
1
と住民
2, 3
との年齢の差はそれぞれ
2, 7
歳である．これらの最大値は
7
歳であるから，
1
行目には
7
を出力する．
住民
2
と住民
1, 3
との年齢の差はそれぞれ
2, 5
歳である．これらの最大値は
5
歳であるから，
2
行目には
5
を出力する．
住民
3
と住民
1, 2
との年齢の差はそれぞれ
7, 5
歳である．これらの最大値は
7
歳であるから，
3
行目には
7
を出力する．
入力例 2
2
100 100
出力例 2
0
0
住民
1
と住民
2
との年齢の差は
0
歳である．したがって，
1
行目には
0
を出力する．
住民
2
と住民
1
との年齢の差は
0
歳である．したがって，
2
行目には
0
を出力する．
入力例 3
10
440894064 101089692 556439322 34369336 98417847 216265879 623843484 554560874 247445405 718003331
出力例 3
406524728
616913639
522069986
683633995
619585484
501737452
589474148
520191538
470557926
683633995
情報オリンピック日本委員会
作 『第 22 回日本情報オリンピック JOI 2022/2023 二次予選競技課題』

Submitted solutions:

code_snippet: //NOAC 25..
#include <bits/stdc++.h>

#define endl "\n"
#define sline cout << endl
#define ll long long
#define ull unsigned long long
#define fi int i =
#define fj int j =

using namespace std;

int main() {
    int N;
    cin >> N;
    vector <ll> A(N);
    ull sum = 0;
    for (fi 0; i < N; i++) {
        cin >> A[i];
        sum += A[i];
    }

    ull ave = sum / N;
    vector <ll> ans(N);
    int Amax = *max_element(A.begin(), A.end());
    int Amin = *min_element(A.begin(), A.end());

    for (fi 0; i < N; i++) {
        /*
        if (A[i] >= ave) {
            ans[i] = A[i] - min;
        }
        else {
            ans[i] = max - A[i];
        }
        */
        ans[i] = max(A[i] - Amin, Amax - A[i]);
    }

    for (fi 0; i < N; i++) {
        cout << ans[i] << endl;
    }

return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 7024
final_rank: 12

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;

int N;
long long A[250010];
long long B[250010];

int main()
{
    cin >> N;
    for (int i = 0; i < N; i++)
        cin >> A[i];
    for (int i = 0; i < N; i++)
        B[i] = A[i];

    sort(A, A + N);
    for (int i = 0; i < N; i++) {
        long long max_number = max(abs(B[i] - A[0]), abs(B[i] - A[N - 1]));
        cout << max_number << endl;
    }
        
    
}
accuracy: 1
time_ms: 240
memory_kb: 7228
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include<string>
#include<stack>
#include<cmath>
using namespace std;

int main() {
    int N;
    cin >> N;
    vector<int>A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }
    int Amax = *max_element(A.begin(), A.end());
    int Amin = *min_element(A.begin(), A.end());
    for (int i : A) {
        if (abs(i - Amax) > abs(i - Amin)) {
            cout << abs(i - Amax) << endl;
        }
        else {
            cout << abs(i - Amin) << endl;
        }
    }
     return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 4028
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

int main()
{
    int N;
    std::cin >> N;
    std::vector<int> age(N);

    int min = 1000000000, max = 0;
    for (int i = 0; i < N; i++) {
        std::cin >> age[i];
        if (min > age[i])
            min = age[i];
        if (max < age[i])
            max = age[i];
    }

    int diff1, diff2;
    for (int i = 0; i < N; i++) {
        diff1 = max - age[i];
        diff2 = age[i] - min;
        if (diff1 < diff2)
            std::cout << diff2 << std::endl;
        else
            std::cout << diff1 << std::endl;
    }

    return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 4096
final_rank: 8

code_snippet: #include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(){
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i,n) cin >> a[i];

  vector<int> b=a;
  sort(b.begin(),b.end());
  
  rep(i,n) {
    cout << max(b[n-1]-a[i],a[i]-b[0]) << endl;
  }
  return 0;

}
accuracy: 1
time_ms: 230
memory_kb: 4960
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
    int n, m = 1000000007, M =0;
    cin >> n;
    vector<int> a(n);
    for(int i=0;i<n;i++){
        cin >> a[i];
        M = max(M,a[i]);
        m = min(m,a[i]);
    }
    
    for(int i=0;i<n;i++){
        cout << max(M-a[i],a[i]-m) << endl;
    }
}
accuracy: 1
time_ms: 220
memory_kb: 4136
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
const int inf = 1<<30;
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
typedef long long ll;
//a:97, A:65
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
const double PI = acos(-1);
typedef string::const_iterator State;//構文解析用

int main(){
    int n; cin >> n;
    vector<int> a(n),s(n);
    for(int i=0;i<n;i++){
        cin >> a[i];
        s[i]=a[i];
    }

    sort(a.begin(),a.end());
    int mi=a[0],mi2=a[0],mx=a[n-1],mx2=a[n-1];
    for(int i=0;i<n;i++) if(a[i]!=mi2){
        mi2=a[i];
        break;
    }
    for(int i=n-1;i>=0;i--) if(a[i]!=mx2){
        mx2=a[i];
        break;
    }
    
    for(int i=0;i<n;i++) cout << max(max(abs(s[i]-mi),abs(s[i]-mi2)), max(abs(s[i]-mx),abs(s[i]-mx2))) << endl;
}
accuracy: 1
time_ms: 230
memory_kb: 4956
final_rank: 15

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;
#define REP(i,n) for(int i=0;i<int(n);i++)





int main(void){
	cin.tie(nullptr);  ios_base::sync_with_stdio(false);
	int i,j;


	int N;
	cin >> N;
	vector<P> a(N);
	REP(i,N){
		int x;
		cin >> x;
		a[i]=P(x,i);
	}


	sort(a.begin(),a.end());

	vector<int> b(N);
    REP(i,N){
		int d=max(a[i].first-a[0].first,a[N-1].first-a[i].first);
		b[a[i].second]=d;
	}


    REP(i,N) cout << b[i] << endl;




	return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 5988
final_rank: 18

code_snippet: #include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int max(int a,int b);
int max_num(int *age,int num);
int min_num(int *age,int num);

int main(void){
    int n;
    scanf("%d",&n);
    int *age;
    age = (int*)malloc(sizeof(int) * n);
    for(int i=0;i<n;i++){
        scanf("%d",&age[i]);
    }
    int age_max = max_num(age,n);
    int age_min = min_num(age,n);
    for(int i=0;i<n;i++){
        printf("%d\n",max(age_max-age[i],age[i]-age_min));
    }
    free(age);
}

int max_num(int *age,int num){
    int max = INT_MIN;
    for(int i=0;i<num;i++){
        if(max < age[i]){
            max = age[i];
        }
    }
    return max;
}
int min_num(int *age,int num){
    int min = INT_MAX;
    for(int i=0;i<num;i++){
        if(min > age[i]){
            min = age[i];
        }
    }
    return min;
}
int max(int a,int b){
    if(a > b){
        return a;
    }else{
        return b;
    }
}
accuracy: 1
time_ms: 30
memory_kb: 3408
final_rank: 1

code_snippet: #include <bits/stdc++.h>

int main()
{
    int N, min = 1e9, max = 0;
    std::cin >> N;
    std::vector<int> A(N);
    for (auto &a : A) {
        std::cin >> a;
        min = std::min(a, min);
        max = std::max(a, max);
    }
    for (const auto &a : A) {
        std::cout << std::max(a - min, max - a) << std::endl;
    }
    return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 4088
final_rank: 7

code_snippet: #include <bits/stdc++.h>

using namespace std;

signed main() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (auto &x : a) cin >> x;
    b = a;
    sort(b.begin(), b.end());
    for (int i = 0; i < n; i++) cout << max(a[i] - b[0], b[n - 1] - a[i]) << '\n';
}
accuracy: 1
time_ms: 70
memory_kb: 4884
final_rank: 5

code_snippet: #include <bits/stdc++.h>

using namespace std;

signed main() {
    int n;
    cin >> n;
    vector<pair<int, int>> a(n), b(n);
    for (int i = 0; i < n; i++) cin >> a[i].first, a[i].second = i;
    b = a;
    sort(b.begin(), b.end());
    for (int i = 0; i < n; i++) {
        if (a[i].second == b[0].second) {
            cout << abs(b[n - 1].first - a[i].first) << '\n';
        } else if (a[i].second == b[n - 1].second) {
            cout << abs(b[0].first - a[i].first) << '\n';
        } else {
            cout << max(abs(b[0].first - a[i].first), abs(b[n - 1].first - a[i].first)) << '\n';
        }
    }
}
accuracy: 1
time_ms: 80
memory_kb: 7036
final_rank: 14

code_snippet: #include <stdio.h>

int main (){
	int N;

	
	scanf("%d", &N);
		int age[N];
	
	for (int i = 0 ; i < N; i++){
		scanf("%d", &age[i]);
		
	}
	
	int max_age = age[0];
	int min_age = age[0];
	
	for ( int i = 0 ; i < N ;i++){
		if ( age[i]>max_age){
			max_age = age[i];
		}
		else if (age[i]<min_age){
			min_age = age[i];
		}
	}
	for ( int i = 0 ; i<N ; i++){
		int max_difference = (age[i]- min_age) > (max_age - age[i]) ? (age[i]- min_age) : (max_age - age[i]) ;
		printf("%d\n", max_difference);
	}
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3744
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
  #include "settings/debug.cpp"
#else
  #define Debug(...) void(0)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  auto b = a;
  sort(b.begin(), b.end());
  rep(i, n) {
    cout << max(b.back() - a[i], a[i] - b.front()) << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 5248
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,l,r) for(ll i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
int main(){
    int n;cin>>n;
    vector<long long>a(n);
    REP(i,n)cin>>a[i];
    auto b=a;
    sort(a.begin(),a.end());
    REP(i,n){
        cout<<max(a.back()-b[i],b[i]-a[0])<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 7036
final_rank: 19

code_snippet: #include <algorithm>
#include <iostream>
#include <vector>

int main(void)
{
    int n{};
    std::cin >> n;
    std::vector<int> a(n);
    for (int i{0}; i < n; ++i)
    {
        std::cin >> a[i];
    }

    auto [min_it, max_it] = std::minmax_element(a.begin(), a.end());

    for (int i{0}; i < n; ++i)
    {
        std::cout << std::max(a[i] - *min_it, *max_it - a[i]) << '\n';
    }

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 4124
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <algorithm> // max_element, min_element
using namespace std;

int main() {
    int N;
    cin >> N;
    
    vector<long long> A(N);
    
    // 年齢の入力を受け取る
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }
    
    // 年齢の最大値と最小値を計算する
    long long max_age = *max_element(A.begin(), A.end());
    long long min_age = *min_element(A.begin(), A.end());
    
    // 各住民の年齢差の最大値を出力
    for (int i = 0; i < N; i++) {
        // 自分自身を除くため、min_age もしくは max_age との差の最大値を取る
        long long max_diff = max(abs(A[i] - max_age), abs(A[i] - min_age));
        cout << max_diff << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 4908
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
    // 入力
    ll N;
    cin >> N;
    vector<ll> A(N);
    for (ll i = 0; i < N; i++) cin >> A[i];

    // 計算
    ll minA = A[0], maxA = A[0];
    for (ll i = 1; i < N; i++) {
        minA = min(minA, A[i]);
        maxA = max(maxA, A[i]);
    }

    // 出力
    for (ll i = 0; i < N; i++)
        cout << max(abs(A[i] - minA), abs(A[i] - maxA)) << endl;
   
    return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 4668
final_rank: 17

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using vc = vector<int>;
using vvc = vector<vector<int>>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const long long INF = 1LL << 60;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    int ans = 999999999;
    cin >> n;
    vc a(n);

    for (int i = 0; i < n; ++i) { cin >> a[i]; }

    int an = *max_element(a.begin(), a.end());
    int bn = *min_element(a.begin(), a.end());

    for (int i = 0; i < n; ++i)
    {
        ans = max(abs(a[i] - an), abs(a[i] - bn));
        cout << ans << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4128
final_rank: 3

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    int n; cin >> n;
    vector<int> a(n);
    int ma = 0, mi = 1e9;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        if(mi > a[i]){
            mi = a[i];
        }
        if(ma < a[i]){
            ma = a[i];
        }
    }
    for(int i = 0; i < n; i++){
        cout << max(ma - a[i], a[i] - mi) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 4140
final_rank: 10