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>
using namespace std;
typedef long long ll;
ll k, n;
ll s, t, numLeft, numRight, distFrom0;
char p, q;
vector<pair<ll, ll> > events;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> k >> n;
ll minSum = 0;
numLeft = numRight = distFrom0 = 0;
for (int i = 0; i < n; i++)
{
cin >> p >> s >> q >> t;
if (p == q)
{
minSum += abs(t - s);
}
else
{
minSum += 1 + abs(t - s);
ll b = max(t, s);
ll a = min(t, s);
events.push_back({a, -1});
events.push_back({b, 1});
numRight++;
distFrom0 += a;
}
}
//cout << "Min sum is " << minSum << "\n";
//cout << "Initial dist: " << distFrom0 << "\n";
sort(events.begin(), events.end());
ll prevLocation = 0;
ll finAns = 1000000000000000;
if (events.size() == 0)
{
finAns = minSum;
}
for (int i = 0; i < events.size(); i++)
{
ll currLocation = events[i].first;
distFrom0 -= numRight * (currLocation - prevLocation);
distFrom0 += numLeft * (currLocation - prevLocation);
if (events[i].second == -1)
{
numRight--;
}
else
{
numLeft++;
}
finAns = min(finAns, minSum + 2 * distFrom0);
//cout << "At location " << currLocation << " dist is " << distFrom0 << '\n';
prevLocation = currLocation;
}
cout << finAns << "\n";
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:45:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int i = 0; i < events.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... |