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;
const int MAXN=100000;
long long k, n, pradSuma=0, suma=0;
char a[MAXN], b[MAXN];
long long x[MAXN], y[MAXN];
vector<tuple<long long, long long>> keliai;
long long dPtr=0;
long long dKairej=0, dDesinej;
vector<pair<long long, long long>> pertiltinesPoros;
long long skaiciuok(long long pos)
{
long long val=0;
for (auto i:pertiltinesPoros)
{
if (i.first<=pos && pos<=i.second)
continue;
val+=2ll*min(abs(i.first-pos), abs(i.second-pos));
}
return val;
}
void tinkintDesine(long long id1, long long id2)
{
while (dKairej<dDesinej)
{
long long pradPos=get<0>(keliai[dPtr]);
dPtr++;
suma-=2ll*(get<0>(keliai[dPtr])-pradPos)*(dDesinej-dKairej);
if (get<1>(keliai[dPtr])==0)
dDesinej--;
else
dKairej++;
}
}
int main()
{
cin>>k>>n;
for (int i=0; i<n; i++)
{
cin>>a[i]>>x[i]>>b[i]>>y[i];
if (x[i]>y[i])
swap(x[i], y[i]);
pradSuma+=y[i]-x[i];
if (a[i]!=b[i])
{
pradSuma++;
keliai.push_back(make_tuple(x[i], 0));
keliai.push_back(make_tuple(y[i], 1));
pertiltinesPoros.push_back({x[i], y[i]});
}
}
keliai.push_back(make_tuple(0, 0));
sort(keliai.begin(), keliai.end());
for (int i=0; i<n; i++)
if (a[i]!=b[i])
suma+=2ll*x[i];
dDesinej=keliai.size()/2;
tinkintDesine(-1, -1);
if (k==1)
{
cout<<pradSuma+suma<<endl;
return 0;
}
long long ats=LONG_MAX;
for (auto i:pertiltinesPoros)
{
ats=min(ats, pradSuma+skaiciuok(i.first));
ats=min(ats, pradSuma+skaiciuok(i.second));
}
cout<<ats;
}
# | 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... |