Submission #813674

# Submission time Handle Problem Language Result Execution time Memory
813674 2023-08-08T01:34:49 Z urd05 Line Town (CCO23_day1problem3) C++17
13 / 25
131 ms 14256 KB
#include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> P;
priority_queue<P> pq;
int arr[500001];
int n;
int tree[500005];
const long long INF=1e14;

int sum(int i) {
    int ret=0;
    while (i>0) {
        ret+=tree[i];
        i-=(i&-i);
    }
    return ret;
}

void upd(int i,int val) {
    while (i<=n) {
        tree[i]+=val;
        i+=(i&-i);
    }
}

int main(void) {
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d",&arr[i]);
        upd(i,1);
        pq.push(P(abs(arr[i]),i));
    }
    int sz=n;
    long long ret=0;
    while (!pq.empty()) {
        int now=pq.top().first;
        if (now==0) {
            break;
        }
        vector<int> vec;
        while (!pq.empty()&&pq.top().first==now) {
            vec.push_back(pq.top().second);
            pq.pop();
        }
        long long value=INF;
        sort(vec.begin(),vec.end());
        vector<int> vec0; //0에서 양수
        vector<int> vec1; //0에서 음수
        for(int i=0;i<vec.size();i++) {
            if ((vec[i]%2==1)^(arr[vec[i]]<0)) {
                vec0.push_back(vec[i]);
            }
            else {
                vec1.push_back(vec[i]);
            }
        }
        bool stone=true;
        bool enone=(sz%2==0);
        long long val=0;
        int k=vec0.size();
//printf("%d %d %d\n",now,vec0.size(),vec1.size());
        if (vec1.size()>vec0.size()+2) {
            printf("-1");
            return 0;
        }
        else if (vec1.size()==vec0.size()+2) {
            if (!enone) {
                printf("-1");
                return 0;
            }
            for(int i=0;i<k;i++) {
                val+=sum(vec1[i]-1);
                upd(vec1[i],-1);
                val+=sum(vec0[i]-1);
                upd(vec0[i],-1);
            }
            val+=sum(vec1[k]-1);
            upd(vec1[k],-1);
            val+=sum(n)-sum(vec1[k+1]);
            upd(vec1[k+1],-1);
            value=min(value,val);
            for(int i=k-1;i>=0;i--) {
                if (vec0[i]<vec1[i+1]) {
                    val++;
                }
                else {
                    val--;
                }
                val-=2*sum(vec0[i])-sum(n);
                val-=2*sum(vec1[i+1])-sum(n);
                value=min(value,val);
            }
        }
        else if (vec1.size()==vec0.size()+1) {
            if (enone) {
                for(int i=0;i<k;i++) {
                    val+=sum(vec1[i]-1);
                    upd(vec1[i],-1);
                    val+=sum(vec0[i]-1);
                    upd(vec0[i],-1);
                }
                val+=sum(vec1[k]-1);
                upd(vec1[k],-1);
                value=min(value,val);
                for(int i=k-1;i>=0;i--) {
                    val-=2*sum(vec1[i+1])-sum(n);
                    value=min(value,val);
                    val-=2*sum(vec0[i])-sum(n);
                    value=min(value,val);
                }
                val-=2*sum(vec1[0])-sum(n);
                value=min(value,val);
            }
            else {
                for(int i=0;i<k;i++) {
                    val+=sum(vec1[i]-1);
                    upd(vec1[i],-1);
                    val+=sum(vec0[i]-1);
                    upd(vec0[i],-1);
                }
                val+=sum(vec1[k]-1);
                upd(vec1[k],-1);
                value=min(value,val);
                for(int i=k-1;i>=0;i--) {
                    if (vec0[i]<vec1[i+1]) {
                        val++;
                    }
                    else {
                        val--;
                    }
                    val-=2*sum(vec0[i])-sum(n);
                    val-=2*sum(vec1[i+1])-sum(n);
                    value=min(value,val);
                }
            }
        }
        else if (vec1.size()==vec0.size()) {
            if (enone) {
                for(int i=0;i<k;i++) {
                    val+=sum(vec1[i]-1);
                    upd(vec1[i],-1);
                    val+=sum(vec0[i]-1);
                    upd(vec0[i],-1);
                }
                value=min(value,val);
                for(int i=k-1;i>=0;i--) {
                    if (vec0[i]>vec1[i]) {
                        val++;
                    }
                    else {
                        val--;
                    }
                    val-=2*sum(vec0[i])-sum(n);
                    val-=2*sum(vec1[i])-sum(n);
                    value=min(value,val);
                }
            }
            else {
                for(int i=0;i<k;i++) {
                    val+=sum(vec1[i]-1);
                    upd(vec1[i],-1);
                    val+=sum(vec0[i]-1);
                    upd(vec0[i],-1);
                }
                value=min(value,val);
                for(int i=k-1;i>=0;i--) {
                    val-=2*sum(vec0[i])-sum(n);
                    value=min(value,val);
                    val-=2*sum(vec1[i])-sum(n);
                    value=min(value,val);
                }
            }
        }
        else if (vec1.size()+1==vec0.size()) {
            if (enone) {
                printf("-1");
                return 0;
            }
            for(int i=0;i<k-1;i++) {
                val+=sum(vec1[i]-1);
                upd(vec1[i],-1);
                val+=sum(vec0[i]-1);
                upd(vec0[i],-1);
            }
            val+=sum(n)-sum(vec0[k-1]);
            upd(vec0[k-1],-1);
            value=min(value,val);
            for(int i=k-2;i>=0;i--) {
                if (vec0[i]>vec1[i]) {
                    val++;
                }
                else {
                    val--;
                }
                val-=2*sum(vec0[i])-sum(n);
                val-=2*sum(vec1[i])-sum(n);
                value=min(value,val);
            }
        }
        else {
            printf("-1");
            return 0;
        }
        ret+=value;
        sz-=vec.size();
    }
    printf("%lld",ret);
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:50:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         for(int i=0;i<vec.size();i++) {
      |                     ~^~~~~~~~~~~
Main.cpp:58:14: warning: unused variable 'stone' [-Wunused-variable]
   58 |         bool stone=true;
      |              ^~~~~
Main.cpp:28:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
Main.cpp:30:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |         scanf("%d",&arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 244 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 244 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 116 ms 14000 KB Output is correct
16 Correct 131 ms 14256 KB Output is correct
17 Correct 116 ms 13184 KB Output is correct
18 Correct 96 ms 13336 KB Output is correct
19 Correct 114 ms 14080 KB Output is correct
20 Correct 121 ms 13596 KB Output is correct
21 Correct 125 ms 14172 KB Output is correct
22 Correct 121 ms 14112 KB Output is correct
23 Correct 125 ms 13236 KB Output is correct
24 Correct 90 ms 14076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 244 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 244 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 116 ms 14000 KB Output is correct
16 Correct 131 ms 14256 KB Output is correct
17 Correct 116 ms 13184 KB Output is correct
18 Correct 96 ms 13336 KB Output is correct
19 Correct 114 ms 14080 KB Output is correct
20 Correct 121 ms 13596 KB Output is correct
21 Correct 125 ms 14172 KB Output is correct
22 Correct 121 ms 14112 KB Output is correct
23 Correct 125 ms 13236 KB Output is correct
24 Correct 90 ms 14076 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 88 ms 11152 KB Output is correct
45 Correct 88 ms 11092 KB Output is correct
46 Correct 87 ms 11568 KB Output is correct
47 Correct 83 ms 11432 KB Output is correct
48 Correct 102 ms 13864 KB Output is correct
49 Correct 119 ms 14216 KB Output is correct
50 Correct 84 ms 10808 KB Output is correct
51 Correct 82 ms 11632 KB Output is correct
52 Correct 82 ms 10688 KB Output is correct
53 Correct 66 ms 11676 KB Output is correct
54 Correct 92 ms 12300 KB Output is correct
55 Correct 96 ms 12176 KB Output is correct
56 Correct 98 ms 13204 KB Output is correct
57 Correct 73 ms 8432 KB Output is correct
58 Correct 53 ms 8520 KB Output is correct
59 Correct 53 ms 8924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 0 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 244 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Incorrect 0 ms 212 KB Output isn't correct
35 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 244 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 116 ms 14000 KB Output is correct
16 Correct 131 ms 14256 KB Output is correct
17 Correct 116 ms 13184 KB Output is correct
18 Correct 96 ms 13336 KB Output is correct
19 Correct 114 ms 14080 KB Output is correct
20 Correct 121 ms 13596 KB Output is correct
21 Correct 125 ms 14172 KB Output is correct
22 Correct 121 ms 14112 KB Output is correct
23 Correct 125 ms 13236 KB Output is correct
24 Correct 90 ms 14076 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 88 ms 11152 KB Output is correct
45 Correct 88 ms 11092 KB Output is correct
46 Correct 87 ms 11568 KB Output is correct
47 Correct 83 ms 11432 KB Output is correct
48 Correct 102 ms 13864 KB Output is correct
49 Correct 119 ms 14216 KB Output is correct
50 Correct 84 ms 10808 KB Output is correct
51 Correct 82 ms 11632 KB Output is correct
52 Correct 82 ms 10688 KB Output is correct
53 Correct 66 ms 11676 KB Output is correct
54 Correct 92 ms 12300 KB Output is correct
55 Correct 96 ms 12176 KB Output is correct
56 Correct 98 ms 13204 KB Output is correct
57 Correct 73 ms 8432 KB Output is correct
58 Correct 53 ms 8520 KB Output is correct
59 Correct 53 ms 8924 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Incorrect 0 ms 212 KB Output isn't correct
62 Halted 0 ms 0 KB -