답안 #1114343

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1114343 2024-11-18T15:55:43 Z VinhLuu Line Town (CCO23_day1problem3) C++17
13 / 25
140 ms 40840 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 Nlog{

  tuple<int,int,int> save[N];

  void solve(){
    int ans = -1;

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

    for(int i = 1; i <= n; i ++){
      if(a[i] == 0){ continue; }
      int x = abs(a[i]);
      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 = n - zero;

    int ret = 0;

//    for(auto j : rev[0]) cerr << j << " ";
//    cerr << "\n";

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

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

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

//    cerr << ri << " r\n";
//    cerr << " satrt\n";

    while(le >= 0){
      if(le + ri == tmp){
        add(ans, ret);
      }
      if(!le) break;
      int nxt, x, val; tie(nxt, x, val) = save[le];
      col[nxt].push_front(x);
      update(x, -1);
//      cerr << x << " rescue\n";
      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--;
//      cerr << le << " " << ret << " r\n";
      while(ri < tmp - le){
        int _right = (((ri + 1) & 1) ^ 1);
        if(rev[_right].empty()) break;
        int pos = rev[_right].back();
//        cerr << _right << " " << pos << " e\n";
        ri++;
        ret += (n - pos - (get(n) - get(pos)));
        update(pos, 1);
        rev[_right].pop_back();
      }
    }


    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;
  }
  Nlog :: solve();
}
/*
5
0 -1 -1 0 1
*/

Compilation message

Main.cpp: In function 'void Nlog::solve()':
Main.cpp:44:11: warning: unused variable 'x' [-Wunused-variable]
   44 |       int x = abs(a[i]);
      |           ^
Main.cpp: In function 'int main()':
Main.cpp:158:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  158 |     freopen(task ".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:159:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |     freopen(task ".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 6480 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 2 ms 6480 KB Output is correct
6 Correct 2 ms 4600 KB Output is correct
7 Correct 2 ms 6596 KB Output is correct
8 Correct 2 ms 6480 KB Output is correct
9 Correct 2 ms 6480 KB Output is correct
10 Correct 1 ms 4544 KB Output is correct
11 Correct 2 ms 4432 KB Output is correct
12 Correct 1 ms 4432 KB Output is correct
13 Correct 2 ms 4600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 6480 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 2 ms 6480 KB Output is correct
6 Correct 2 ms 4600 KB Output is correct
7 Correct 2 ms 6596 KB Output is correct
8 Correct 2 ms 6480 KB Output is correct
9 Correct 2 ms 6480 KB Output is correct
10 Correct 1 ms 4544 KB Output is correct
11 Correct 2 ms 4432 KB Output is correct
12 Correct 1 ms 4432 KB Output is correct
13 Correct 2 ms 4600 KB Output is correct
14 Correct 1 ms 4432 KB Output is correct
15 Correct 106 ms 40124 KB Output is correct
16 Correct 118 ms 39996 KB Output is correct
17 Correct 126 ms 39864 KB Output is correct
18 Correct 116 ms 40684 KB Output is correct
19 Correct 113 ms 40124 KB Output is correct
20 Correct 129 ms 40756 KB Output is correct
21 Correct 115 ms 39864 KB Output is correct
22 Correct 120 ms 39960 KB Output is correct
23 Correct 140 ms 40840 KB Output is correct
24 Correct 122 ms 39864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 6480 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 2 ms 6480 KB Output is correct
6 Correct 2 ms 4600 KB Output is correct
7 Correct 2 ms 6596 KB Output is correct
8 Correct 2 ms 6480 KB Output is correct
9 Correct 2 ms 6480 KB Output is correct
10 Correct 1 ms 4544 KB Output is correct
11 Correct 2 ms 4432 KB Output is correct
12 Correct 1 ms 4432 KB Output is correct
13 Correct 2 ms 4600 KB Output is correct
14 Correct 1 ms 4432 KB Output is correct
15 Correct 2 ms 6480 KB Output is correct
16 Correct 1 ms 4432 KB Output is correct
17 Correct 1 ms 4432 KB Output is correct
18 Correct 1 ms 4432 KB Output is correct
19 Correct 2 ms 6480 KB Output is correct
20 Correct 1 ms 6480 KB Output is correct
21 Correct 2 ms 6480 KB Output is correct
22 Correct 2 ms 6480 KB Output is correct
23 Correct 1 ms 4432 KB Output is correct
24 Correct 2 ms 6480 KB Output is correct
25 Correct 1 ms 6480 KB Output is correct
26 Correct 1 ms 6480 KB Output is correct
27 Correct 2 ms 4432 KB Output is correct
28 Correct 1 ms 4432 KB Output is correct
29 Correct 2 ms 4600 KB Output is correct
30 Correct 1 ms 4432 KB Output is correct
31 Correct 2 ms 4432 KB Output is correct
32 Correct 1 ms 4432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 6480 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 2 ms 6480 KB Output is correct
6 Correct 2 ms 4600 KB Output is correct
7 Correct 2 ms 6596 KB Output is correct
8 Correct 2 ms 6480 KB Output is correct
9 Correct 2 ms 6480 KB Output is correct
10 Correct 1 ms 4544 KB Output is correct
11 Correct 2 ms 4432 KB Output is correct
12 Correct 1 ms 4432 KB Output is correct
13 Correct 2 ms 4600 KB Output is correct
14 Correct 1 ms 4432 KB Output is correct
15 Correct 106 ms 40124 KB Output is correct
16 Correct 118 ms 39996 KB Output is correct
17 Correct 126 ms 39864 KB Output is correct
18 Correct 116 ms 40684 KB Output is correct
19 Correct 113 ms 40124 KB Output is correct
20 Correct 129 ms 40756 KB Output is correct
21 Correct 115 ms 39864 KB Output is correct
22 Correct 120 ms 39960 KB Output is correct
23 Correct 140 ms 40840 KB Output is correct
24 Correct 122 ms 39864 KB Output is correct
25 Correct 1 ms 4432 KB Output is correct
26 Correct 2 ms 6480 KB Output is correct
27 Correct 1 ms 4432 KB Output is correct
28 Correct 1 ms 4432 KB Output is correct
29 Correct 1 ms 4432 KB Output is correct
30 Correct 2 ms 6480 KB Output is correct
31 Correct 1 ms 6480 KB Output is correct
32 Correct 2 ms 6480 KB Output is correct
33 Correct 2 ms 6480 KB Output is correct
34 Correct 1 ms 4432 KB Output is correct
35 Correct 2 ms 6480 KB Output is correct
36 Correct 1 ms 6480 KB Output is correct
37 Correct 1 ms 6480 KB Output is correct
38 Correct 2 ms 4432 KB Output is correct
39 Correct 1 ms 4432 KB Output is correct
40 Correct 2 ms 4600 KB Output is correct
41 Correct 1 ms 4432 KB Output is correct
42 Correct 2 ms 4432 KB Output is correct
43 Correct 1 ms 4432 KB Output is correct
44 Correct 74 ms 27584 KB Output is correct
45 Correct 76 ms 27428 KB Output is correct
46 Correct 78 ms 27624 KB Output is correct
47 Correct 82 ms 28216 KB Output is correct
48 Correct 87 ms 33720 KB Output is correct
49 Correct 127 ms 40632 KB Output is correct
50 Correct 72 ms 26560 KB Output is correct
51 Correct 83 ms 28412 KB Output is correct
52 Correct 76 ms 28176 KB Output is correct
53 Correct 78 ms 28168 KB Output is correct
54 Correct 87 ms 32444 KB Output is correct
55 Correct 91 ms 33724 KB Output is correct
56 Correct 95 ms 30908 KB Output is correct
57 Correct 17 ms 9552 KB Output is correct
58 Correct 18 ms 9548 KB Output is correct
59 Correct 18 ms 9532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Incorrect 2 ms 6588 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Incorrect 2 ms 6588 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 6480 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 2 ms 6480 KB Output is correct
6 Correct 2 ms 4600 KB Output is correct
7 Correct 2 ms 6596 KB Output is correct
8 Correct 2 ms 6480 KB Output is correct
9 Correct 2 ms 6480 KB Output is correct
10 Correct 1 ms 4544 KB Output is correct
11 Correct 2 ms 4432 KB Output is correct
12 Correct 1 ms 4432 KB Output is correct
13 Correct 2 ms 4600 KB Output is correct
14 Correct 1 ms 4432 KB Output is correct
15 Correct 2 ms 6480 KB Output is correct
16 Correct 1 ms 4432 KB Output is correct
17 Correct 1 ms 4432 KB Output is correct
18 Correct 1 ms 4432 KB Output is correct
19 Correct 2 ms 6480 KB Output is correct
20 Correct 1 ms 6480 KB Output is correct
21 Correct 2 ms 6480 KB Output is correct
22 Correct 2 ms 6480 KB Output is correct
23 Correct 1 ms 4432 KB Output is correct
24 Correct 2 ms 6480 KB Output is correct
25 Correct 1 ms 6480 KB Output is correct
26 Correct 1 ms 6480 KB Output is correct
27 Correct 2 ms 4432 KB Output is correct
28 Correct 1 ms 4432 KB Output is correct
29 Correct 2 ms 4600 KB Output is correct
30 Correct 1 ms 4432 KB Output is correct
31 Correct 2 ms 4432 KB Output is correct
32 Correct 1 ms 4432 KB Output is correct
33 Correct 1 ms 4432 KB Output is correct
34 Incorrect 2 ms 6588 KB Output isn't correct
35 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4432 KB Output is correct
2 Correct 1 ms 4432 KB Output is correct
3 Correct 1 ms 6480 KB Output is correct
4 Correct 1 ms 4432 KB Output is correct
5 Correct 2 ms 6480 KB Output is correct
6 Correct 2 ms 4600 KB Output is correct
7 Correct 2 ms 6596 KB Output is correct
8 Correct 2 ms 6480 KB Output is correct
9 Correct 2 ms 6480 KB Output is correct
10 Correct 1 ms 4544 KB Output is correct
11 Correct 2 ms 4432 KB Output is correct
12 Correct 1 ms 4432 KB Output is correct
13 Correct 2 ms 4600 KB Output is correct
14 Correct 1 ms 4432 KB Output is correct
15 Correct 106 ms 40124 KB Output is correct
16 Correct 118 ms 39996 KB Output is correct
17 Correct 126 ms 39864 KB Output is correct
18 Correct 116 ms 40684 KB Output is correct
19 Correct 113 ms 40124 KB Output is correct
20 Correct 129 ms 40756 KB Output is correct
21 Correct 115 ms 39864 KB Output is correct
22 Correct 120 ms 39960 KB Output is correct
23 Correct 140 ms 40840 KB Output is correct
24 Correct 122 ms 39864 KB Output is correct
25 Correct 1 ms 4432 KB Output is correct
26 Correct 2 ms 6480 KB Output is correct
27 Correct 1 ms 4432 KB Output is correct
28 Correct 1 ms 4432 KB Output is correct
29 Correct 1 ms 4432 KB Output is correct
30 Correct 2 ms 6480 KB Output is correct
31 Correct 1 ms 6480 KB Output is correct
32 Correct 2 ms 6480 KB Output is correct
33 Correct 2 ms 6480 KB Output is correct
34 Correct 1 ms 4432 KB Output is correct
35 Correct 2 ms 6480 KB Output is correct
36 Correct 1 ms 6480 KB Output is correct
37 Correct 1 ms 6480 KB Output is correct
38 Correct 2 ms 4432 KB Output is correct
39 Correct 1 ms 4432 KB Output is correct
40 Correct 2 ms 4600 KB Output is correct
41 Correct 1 ms 4432 KB Output is correct
42 Correct 2 ms 4432 KB Output is correct
43 Correct 1 ms 4432 KB Output is correct
44 Correct 74 ms 27584 KB Output is correct
45 Correct 76 ms 27428 KB Output is correct
46 Correct 78 ms 27624 KB Output is correct
47 Correct 82 ms 28216 KB Output is correct
48 Correct 87 ms 33720 KB Output is correct
49 Correct 127 ms 40632 KB Output is correct
50 Correct 72 ms 26560 KB Output is correct
51 Correct 83 ms 28412 KB Output is correct
52 Correct 76 ms 28176 KB Output is correct
53 Correct 78 ms 28168 KB Output is correct
54 Correct 87 ms 32444 KB Output is correct
55 Correct 91 ms 33724 KB Output is correct
56 Correct 95 ms 30908 KB Output is correct
57 Correct 17 ms 9552 KB Output is correct
58 Correct 18 ms 9548 KB Output is correct
59 Correct 18 ms 9532 KB Output is correct
60 Correct 1 ms 4432 KB Output is correct
61 Incorrect 2 ms 6588 KB Output isn't correct
62 Halted 0 ms 0 KB -