답안 #1114227

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1114227 2024-11-18T11:57:04 Z duckindog Line Town (CCO23_day1problem3) C++17
20 / 25
185 ms 30016 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define int long long
 
const int N = 500'000 + 10,
          MAX = 1'000'000'000'000'000;
int n;
int h[N];

namespace sub4 { 
  struct BIT { 
    int bit[N];
    inline void upd(int i, int x) { 
      for (; i <= n; i += i & -i) bit[i] += x;
    }
    inline int que(int i) { 
      int ret = 0;
      for (; i; i -= i & -i) ret += bit[i];
      return ret;
    }
  } T, Z;
  
  int pref[N];
  
  vector<int> position;
  void solve() { 
    int cntZero = 0, totalZero = 0;
    for (int i = 1; i <= n; ++i) {
      if (h[i]) continue;
      position.push_back(i);
      cntZero += 1;
      totalZero += i - 1 - Z.que(i - 1);
      Z.upd(i, 1);
    }
    const int sz = n - cntZero;
    
    int answer = MAX;
    for (int type = 1; type <= 2; ++type) { 
      memset(pref, 0, sizeof pref);
      memset(T.bit, 0, sizeof T.bit);
  
      deque<int> odd, even;
      for (int i = 1; i <= n; ++i) { 
        if (!h[i]) continue;
        auto& vt = (i ^ (h[i] == 1)) & 1 ? odd : even;
        vt.push_back(i);
      }
  
      vector<int> sequence;
      for (int i = 1; i < sz; ++i) { 
        auto& ret = pref[i];
        ret = pref[i - 1];
  
        auto& vt = i & 1 ? odd : even;
        if (!vt.size()) { ret = MAX; continue; }
        int pos = vt.front();
        vt.pop_front();
  
        ret += pos - 1 - T.que(pos - 1); 
        ret -= position.end() - upper_bound(position.begin(), position.end(), pos);
        T.upd(pos, 1);
        sequence.push_back(pos);
      }
      
      int total = totalZero;
      if (type == 1) { //calculate total
        auto& vt = (n ^ 1) & 1 ? odd : even;
        if (!vt.size()) total = MAX;
        else { 
          int pos = vt.front();
          vt.pop_front();
          total += pos - 1 - T.que(pos - 1) - Z.que(pos - 1);
          sequence.push_back(pos);
        }
      } else if (sz) { 
        auto& ret = pref[sz];
        ret = pref[sz - 1];
  
        auto& vt = sz & 1 ? odd : even;
        if (!vt.size()) ret = MAX;
        else { 
          int pos = vt.front();
          vt.pop_front();
  
          ret += pos - 1 - T.que(pos - 1); 
          ret -= position.end() - upper_bound(position.begin(), position.end(), pos);
          T.upd(pos, 1);
          sequence.push_back(pos);
        }
      }
      
      if ((int)sequence.size() == sz) {       
        if (type == 1) answer = min(answer, pref[sz - 1] + total);
        else answer = min(answer, pref[sz] + total);
  
        for (int i = (type == 1 ? sz - 2 : sz - 1); i >= 1; i -= 2) { 
          if (!(cntZero & 1)) swap(sequence[i - 1], sequence[i]);
  
          T.upd(sequence[i - 1], -1);
          T.upd(sequence[i], -1);
  
          total += sequence[i - 1] - 1 - T.que(sequence[i - 1] - 1) - Z.que(sequence[i - 1] - 1);
          T.upd(sequence[i - 1], 1);
          
          total += sequence[i] - 1 - T.que(sequence[i] - 1) - Z.que(sequence[i] - 1);
          T.upd(sequence[i], 1);
  
          T.upd(sequence[i - 1], -1);
          T.upd(sequence[i], -1);
          
          answer = min(answer, pref[i - 1] + total);
        }
      }
    }
  
    cout << (answer == MAX ? -1 : answer) << "\n";
  }
}

namespace sub6 { 
  int bit[N];
  void upd(int i, int x) { 
    for (; i <= n; i += i & -i) bit[i] += x;
  } 
  int que(int i) { 
    int ret = 0;
    for (; i; i -= i & -i) ret += bit[i];
    return ret;
  }

