# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
660886 | danikoynov | Palembang Bridges (APIO15_bridge) | C++14 | 2072 ms | 3628 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 1e5 + 10;
struct work
{
ll x, y;
} w[maxn];
int k, n;
void solve()
{
cin >> k >> n;
ll ans = 0;
vector < ll > v;
for (int i = 1; i <= n; i ++)
{
char c1, c2;
cin >> c1 >> w[i].x >> c2 >> w[i].y;
if (w[i].x > w[i].y)
swap(w[i].x, w[i].y);
if (c1 == c2)
{
ans = ans + w[i].y - w[i].x;
i --;
n --;
}
else
{
v.push_back(w[i].y);
v.push_back(w[i].x);
ans ++;
}
}
//for (int i = 1; i <= n; i ++)
// cout << w[i].x << " - " << w[i].y << endl;
if (k == 1)
{
sort(v.begin(), v.end());
for (int i = 0; i < v.size() / 2; i ++)
ans = ans - (v[i]);
for (int i = v.size() / 2; i < v.size(); i ++)
ans = ans + v[i];
cout << ans << endl;
}
else
{
ll best = 1e18;
if (v.empty())
best = 0;
for (int i = 0; i < v.size(); i ++)
for (int j = i + 1; j < v.size(); j ++)
{
ll cur = 0;
for (int d = 1; d <= n; d ++)
{
cur = cur + min(abs(w[d].x - v[i]) + abs(w[d].y - v[i]),
abs(w[d].x - v[j]) + abs(w[d].y - v[j]));
}
best = min(best, cur);
}
cout << ans + best << endl;
}
}
int main()
{
solve();
return 0;
}
/**
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
Compilation message (stderr)
# | 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... |