# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
226325 | Ruxandra985 | Coin Collecting (JOI19_ho_t4) | C++14 | 5 ms | 384 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>
using namespace std;
pair <long long,long long> v[200010];
long long f[200010][3] , taken[100010][3];
long long modul (long long x){
return max(x , -x);
}
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
long long i , n , l1 , c1 , l2 , c2 , part , aux;
long long sol = 0;
fscanf (fin,"%lld",&n);
for (i = 1 ; i <= 2 * n ; i++){
fscanf (fin,"%lld%lld",&v[i].first,&v[i].second);
/// acum reduci v ul
if (v[i].second <= 1){
sol = sol + 1 - v[i].second;
v[i].second = 1;
}
else {
sol = sol + v[i].second - 2;
v[i].second = 2;
}
if (v[i].first < 1){
sol = sol + 1 - v[i].first;
v[i].first = 1;
}
else if (v[i].first > n){
sol = sol + v[i].first - n;
v[i].first = n;
}
f[v[i].first][v[i].second]++;
//printf ("%lld %lld\n", v[i].first , v[i].second);
}
/// renunt la heap gata ok am inteles, nu e bn:)))))
for (i = 1 ; i <= n ; i++){
if (f[i][1] > 0){
taken[i][1] = 1;
f[i][1]--;
}
if (f[i][2] > 0){
taken[i][2] = 1;
f[i][2]--;
}
}
c1 = c2 = 0;
l1 = l2 = 0;
aux = sol;
sol = 0;
for (i = 1 ; i <= n ; i++){
//if (i == 3)
// printf ("a");
if (!c1){
/// te duci in sus pana gasesti cv
l1++;
while (l1 <= n && !f[l1][1])
l1++;
if (l1 <= n)
c1 = f[l1][1];
}
if (!c2){
/// te duci in sus pana gasesti cv
l2++;
while (l2 <= n && !f[l2][2])
l2++;
if (l2 <= n)
c2 = f[l2][2];
}
if (!taken[i][1]){
if ( min(l2 , l1) <= i ){
if (l2 < l1){
c2--;
sol += modul(l2 - i) + 1;
}
else {
c1--;
sol += modul(l1 - i);
}
}
else if ((c1 && c2 && modul(l1 - i) <= modul(l2 - i) + 1) || !c2){
c1--;
sol += modul(l1 - i);
}
else {
c2--;
sol += modul(l2 - i) + 1;
}
}
if (!taken[i][2]){
if ( min(l2 , l1) <= i ){
if (l2 <= l1){
c2--;
sol += modul(l2 - i);
}
else {
c1--;
sol += modul(l1 - i) + 1;
}
}
else if ((c1 && c2 && modul(l2 - i) <= modul(l1 - i) + 1) || !c1){
c2--;
sol += modul(l2 - i);
}
else {
c1--;
sol += modul(l1 - i) + 1;
}
}
}
c1 = c2 = 0;
l1 = l2 = 0;
part = sol;
sol = 0;
for (i = 1 ; i <= n ; i++){
if (!c1){
/// te duci in sus pana gasesti cv
l1++;
while (l1 <= n && !f[l1][1])
l1++;
if (l1 <= n)
c1 = f[l1][1];
}
if (!c2){
/// te duci in sus pana gasesti cv
l2++;
while (l2 <= n && !f[l2][2])
l2++;
if (l2 <= n)
c2 = f[l2][2];
}
if (!taken[i][1]){
if ((c1 && c2 && modul(l1 - i) <= modul(l2 - i) + 1) || !c2){
c1--;
sol += modul(l1 - i);
}
else {
c2--;
sol += modul(l2 - i) + 1;
}
}
if (!taken[i][2]){
if ((c1 && c2 && modul(l2 - i) <= modul(l1 - i) + 1) || !c1){
c2--;
sol += modul(l2 - i);
}
else {
c1--;
sol += modul(l1 - i) + 1;
}
}
}
part = min(part , sol);
fprintf (fout,"%lld",aux + part);
return 0;
}
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... |