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];
vector<ll> num;
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;
num.pb(a[i].ph);
num.pb(a[i].po);
}
}
ll Cal1(ll c1, ll 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);
}
ll res = INF;
for (int i = lower_bound(num.begin(), num.end(), p + 1) - num.begin(); i < num.size(); i++)
{
res = min(res, Cal1(p, num[i]));
}
return res;
}
void Solve()
{
sort(num.begin(), num.end());
num.erase(unique(num.begin(), num.end()), num.end());
//cout << Cal(4) << '\n';
if (k == 1)
{
int l = 0, r = 1e9;
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);
} else
{
ll res = INF;
for (int i = 0; i < num.size(); i++)
{
for (int j = i + 1; j < num.size(); j++)
{
res = min(res, Cal1(num[i], num[j]));
}
}
cout << res;
}
}
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)
bridge.cpp: In function 'long long int Cal(int)':
bridge.cpp:55:78: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i = lower_bound(num.begin(), num.end(), p + 1) - num.begin(); i < num.size(); i++)
| ~~^~~~~~~~~~~~
bridge.cpp: In function 'void Solve()':
bridge.cpp:87:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int i = 0; i < num.size(); i++)
| ~~^~~~~~~~~~~~
bridge.cpp:89:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int j = i + 1; j < num.size(); j++)
| ~~^~~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(task".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |