# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
585468 | messiuuuuu | Palembang Bridges (APIO15_bridge) | C++17 | 47 ms | 2672 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 task "A"
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 1e5 + 5;
const ll INF = 1e18 + 5;
int n, k;
struct TRoad
{
char h, o;
ll ph, po;
}a[MAXN];
void Input()
{
cin >> k >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i].h >> a[i].ph >> a[i].o >> a[i].po;
}
}
ll Cal1(int c1, int c2)
{
ll res = 0;
for (int i = 1; i <= n; i++)
{
if (a[i].h == a[i].o)
{
res += abs(a[i].ph - a[i].po);
} else
{
res += min(abs(a[i].ph - c1) + abs(a[i].po - c1) + 1, abs(a[i].ph - c2) + abs(a[i].po - c2) + 1);
}
}
return res;
}
ll Cal(int p)
{
if (k == 1)
{
return Cal1(p, -2e9);
}
int l = p + 1, r = 1e9;
while (l <= r)
{
int mid = (l + r) / 2;
if (mid == 1e9)
break;
if (Cal1(p, mid) > Cal1(p, mid + 1))
{
l = mid + 1;
} else
r = mid - 1;
}
//cout << l << '\n';
return Cal1(p, l);
}
void Solve()
{
int l = 0, r = 1e9;
//cout << Cal(4) << '\n';
while (l <= r)
{
int mid = (l + r) / 2;
if (mid == 1e9)
break;
if (Cal(mid) >= Cal(mid + 1))
l = mid + 1;
else
r = mid - 1;
}
cout << Cal(l);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(task".INP","r"))
{
freopen(task".INP","r",stdin);
//freopen(task".OUT","w",stdout);
}
Input();
Solve();
}
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... |