  long long f[N][2];
  void solve() { 
    vector<int> vt(n); iota(vt.begin(), vt.end(), 1);
    sort(vt.begin(), vt.end(), [&](const auto& a, const auto& b) { 
      return abs(h[a]) > abs(h[b]);
    });
    for (int i = 1; i <= n; ++i) upd(i, 1);

    memset(f, 14, sizeof f);
    f[0][0] = 0;
    for (int i = 1; i <= n; ++i) { 
      int pos = vt[i - 1];
      upd(pos, -1);

      for (int type = 0; type <= 1; ++type) { 
        auto& ret = f[i][type];

        if ((type ^ pos ^ (h[pos] < 0)) & 1 || !h[pos]) { // go left
          ret = min(ret, f[i - 1][type ^ 1] + que(pos));
        }

        if (((n - i + 1) ^ type ^ pos ^ (h[pos] > 0)) & 1 || !h[pos]) { // go right
          ret = min(ret, f[i - 1][type] + que(n) - que(pos));
        }
      }
    }

    long long answer = min(f[n][0], f[n][1]);
    cout << (answer >= MAX ? -1 : answer) << "\n";
  }
}

int32_t main() { 
  cin.tie(0)->sync_with_stdio(0);
 
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> h[i];

  if (all_of(h + 1, h + n + 1, [](const auto& a) { return abs(a) <= 1; })) { 
    sub4::solve();
    return 0;
  };
  
  sub6::solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12624 KB Output is correct
2 Correct 3 ms 12624 KB Output is correct
3 Correct 2 ms 12624 KB Output is correct
4 Correct 3 ms 12624 KB Output is correct
5 Correct 3 ms 12624 KB Output is correct
6 Correct 2 ms 12624 KB Output is correct
7 Correct 3 ms 12624 KB Output is correct
8 Correct 2 ms 12624 KB Output is correct
9 Correct 2 ms 12624 KB Output is correct
10 Correct 2 ms 12624 KB Output is correct
11 Correct 2 ms 12624 KB Output is correct
12 Correct 3 ms 12624 KB Output is correct
13 Correct 2 ms 12624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12624 KB Output is correct
2 Correct 3 ms 12624 KB Output is correct
3 Correct 2 ms 12624 KB Output is correct
4 Correct 3 ms 12624 KB Output is correct
5 Correct 3 ms 12624 KB Output is correct
6 Correct 2 ms 12624 KB Output is correct
7 Correct 3 ms 12624 KB Output is correct
8 Correct 2 ms 12624 KB Output is correct
9 Correct 2 ms 12624 KB Output is correct
10 Correct 2 ms 12624 KB Output is correct
11 Correct 2 ms 12624 KB Output is correct
12 Correct 3 ms 12624 KB Output is correct
13 Correct 2 ms 12624 KB Output is correct
14 Correct 3 ms 12624 KB Output is correct
15 Correct 104 ms 27352 KB Output is correct
16 Correct 114 ms 27568 KB Output is correct
17 Correct 107 ms 27572 KB Output is correct
18 Correct 78 ms 27324 KB Output is correct
19 Correct 115 ms 27324 KB Output is correct
20 Correct 158 ms 27320 KB Output is correct
21 Correct 155 ms 27320 KB Output is correct
22 Correct 159 ms 27444 KB Output is correct
23 Correct 161 ms 27328 KB Output is correct
24 Correct 94 ms 27332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12624 KB Output is correct
2 Correct 3 ms 12624 KB Output is correct
3 Correct 2 ms 12624 KB Output is correct
4 Correct 3 ms 12624 KB Output is correct
5 Correct 3 ms 12624 KB Output is correct
6 Correct 2 ms 12624 KB Output is correct
7 Correct 3 ms 12624 KB Output is correct
8 Correct 2 ms 12624 KB Output is correct
9 Correct 2 ms 12624 KB Output is correct
10 Correct 2 ms 12624 KB Output is correct
11 Correct 2 ms 12624 KB Output is correct
12 Correct 3 ms 12624 KB Output is correct
13 Correct 2 ms 12624 KB Output is correct
14 Correct 2 ms 12624 KB Output is correct
15 Correct 3 ms 11600 KB Output is correct
16 Correct 2 ms 12624 KB Output is correct
17 Correct 3 ms 12624 KB Output is correct
18 Correct 3 ms 11852 KB Output is correct
19 Correct 4 ms 11856 KB Output is correct
20 Correct 3 ms 11896 KB Output is correct
21 Correct 4 ms 11856 KB Output is correct
22 Correct 3 ms 12792 KB Output is correct
23 Correct 3 ms 11856 KB Output is correct
24 Correct 4 ms 11856 KB Output is correct
25 Correct 3 ms 11856 KB Output is correct
26 Correct 4 ms 12024 KB Output is correct
27 Correct 3 ms 11856 KB Output is correct
28 Correct 3 ms 11856 KB Output is correct
29 Correct 4 ms 11856 KB Output is correct
30 Correct 3 ms 12624 KB Output is correct
31 Correct 4 ms 12624 KB Output is correct
32 Correct 3 ms 12624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12624 KB Output is correct
2 Correct 3 ms 12624 KB Output is correct
3 Correct 2 ms 12624 KB Output is correct
4 Correct 3 ms 12624 KB Output is correct
5 Correct 3 ms 12624 KB Output is correct
6 Correct 2 ms 12624 KB Output is correct
7 Correct 3 ms 12624 KB Output is correct
8 Correct 2 ms 12624 KB Output is correct
9 Correct 2 ms 12624 KB Output is correct
10 Correct 2 ms 12624 KB Output is correct
11 Correct 2 ms 12624 KB Output is correct
12 Correct 3 ms 12624 KB Output is correct
13 Correct 2 ms 12624 KB Output is correct
14 Correct 3 ms 12624 KB Output is correct
15 Correct 104 ms 27352 KB Output is correct
16 Correct 114 ms 27568 KB Output is correct
17 Correct 107 ms 27572 KB Output is correct
18 Correct 78 ms 27324 KB Output is correct
19 Correct 115 ms 27324 KB Output is correct
20 Correct 158 ms 27320 KB Output is correct
21 Correct 155 ms 27320 KB Output is correct
22 Correct 159 ms 27444 KB Output is correct
23 Correct 161 ms 27328 KB Output is correct
24 Correct 94 ms 27332 KB Output is correct
25 Correct 2 ms 12624 KB Output is correct
26 Correct 3 ms 11600 KB Output is correct
27 Correct 2 ms 12624 KB Output is correct
28 Correct 3 ms 12624 KB Output is correct
29 Correct 3 ms 11852 KB Output is correct
30 Correct 4 ms 11856 KB Output is correct
31 Correct 3 ms 11896 KB Output is correct
32 Correct 4 ms 11856 KB Output is correct
33 Correct 3 ms 12792 KB Output is correct
34 Correct 3 ms 11856 KB Output is correct
35 Correct 4 ms 11856 KB Output is correct
36 Correct 3 ms 11856 KB Output is correct
37 Correct 4 ms 12024 KB Output is correct
38 Correct 3 ms 11856 KB Output is correct
39 Correct 3 ms 11856 KB Output is correct
40 Correct 4 ms 11856 KB Output is correct
41 Correct 3 ms 12624 KB Output is correct
42 Correct 4 ms 12624 KB Output is correct
43 Correct 3 ms 12624 KB Output is correct
44 Correct 121 ms 25508 KB Output is correct
45 Correct 100 ms 25564 KB Output is correct
46 Correct 96 ms 25536 KB Output is correct
47 Correct 86 ms 25544 KB Output is correct
48 Correct 138 ms 30016 KB Output is correct
49 Correct 185 ms 27320 KB Output is correct
50 Correct 86 ms 24520 KB Output is correct
51 Correct 116 ms 24532 KB Output is correct
52 Correct 121 ms 24440 KB Output is correct
53 Correct 67 ms 24632 KB Output is correct
54 Correct 147 ms 29236 KB Output is correct
55 Correct 107 ms 28992 KB Output is correct
56 Correct 161 ms 28468 KB Output is correct
57 Correct 38 ms 21412 KB Output is correct
58 Correct 36 ms 21436 KB Output is correct
59 Correct 37 ms 21404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13048 KB Output is correct
2 Correct 2 ms 12880 KB Output is correct
3 Correct 3 ms 12880 KB Output is correct
4 Correct 3 ms 12880 KB Output is correct
5 Correct 3 ms 12880 KB Output is correct
6 Correct 2 ms 12880 KB Output is correct
7 Correct 3 ms 12880 KB Output is correct
8 Correct 3 ms 12880 KB Output is correct
9 Correct 3 ms 12624 KB Output is correct
10 Correct 3 ms 12932 KB Output is correct
11 Correct 3 ms 12880 KB Output is correct
12 Correct 2 ms 12880 KB Output is correct
13 Correct 2 ms 12880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13048 KB Output is correct
2 Correct 2 ms 12880 KB Output is correct
3 Correct 3 ms 12880 KB Output is correct
4 Correct 3 ms 12880 KB Output is correct
5 Correct 3 ms 12880 KB Output is correct
6 Correct 2 ms 12880 KB Output is correct
7 Correct 3 ms 12880 KB Output is correct
8 Correct 3 ms 12880 KB Output is correct
9 Correct 3 ms 12624 KB Output is correct
10 Correct 3 ms 12932 KB Output is correct
11 Correct 3 ms 12880 KB Output is correct
12 Correct 2 ms 12880 KB Output is correct
13 Correct 2 ms 12880 KB Output is correct
14 Correct 78 ms 21464 KB Output is correct
15 Correct 132 ms 26440 KB Output is correct
16 Correct 124 ms 24892 KB Output is correct
17 Correct 133 ms 25160 KB Output is correct
18 Correct 132 ms 24904 KB Output is correct
19 Correct 129 ms 25548 KB Output is correct
20 Correct 2 ms 13048 KB Output is correct
21 Correct 2 ms 12892 KB Output is correct
22 Correct 133 ms 26440 KB Output is correct
23 Correct 121 ms 26608 KB Output is correct
24 Correct 74 ms 24640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12624 KB Output is correct
2 Correct 3 ms 12624 KB Output is correct
3 Correct 2 ms 12624 KB Output is correct
4 Correct 3 ms 12624 KB Output is correct
5 Correct 3 ms 12624 KB Output is correct
6 Correct 2 ms 12624 KB Output is correct
7 Correct 3 ms 12624 KB Output is correct
8 Correct 2 ms 12624 KB Output is correct
9 Correct 2 ms 12624 KB Output is correct
10 Correct 2 ms 12624 KB Output is correct
11 Correct 2 ms 12624 KB Output is correct
12 Correct 3 ms 12624 KB Output is correct
13 Correct 2 ms 12624 KB Output is correct
14 Correct 2 ms 12624 KB Output is correct
15 Correct 3 ms 11600 KB Output is correct
16 Correct 2 ms 12624 KB Output is correct
17 Correct 3 ms 12624 KB Output is correct
18 Correct 3 ms 11852 KB Output is correct
19 Correct 4 ms 11856 KB Output is correct
20 Correct 3 ms 11896 KB Output is correct
21 Correct 4 ms 11856 KB Output is correct
22 Correct 3 ms 12792 KB Output is correct
23 Correct 3 ms 11856 KB Output is correct
24 Correct 4 ms 11856 KB Output is correct
25 Correct 3 ms 11856 KB Output is correct
26 Correct 4 ms 12024 KB Output is correct
27 Correct 3 ms 11856 KB Output is correct
28 Correct 3 ms 11856 KB Output is correct
29 Correct 4 ms 11856 KB Output is correct
30 Correct 3 ms 12624 KB Output is correct
31 Correct 4 ms 12624 KB Output is correct
32 Correct 3 ms 12624 KB Output is correct
33 Correct 3 ms 13048 KB Output is correct
34 Correct 2 ms 12880 KB Output is correct
35 Correct 3 ms 12880 KB Output is correct
36 Correct 3 ms 12880 KB Output is correct
37 Correct 3 ms 12880 KB Output is correct
38 Correct 2 ms 12880 KB Output is correct
39 Correct 3 ms 12880 KB Output is correct
40 Correct 3 ms 12880 KB Output is correct
41 Correct 3 ms 12624 KB Output is correct
42 Correct 3 ms 12932 KB Output is correct
43 Correct 3 ms 12880 KB Output is correct
44 Correct 2 ms 12880 KB Output is correct
45 Correct 2 ms 12880 KB Output is correct
46 Correct 3 ms 12880 KB Output is correct
47 Correct 2 ms 12880 KB Output is correct
48 Incorrect 3 ms 12880 KB Output isn't correct
49 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 12624 KB Output is correct
2 Correct 3 ms 12624 KB Output is correct
3 Correct 2 ms 12624 KB Output is correct
4 Correct 3 ms 12624 KB Output is correct
5 Correct 3 ms 12624 KB Output is correct
6 Correct 2 ms 12624 KB Output is correct
7 Correct 3 ms 12624 KB Output is correct
8 Correct 2 ms 12624 KB Output is correct
9 Correct 2 ms 12624 KB Output is correct
10 Correct 2 ms 12624 KB Output is correct
11 Correct 2 ms 12624 KB Output is correct
12 Correct 3 ms 12624 KB Output is correct
13 Correct 2 ms 12624 KB Output is correct
14 Correct 3 ms 12624 KB Output is correct
15 Correct 104 ms 27352 KB Output is correct
16 Correct 114 ms 27568 KB Output is correct
17 Correct 107 ms 27572 KB Output is correct
18 Correct 78 ms 27324 KB Output is correct
19 Correct 115 ms 27324 KB Output is correct
20 Correct 158 ms 27320 KB Output is correct
21 Correct 155 ms 27320 KB Output is correct
22 Correct 159 ms 27444 KB Output is correct
23 Correct 161 ms 27328 KB Output is correct
24 Correct 94 ms 27332 KB Output is correct
25 Correct 2 ms 12624 KB Output is correct
26 Correct 3 ms 11600 KB Output is correct
27 Correct 2 ms 12624 KB Output is correct
28 Correct 3 ms 12624 KB Output is correct
29 Correct 3 ms 11852 KB Output is correct
30 Correct 4 ms 11856 KB Output is correct
31 Correct 3 ms 11896 KB Output is correct
32 Correct 4 ms 11856 KB Output is correct
33 Correct 3 ms 12792 KB Output is correct
34 Correct 3 ms 11856 KB Output is correct
35 Correct 4 ms 11856 KB Output is correct
36 Correct 3 ms 11856 KB Output is correct
37 Correct 4 ms 12024 KB Output is correct
38 Correct 3 ms 11856 KB Output is correct
39 Correct 3 ms 11856 KB Output is correct
40 Correct 4 ms 11856 KB Output is correct
41 Correct 3 ms 12624 KB Output is correct
42 Correct 4 ms 12624 KB Output is correct
43 Correct 3 ms 12624 KB Output is correct
44 Correct 121 ms 25508 KB Output is correct
45 Correct 100 ms 25564 KB Output is correct
46 Correct 96 ms 25536 KB Output is correct
47 Correct 86 ms 25544 KB Output is correct
48 Correct 138 ms 30016 KB Output is correct
49 Correct 185 ms 27320 KB Output is correct
50 Correct 86 ms 24520 KB Output is correct
51 Correct 116 ms 24532 KB Output is correct
52 Correct 121 ms 24440 KB Output is correct
53 Correct 67 ms 24632 KB Output is correct
54 Correct 147 ms 29236 KB Output is correct
55 Correct 107 ms 28992 KB Output is correct
56 Correct 161 ms 28468 KB Output is correct
57 Correct 38 ms 21412 KB Output is correct
58 Correct 36 ms 21436 KB Output is correct
59 Correct 37 ms 21404 KB Output is correct
60 Correct 3 ms 13048 KB Output is correct
61 Correct 2 ms 12880 KB Output is correct
62 Correct 3 ms 12880 KB Output is correct
63 Correct 3 ms 12880 KB Output is correct
64 Correct 3 ms 12880 KB Output is correct
65 Correct 2 ms 12880 KB Output is correct
66 Correct 3 ms 12880 KB Output is correct
67 Correct 3 ms 12880 KB Output is correct
68 Correct 3 ms 12624 KB Output is correct
69 Correct 3 ms 12932 KB Output is correct
70 Correct 3 ms 12880 KB Output is correct
71 Correct 2 ms 12880 KB Output is correct
72 Correct 2 ms 12880 KB Output is correct
73 Correct 78 ms 21464 KB Output is correct
74 Correct 132 ms 26440 KB Output is correct
75 Correct 124 ms 24892 KB Output is correct
76 Correct 133 ms 25160 KB Output is correct
77 Correct 132 ms 24904 KB Output is correct
78 Correct 129 ms 25548 KB Output is correct
79 Correct 2 ms 13048 KB Output is correct
80 Correct 2 ms 12892 KB Output is correct
81 Correct 133 ms 26440 KB Output is correct
82 Correct 121 ms 26608 KB Output is correct
83 Correct 74 ms 24640 KB Output is correct
84 Correct 3 ms 12880 KB Output is correct
85 Correct 2 ms 12880 KB Output is correct
86 Incorrect 3 ms 12880 KB Output isn't correct
87 Halted 0 ms 0 KB -