Submission #1114369

# Submission time Handle Problem Language Result Execution time Memory
1114369 2024-11-18T17:03:03 Z VinhLuu Line Town (CCO23_day1problem3) C++17
25 / 25
531 ms 65324 KB
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define all(lpv) lpv.begin(), lpv.end()
#define fi first
#define se second
using namespace std;

typedef pair<int,int> pii;
const int N = 1e6 + 5;
const int oo = 1e16;
const int mod = 1e6 + 3;

int n, a[N], m, zero;

void add(int &x,int y){
  if(x == -1) x = y;
  else x = min(x, y);
}

int bit[N];

void update(int i,int val){
  for(; i <= n; i += i & -i) bit[i] += val;
}

int get(int i){
  int ret = 0;
  for(; i; i -= i & -i) ret += bit[i];
  return ret;
}

namespace AC{

  pair<int,int> save[2][N];

  int g[2][2];

  vector<int> vr[N];

  void solve(){
    int pre = 0, nx = 1;
    g[pre][0] = 0;
    g[pre][1] = -1;
    g[nx][0] = g[nx][1] = -1;
    int sum = 0;
    for(int i = 1; i <= n; i ++) if(a[i] != 0){
      vr[abs(a[i])].push_back(i);
    }

    int ans = -1;

    for(int val = m; val >= 1; val --){
//      cerr << val << " r\n";
//      for(auto i : vr[val]) cerr << i << " ";
//      cerr << " h\n";
      for(int t = 0; t <= 1; t ++) if(g[pre][t] != -1){
        int ble = t, bri = (sum - t) % 2;

//        cerr << val << " " << t << " " << g[pre][t] << " " << ble << " " << bri  << " start\n";

        deque<int> col[2], rev[2];

        for(auto i : vr[val]){
          if(i & 1){
            if(a[i] > 0) col[0].push_back(i);
            else col[1].push_back(i);
          }else{
            if(a[i] > 0) col[1].push_back(i);
            else col[0].push_back(i);
          }
          if((n - i + 1) & 1){
            if(a[i] > 0) rev[0].push_back(i);
            else rev[1].push_back(i);
          }else{
            if(a[i] > 0) rev[1].push_back(i);
            else rev[0].push_back(i);
          }
        }

        int le = 0, ri = 0;

        int tmp = (int)vr[val].size();

        int ret = 0;

        while(le < tmp){
          int nxt = ((ble + le + 1) & 1);
          if(col[nxt].empty()) break;
          le++;
          int x = col[nxt].front();
          ret += x - 1 - get(x - 1);
          update(x, 1);
          if((n - x + 1) & 1){
            if(a[x] > 0) rev[0].pop_front();
            else rev[1].pop_front();
          }else{
            if(a[x] > 0) rev[1].pop_front();
            else rev[0].pop_front();
          }
          save[0][le] = {nxt, x};
          col[nxt].pop_front();
        }

//        cerr << le << " " << ret << " h\n";

        while(ri < tmp - le){
          int nxt = (((bri + ri + 1) & 1) ^ 1);
          if(rev[nxt].empty()) break;
          ri++;
          int x = rev[nxt].back();
          ret += n - x - (get(n) - get(x));
          update(x, 1);
          save[1][ri] = {nxt, x};
          rev[nxt].pop_back();
        }

        while(le >= 0){
          if(le + ri == tmp) add(g[nx][(ble + le) % 2], ret + g[pre][t]);
          if(!le) break;
          int nxt, x; tie(nxt, x) = save[0][le];
          col[nxt].push_front(x);
          update(x, -1);
          ret -= x - 1 - get(x - 1);
          if((n - x + 1) & 1){
            if(a[x] > 0) rev[0].push_front(x);
            else rev[1].push_front(x);
          }else{
            if(a[x] > 0) rev[1].push_front(x);
            else rev[0].push_front(x);
          }
          le--;
          while(ri < tmp - le){
            int _right = (((bri + ri + 1) & 1) ^ 1);
            if(rev[_right].empty()) break;
            int pos = rev[_right].back();
            ri++;
            ret += (n - pos - (get(n) - get(pos)));
            update(pos, 1);
            save[1][ri] = {nxt, pos};
            rev[_right].pop_back();
          }
        }
        while(ri > 0){
          int nxt, x; tie(nxt, x) = save[1][ri];
          update(x, -1);
          ri--;
        }
        g[pre][t] = -1;
      }

      sum += (int)vr[val].size();
      for(auto i : vr[val]) update(i, 1);

      if(g[nx][0] == -1 && g[nx][1] == -1){
        cout << -1;
        return;
      }
      swap(pre, nx);
    }
    if(g[pre][0] != -1) add(ans, g[pre][0]);
    if(g[pre][1] != -1) add(ans, g[pre][1]);
    cout << ans;
  }
}

signed main(){
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #define task "v"
  if(fopen(task ".inp","r")){
    freopen(task ".inp","r",stdin);
    freopen(task ".out","w",stdout);
  }
  cin >> n;
  vector<int> rrh;
  for(int i = 1; i <= n; i ++){
    cin >> a[i];
    zero += (a[i] == 0);
    if(a[i] != 0) rrh.push_back(abs(a[i]));
  }
  sort(all(rrh)); rrh.resize(unique(all(rrh)) - rrh.begin());
  m = (int)rrh.size();
  for(int i = 1; i <= n; i ++){
    if(a[i] == 0) continue;
    if(a[i] < 0) a[i] = -(lower_bound(all(rrh), abs(a[i])) - rrh.begin() + 1);
    else a[i] = lower_bound(all(rrh), abs(a[i])) - rrh.begin() + 1;
  }
  AC :: solve();
}
/*
5
0 -1 -1 0 1
*/

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:171:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  171 |     freopen(task ".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:172:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  172 |     freopen(task ".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 29264 KB Output is correct
2 Correct 5 ms 23888 KB Output is correct
3 Correct 6 ms 23888 KB Output is correct
4 Correct 5 ms 24144 KB Output is correct
5 Correct 6 ms 24060 KB Output is correct
6 Correct 7 ms 24144 KB Output is correct
7 Correct 7 ms 24144 KB Output is correct
8 Correct 9 ms 24144 KB Output is correct
9 Correct 7 ms 24144 KB Output is correct
10 Correct 6 ms 25424 KB Output is correct
11 Correct 8 ms 24312 KB Output is correct
12 Correct 9 ms 24144 KB Output is correct
13 Correct 8 ms 24144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 29264 KB Output is correct
2 Correct 5 ms 23888 KB Output is correct
3 Correct 6 ms 23888 KB Output is correct
4 Correct 5 ms 24144 KB Output is correct
5 Correct 6 ms 24060 KB Output is correct
6 Correct 7 ms 24144 KB Output is correct
7 Correct 7 ms 24144 KB Output is correct
8 Correct 9 ms 24144 KB Output is correct
9 Correct 7 ms 24144 KB Output is correct
10 Correct 6 ms 25424 KB Output is correct
11 Correct 8 ms 24312 KB Output is correct
12 Correct 9 ms 24144 KB Output is correct
13 Correct 8 ms 24144 KB Output is correct
14 Correct 5 ms 25168 KB Output is correct
15 Correct 141 ms 64088 KB Output is correct
16 Correct 143 ms 64816 KB Output is correct
17 Correct 148 ms 64056 KB Output is correct
18 Correct 141 ms 64560 KB Output is correct
19 Correct 136 ms 64304 KB Output is correct
20 Correct 156 ms 64048 KB Output is correct
21 Correct 158 ms 65324 KB Output is correct
22 Correct 157 ms 65072 KB Output is correct
23 Correct 155 ms 64280 KB Output is correct
24 Correct 145 ms 64624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 29264 KB Output is correct
2 Correct 5 ms 23888 KB Output is correct
3 Correct 6 ms 23888 KB Output is correct
4 Correct 5 ms 24144 KB Output is correct
5 Correct 6 ms 24060 KB Output is correct
6 Correct 7 ms 24144 KB Output is correct
7 Correct 7 ms 24144 KB Output is correct
8 Correct 9 ms 24144 KB Output is correct
9 Correct 7 ms 24144 KB Output is correct
10 Correct 6 ms 25424 KB Output is correct
11 Correct 8 ms 24312 KB Output is correct
12 Correct 9 ms 24144 KB Output is correct
13 Correct 8 ms 24144 KB Output is correct
14 Correct 8 ms 24060 KB Output is correct
15 Correct 4 ms 23888 KB Output is correct
16 Correct 6 ms 23920 KB Output is correct
17 Correct 8 ms 23888 KB Output is correct
18 Correct 9 ms 23888 KB Output is correct
19 Correct 10 ms 23940 KB Output is correct
20 Correct 9 ms 23888 KB Output is correct
21 Correct 11 ms 23888 KB Output is correct
22 Correct 10 ms 24144 KB Output is correct
23 Correct 9 ms 23888 KB Output is correct
24 Correct 9 ms 23888 KB Output is correct
25 Correct 9 ms 23920 KB Output is correct
26 Correct 11 ms 23888 KB Output is correct
27 Correct 9 ms 23888 KB Output is correct
28 Correct 10 ms 23936 KB Output is correct
29 Correct 10 ms 23888 KB Output is correct
30 Correct 10 ms 23888 KB Output is correct
31 Correct 10 ms 23888 KB Output is correct
32 Correct 13 ms 24108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 29264 KB Output is correct
2 Correct 5 ms 23888 KB Output is correct
3 Correct 6 ms 23888 KB Output is correct
4 Correct 5 ms 24144 KB Output is correct
5 Correct 6 ms 24060 KB Output is correct
6 Correct 7 ms 24144 KB Output is correct
7 Correct 7 ms 24144 KB Output is correct
8 Correct 9 ms 24144 KB Output is correct
9 Correct 7 ms 24144 KB Output is correct
10 Correct 6 ms 25424 KB Output is correct
11 Correct 8 ms 24312 KB Output is correct
12 Correct 9 ms 24144 KB Output is correct
13 Correct 8 ms 24144 KB Output is correct
14 Correct 5 ms 25168 KB Output is correct
15 Correct 141 ms 64088 KB Output is correct
16 Correct 143 ms 64816 KB Output is correct
17 Correct 148 ms 64056 KB Output is correct
18 Correct 141 ms 64560 KB Output is correct
19 Correct 136 ms 64304 KB Output is correct
20 Correct 156 ms 64048 KB Output is correct
21 Correct 158 ms 65324 KB Output is correct
22 Correct 157 ms 65072 KB Output is correct
23 Correct 155 ms 64280 KB Output is correct
24 Correct 145 ms 64624 KB Output is correct
25 Correct 8 ms 24060 KB Output is correct
26 Correct 4 ms 23888 KB Output is correct
27 Correct 6 ms 23920 KB Output is correct
28 Correct 8 ms 23888 KB Output is correct
29 Correct 9 ms 23888 KB Output is correct
30 Correct 10 ms 23940 KB Output is correct
31 Correct 9 ms 23888 KB Output is correct
32 Correct 11 ms 23888 KB Output is correct
33 Correct 10 ms 24144 KB Output is correct
34 Correct 9 ms 23888 KB Output is correct
35 Correct 9 ms 23888 KB Output is correct
36 Correct 9 ms 23920 KB Output is correct
37 Correct 11 ms 23888 KB Output is correct
38 Correct 9 ms 23888 KB Output is correct
39 Correct 10 ms 23936 KB Output is correct
40 Correct 10 ms 23888 KB Output is correct
41 Correct 10 ms 23888 KB Output is correct
42 Correct 10 ms 23888 KB Output is correct
43 Correct 13 ms 24108 KB Output is correct
44 Correct 112 ms 48456 KB Output is correct
45 Correct 91 ms 47668 KB Output is correct
46 Correct 93 ms 48704 KB Output is correct
47 Correct 92 ms 48600 KB Output is correct
48 Correct 109 ms 52900 KB Output is correct
49 Correct 160 ms 64084 KB Output is correct
50 Correct 88 ms 46528 KB Output is correct
51 Correct 89 ms 46332 KB Output is correct
52 Correct 89 ms 46428 KB Output is correct
53 Correct 88 ms 46708 KB Output is correct
54 Correct 114 ms 52784 KB Output is correct
55 Correct 108 ms 52784 KB Output is correct
56 Correct 119 ms 52280 KB Output is correct
57 Correct 29 ms 28496 KB Output is correct
58 Correct 29 ms 28532 KB Output is correct
59 Correct 33 ms 28752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23888 KB Output is correct
2 Correct 12 ms 23888 KB Output is correct
3 Correct 12 ms 24192 KB Output is correct
4 Correct 10 ms 23888 KB Output is correct
5 Correct 14 ms 24052 KB Output is correct
6 Correct 11 ms 23888 KB Output is correct
7 Correct 9 ms 23960 KB Output is correct
8 Correct 11 ms 24056 KB Output is correct
9 Correct 12 ms 23780 KB Output is correct
10 Correct 9 ms 23888 KB Output is correct
11 Correct 8 ms 23888 KB Output is correct
12 Correct 11 ms 24092 KB Output is correct
13 Correct 12 ms 23888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23888 KB Output is correct
2 Correct 12 ms 23888 KB Output is correct
3 Correct 12 ms 24192 KB Output is correct
4 Correct 10 ms 23888 KB Output is correct
5 Correct 14 ms 24052 KB Output is correct
6 Correct 11 ms 23888 KB Output is correct
7 Correct 9 ms 23960 KB Output is correct
8 Correct 11 ms 24056 KB Output is correct
9 Correct 12 ms 23780 KB Output is correct
10 Correct 9 ms 23888 KB Output is correct
11 Correct 8 ms 23888 KB Output is correct
12 Correct 11 ms 24092 KB Output is correct
13 Correct 12 ms 23888 KB Output is correct
14 Correct 114 ms 52412 KB Output is correct
15 Correct 530 ms 56244 KB Output is correct
16 Correct 479 ms 54860 KB Output is correct
17 Correct 471 ms 54672 KB Output is correct
18 Correct 490 ms 54676 KB Output is correct
19 Correct 491 ms 55368 KB Output is correct
20 Correct 6 ms 23888 KB Output is correct
21 Correct 10 ms 23892 KB Output is correct
22 Correct 303 ms 56328 KB Output is correct
23 Correct 297 ms 56316 KB Output is correct
24 Correct 215 ms 54428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 29264 KB Output is correct
2 Correct 5 ms 23888 KB Output is correct
3 Correct 6 ms 23888 KB Output is correct
4 Correct 5 ms 24144 KB Output is correct
5 Correct 6 ms 24060 KB Output is correct
6 Correct 7 ms 24144 KB Output is correct
7 Correct 7 ms 24144 KB Output is correct
8 Correct 9 ms 24144 KB Output is correct
9 Correct 7 ms 24144 KB Output is correct
10 Correct 6 ms 25424 KB Output is correct
11 Correct 8 ms 24312 KB Output is correct
12 Correct 9 ms 24144 KB Output is correct
13 Correct 8 ms 24144 KB Output is correct
14 Correct 8 ms 24060 KB Output is correct
15 Correct 4 ms 23888 KB Output is correct
16 Correct 6 ms 23920 KB Output is correct
17 Correct 8 ms 23888 KB Output is correct
18 Correct 9 ms 23888 KB Output is correct
19 Correct 10 ms 23940 KB Output is correct
20 Correct 9 ms 23888 KB Output is correct
21 Correct 11 ms 23888 KB Output is correct
22 Correct 10 ms 24144 KB Output is correct
23 Correct 9 ms 23888 KB Output is correct
24 Correct 9 ms 23888 KB Output is correct
25 Correct 9 ms 23920 KB Output is correct
26 Correct 11 ms 23888 KB Output is correct
27 Correct 9 ms 23888 KB Output is correct
28 Correct 10 ms 23936 KB Output is correct
29 Correct 10 ms 23888 KB Output is correct
30 Correct 10 ms 23888 KB Output is correct
31 Correct 10 ms 23888 KB Output is correct
32 Correct 13 ms 24108 KB Output is correct
33 Correct 10 ms 23888 KB Output is correct
34 Correct 12 ms 23888 KB Output is correct
35 Correct 12 ms 24192 KB Output is correct
36 Correct 10 ms 23888 KB Output is correct
37 Correct 14 ms 24052 KB Output is correct
38 Correct 11 ms 23888 KB Output is correct
39 Correct 9 ms 23960 KB Output is correct
40 Correct 11 ms 24056 KB Output is correct
41 Correct 12 ms 23780 KB Output is correct
42 Correct 9 ms 23888 KB Output is correct
43 Correct 8 ms 23888 KB Output is correct
44 Correct 11 ms 24092 KB Output is correct
45 Correct 12 ms 23888 KB Output is correct
46 Correct 8 ms 23888 KB Output is correct
47 Correct 9 ms 24172 KB Output is correct
48 Correct 9 ms 23924 KB Output is correct
49 Correct 9 ms 23956 KB Output is correct
50 Correct 7 ms 23888 KB Output is correct
51 Correct 8 ms 23888 KB Output is correct
52 Correct 7 ms 24056 KB Output is correct
53 Correct 7 ms 23888 KB Output is correct
54 Correct 8 ms 23888 KB Output is correct
55 Correct 9 ms 23888 KB Output is correct
56 Correct 7 ms 23888 KB Output is correct
57 Correct 9 ms 23888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 29264 KB Output is correct
2 Correct 5 ms 23888 KB Output is correct
3 Correct 6 ms 23888 KB Output is correct
4 Correct 5 ms 24144 KB Output is correct
5 Correct 6 ms 24060 KB Output is correct
6 Correct 7 ms 24144 KB Output is correct
7 Correct 7 ms 24144 KB Output is correct
8 Correct 9 ms 24144 KB Output is correct
9 Correct 7 ms 24144 KB Output is correct
10 Correct 6 ms 25424 KB Output is correct
11 Correct 8 ms 24312 KB Output is correct
12 Correct 9 ms 24144 KB Output is correct
13 Correct 8 ms 24144 KB Output is correct
14 Correct 5 ms 25168 KB Output is correct
15 Correct 141 ms 64088 KB Output is correct
16 Correct 143 ms 64816 KB Output is correct
17 Correct 148 ms 64056 KB Output is correct
18 Correct 141 ms 64560 KB Output is correct
19 Correct 136 ms 64304 KB Output is correct
20 Correct 156 ms 64048 KB Output is correct
21 Correct 158 ms 65324 KB Output is correct
22 Correct 157 ms 65072 KB Output is correct
23 Correct 155 ms 64280 KB Output is correct
24 Correct 145 ms 64624 KB Output is correct
25 Correct 8 ms 24060 KB Output is correct
26 Correct 4 ms 23888 KB Output is correct
27 Correct 6 ms 23920 KB Output is correct
28 Correct 8 ms 23888 KB Output is correct
29 Correct 9 ms 23888 KB Output is correct
30 Correct 10 ms 23940 KB Output is correct
31 Correct 9 ms 23888 KB Output is correct
32 Correct 11 ms 23888 KB Output is correct
33 Correct 10 ms 24144 KB Output is correct
34 Correct 9 ms 23888 KB Output is correct
35 Correct 9 ms 23888 KB Output is correct
36 Correct 9 ms 23920 KB Output is correct
37 Correct 11 ms 23888 KB Output is correct
38 Correct 9 ms 23888 KB Output is correct
39 Correct 10 ms 23936 KB Output is correct
40 Correct 10 ms 23888 KB Output is correct
41 Correct 10 ms 23888 KB Output is correct
42 Correct 10 ms 23888 KB Output is correct
43 Correct 13 ms 24108 KB Output is correct
44 Correct 112 ms 48456 KB Output is correct
45 Correct 91 ms 47668 KB Output is correct
46 Correct 93 ms 48704 KB Output is correct
47 Correct 92 ms 48600 KB Output is correct
48 Correct 109 ms 52900 KB Output is correct
49 Correct 160 ms 64084 KB Output is correct
50 Correct 88 ms 46528 KB Output is correct
51 Correct 89 ms 46332 KB Output is correct
52 Correct 89 ms 46428 KB Output is correct
53 Correct 88 ms 46708 KB Output is correct
54 Correct 114 ms 52784 KB Output is correct
55 Correct 108 ms 52784 KB Output is correct
56 Correct 119 ms 52280 KB Output is correct
57 Correct 29 ms 28496 KB Output is correct
58 Correct 29 ms 28532 KB Output is correct
59 Correct 33 ms 28752 KB Output is correct
60 Correct 10 ms 23888 KB Output is correct
61 Correct 12 ms 23888 KB Output is correct
62 Correct 12 ms 24192 KB Output is correct
63 Correct 10 ms 23888 KB Output is correct
64 Correct 14 ms 24052 KB Output is correct
65 Correct 11 ms 23888 KB Output is correct
66 Correct 9 ms 23960 KB Output is correct
67 Correct 11 ms 24056 KB Output is correct
68 Correct 12 ms 23780 KB Output is correct
69 Correct 9 ms 23888 KB Output is correct
70 Correct 8 ms 23888 KB Output is correct
71 Correct 11 ms 24092 KB Output is correct
72 Correct 12 ms 23888 KB Output is correct
73 Correct 114 ms 52412 KB Output is correct
74 Correct 530 ms 56244 KB Output is correct
75 Correct 479 ms 54860 KB Output is correct
76 Correct 471 ms 54672 KB Output is correct
77 Correct 490 ms 54676 KB Output is correct
78 Correct 491 ms 55368 KB Output is correct
79 Correct 6 ms 23888 KB Output is correct
80 Correct 10 ms 23892 KB Output is correct
81 Correct 303 ms 56328 KB Output is correct
82 Correct 297 ms 56316 KB Output is correct
83 Correct 215 ms 54428 KB Output is correct
84 Correct 8 ms 23888 KB Output is correct
85 Correct 9 ms 24172 KB Output is correct
86 Correct 9 ms 23924 KB Output is correct
87 Correct 9 ms 23956 KB Output is correct
88 Correct 7 ms 23888 KB Output is correct
89 Correct 8 ms 23888 KB Output is correct
90 Correct 7 ms 24056 KB Output is correct
91 Correct 7 ms 23888 KB Output is correct
92 Correct 8 ms 23888 KB Output is correct
93 Correct 9 ms 23888 KB Output is correct
94 Correct 7 ms 23888 KB Output is correct
95 Correct 9 ms 23888 KB Output is correct
96 Correct 135 ms 46260 KB Output is correct
97 Correct 130 ms 46088 KB Output is correct
98 Correct 108 ms 46004 KB Output is correct
99 Correct 261 ms 54712 KB Output is correct
100 Correct 112 ms 55596 KB Output is correct
101 Correct 233 ms 52404 KB Output is correct
102 Correct 531 ms 56216 KB Output is correct
103 Correct 472 ms 49844 KB Output is correct
104 Correct 267 ms 43712 KB Output is correct
105 Correct 235 ms 43196 KB Output is correct
106 Correct 218 ms 43188 KB Output is correct