이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const ll inf = 1e18;
vector<pair<int, int> > eve;
ll solve(){
int n = eve.size();
vector<ll> a(2 * n + 1);
for (int i = 0; i < n; i++){
a[2 * i + 0 + 1] = eve[i].first;
a[2 * i + 1 + 1] = eve[i].second;
}
sort(a.begin() + 1, a.end());
n = 2 * n;
for (int i = 1; i <= n; i++)
a[i] += a[i - 1];
ll answer = inf;
for (int i = 1; i <= n; i++){
ll T = a[i] - a[i - 1];
answer = min(answer, T * (i - (n - i)) - a[i] + (a[n] - a[i]));
}
return answer + eve.size();
}
int main(){
ios_base::sync_with_stdio(false);
int k, n;
cin >> k >> n;
ll answer = 0;
for (int i = 0; i < n; i++){
int x, y;
char a, b;
cin >> a >> x >> b >> y;
if (a == b){
answer += abs(x - y);
continue;
}
if (a == 'B')
swap(x, y);
eve.push_back({x, y});
}
if (eve.empty())
return cout << answer << endl, 0;
if (k == 1){
cout << answer + solve() << '\n';
return 0;
}
vector<int> places;
for (int i = 0; i < eve.size(); i++){
places.push_back(eve[i].first);
places.push_back(eve[i].second);
}
sort(places.begin(), places.end());
if (n <= 100){
int m = places.size();
ll tot = inf;
for (int i = 0; i < m; i++){
for (int j = i + 1; j < m; j++){
ll sum = 0;
for (auto k : eve){
ll tmp = min(abs(k.first - places[i]) + abs(k.second - places[i]),
abs(k.first - places[j]) + abs(k.second - places[j]));
sum += tmp;
}
tot = min(tot, sum + answer + m / 2);
}
}
return cout << tot << endl, 0;
}
}
컴파일 시 표준 에러 (stderr) 메시지
bridge.cpp: In function 'int main()':
bridge.cpp:54:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < eve.size(); i++){
~~^~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |