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 = 200005;
int n;
long long ans;
long long cnt[MAXN][2];
long long X[MAXN], Y[MAXN];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= 2 * n; i++)
{
cin >> X[i] >> Y[i];
if(X[i] <= 0)
{
ans += (1 - X[i]);
X[i] = 1;
}
if(X[i] > n)
{
ans += abs(X[i] - n);
X[i] = n;
}
if(X[i] >= 1 and X[i] <= n)
{
if(Y[i] <= 1)
{
ans += abs(Y[i] - 1);
Y[i] = 1;
}
else
{
ans += abs(Y[i] - 2);
Y[i] = 2;
}
cnt[X[i]][Y[i] - 1]++;
}
}
int cur = 1, id = 0, lst = -1;
for(int i = 1; i <= n; i++)
{
if(cnt[i][1] == 0 and cnt[i][0] == 0)
{
continue;
}
if(lst != -1)
{
ans += abs(lst - cur);
ans += abs(i - cur);
lst = -1;
if(cnt[i][id ^ 1])
{
cnt[i][id ^ 1]--;
}
else
{
cnt[i][id]--;
ans++;
}
cur++;
}
while(cnt[i][1] > 0 and cnt[i][0] > 0)
{
ans += abs(i - cur);
ans += abs(i - cur);
cnt[i][1]--;
cnt[i][0]--;
cur++;
}
while(cnt[i][0] > 1)
{
ans += abs(i - cur);
ans += abs(i - cur);
ans++;
cur++;
cnt[i][0] -= 2;
}
while(cnt[i][1] > 1)
{
ans += abs(i - cur);
ans += abs(i - cur);
ans++;
cur++;
cnt[i][1] -= 2;
}
if(cnt[i][0])
{
lst = i;
id = 0;
}
if(cnt[i][1])
{
lst = i;
id = 1;
}
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |