Submission #813655

# Submission time Handle Problem Language Result Execution time Memory
813655 2023-08-08T01:21:06 Z urd05 Line Town (CCO23_day1problem3) C++17
13 / 25
149 ms 14220 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 ((sum(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 212 KB Output is correct
2 Correct 1 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 364 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 340 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 212 KB Output is correct
2 Correct 1 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 364 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 340 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 121 ms 14008 KB Output is correct
16 Correct 133 ms 14096 KB Output is correct
17 Correct 145 ms 13176 KB Output is correct
18 Correct 101 ms 13324 KB Output is correct
19 Correct 122 ms 14040 KB Output is correct
20 Correct 128 ms 13488 KB Output is correct
21 Correct 130 ms 14100 KB Output is correct
22 Correct 135 ms 14000 KB Output is correct
23 Correct 149 ms 13296 KB Output is correct
24 Correct 98 ms 14156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 364 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 340 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 0 ms 212 KB Output is correct
16 Correct 1 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 288 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 212 KB Output is correct
2 Correct 1 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 364 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 340 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 121 ms 14008 KB Output is correct
16 Correct 133 ms 14096 KB Output is correct
17 Correct 145 ms 13176 KB Output is correct
18 Correct 101 ms 13324 KB Output is correct
19 Correct 122 ms 14040 KB Output is correct
20 Correct 128 ms 13488 KB Output is correct
21 Correct 130 ms 14100 KB Output is correct
22 Correct 135 ms 14000 KB Output is correct
23 Correct 149 ms 13296 KB Output is correct
24 Correct 98 ms 14156 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 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 288 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 104 ms 11052 KB Output is correct
45 Correct 93 ms 10988 KB Output is correct
46 Correct 94 ms 11512 KB Output is correct
47 Correct 85 ms 11400 KB Output is correct
48 Correct 109 ms 13744 KB Output is correct
49 Correct 135 ms 14220 KB Output is correct
50 Correct 99 ms 10780 KB Output is correct
51 Correct 84 ms 11564 KB Output is correct
52 Correct 85 ms 10676 KB Output is correct
53 Correct 70 ms 11612 KB Output is correct
54 Correct 122 ms 12296 KB Output is correct
55 Correct 111 ms 12148 KB Output is correct
56 Correct 98 ms 13100 KB Output is correct
57 Correct 75 ms 8404 KB Output is correct
58 Correct 59 ms 8496 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 1 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 1 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 Correct 1 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 364 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 340 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 0 ms 212 KB Output is correct
16 Correct 1 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 288 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 1 ms 212 KB Output isn't correct
35 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 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 364 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 340 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 121 ms 14008 KB Output is correct
16 Correct 133 ms 14096 KB Output is correct
17 Correct 145 ms 13176 KB Output is correct
18 Correct 101 ms 13324 KB Output is correct
19 Correct 122 ms 14040 KB Output is correct
20 Correct 128 ms 13488 KB Output is correct
21 Correct 130 ms 14100 KB Output is correct
22 Correct 135 ms 14000 KB Output is correct
23 Correct 149 ms 13296 KB Output is correct
24 Correct 98 ms 14156 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 0 ms 212 KB Output is correct
27 Correct 1 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 288 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 104 ms 11052 KB Output is correct
45 Correct 93 ms 10988 KB Output is correct
46 Correct 94 ms 11512 KB Output is correct
47 Correct 85 ms 11400 KB Output is correct
48 Correct 109 ms 13744 KB Output is correct
49 Correct 135 ms 14220 KB Output is correct
50 Correct 99 ms 10780 KB Output is correct
51 Correct 84 ms 11564 KB Output is correct
52 Correct 85 ms 10676 KB Output is correct
53 Correct 70 ms 11612 KB Output is correct
54 Correct 122 ms 12296 KB Output is correct
55 Correct 111 ms 12148 KB Output is correct
56 Correct 98 ms 13100 KB Output is correct
57 Correct 75 ms 8404 KB Output is correct
58 Correct 59 ms 8496 KB Output is correct
59 Correct 53 ms 8924 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Incorrect 1 ms 212 KB Output isn't correct
62 Halted 0 ms 0 KB -