https://atcoder.jp/contests/typical90/tasks/typical90_n
014 - We Used to Sing a Song Together(★3)
AtCoder is a programming contest site for anyone from beginners to experts. We hold weekly programming contests online.
atcoder.jp
초등학생과 초등학교의 위치가 N개 주어질때 한 학교당 한 학생만 다니게 만들고 그 거리의차이를 불만도라고 할때 불만도를 최소화 해라. 정렬을해서 하나씩 맞춰주는 그리디한 방식이 최적임을 보장할 수 있기 때문에 이를 구현하면된다.
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_set>
#include <numeric>
#include <random>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define range(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
const int INF = 200000000;
const double pi = 3.14159265358979;
const ll MOD = 987654321;
void yesno(bool a)
{
if (a)
cout << "Yes\n";
else
cout << "No\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll a, b;
ll sum = 0;
ll N;
vll va, vb;
cin >> N;
rep(i, N)
{
ll a;
cin >> a;
va.push_back(a);
}
rep(i, N)
{
ll a;
cin >> a;
vb.push_back(a);
}
sort(va.begin(), va.end());
sort(vb.begin(), vb.end());
rep(i, N)
{
sum += abs(va[i] - vb[i]);
}
cout << sum;
return 0;
}
'알고리즘 > atcoder90제' 카테고리의 다른 글
앳코더90 - 016 - Minimum Coins(★3) (0) | 2024.11.07 |
---|---|
앳코더90 - 015 - Don't be too close(★6) (0) | 2024.11.07 |
앳코더90 - 013 - Passing(★5) (0) | 2024.03.19 |
앳코더90 - 011 - Gravy Jobs(★6) (0) | 2024.03.17 |
앳코더90 - 012 - Red Painting(★4) (0) | 2024.03.13 |