Submission #1113371

# Submission time Handle Problem Language Result Execution time Memory
1113371 2024-11-16T13:29:44 Z VinhLuu Line Town (CCO23_day1problem3) C++17
6 / 25
2000 ms 65760 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 brute{
  vector<int> even[N], odd[N];
  void solve(){
    int ans = -1;
    for(int k = 0; k <= n - zero; k ++){
      for(int i = 0; i <= m; i ++){
        even[i].clear();
        odd[i].clear();
      }
      for(int i = n; i >= 1; i --){
        if(a[i] == 0){
          even[0].push_back(i);
          continue;
        }
        int x = abs(a[i]);
        if(i & 1){
          if(a[i] > 0) even[x].push_back(i);
          else odd[x].push_back(i);
        }else{
          if(a[i] > 0) odd[x].push_back(i);
          else even[x].push_back(i);
        }
      }
//      cerr << k << " start\n";
      bool ff = true;
      int cnt = 0;
      for(int i = 1; i <= n; i ++) bit[i] = 0;
      for(int i = 1; i <= k; i ++){
        int x = 1;
        if(i & 1){
          if(odd[x].empty()){
            ff = false;
            break;
          }
          int pos = odd[x].back();
          odd[x].pop_back();
          cnt += pos - 1 - get(pos - 1);
          update(pos, 1);
        }else{
          if(even[x].empty()){
            ff = false;
            break;
          }
          int pos = even[x].back();
          even[x].pop_back();
          cnt += pos - 1 - get(pos - 1);
          update(pos, 1);
        }
      }
//      cerr << ff << " " << cnt << " g\n";
      if(!ff) continue;
      while(!even[0].empty()){
        int pos = even[0].back();
        even[0].pop_back();
        cnt += pos - 1 - get(pos - 1);
        update(pos, 1);
      }
//      cerr << k << " " << cnt << " y\n";
      for(int i = k + zero + 1; i <= n; i ++){
        int x = 1;
        if(i & 1){
          if(even[x].empty()){
            ff = false;
            break;
          }
          int pos = even[x].back();
          even[x].pop_back();
          cnt += pos - 1 - get(pos - 1);
          update(pos, 1);
        }else{
          if(odd[x].empty()){
            ff = false;
            break;
          }
          int pos = odd[x].back();
          odd[x].pop_back();
          cnt += pos - 1 - get(pos - 1);
          update(pos, 1);
        }
      }
      if(!ff) continue;
//      cerr << k << " " << cnt << " result\n";
      add(ans, cnt);
    }
    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;
  }
  brute :: solve();
}
/*
5
0 -1 -1 0 1
*/

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:125:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |     freopen(task ".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:126:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |     freopen(task ".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 30 ms 47176 KB Output is correct
2 Correct 30 ms 47344 KB Output is correct
3 Correct 30 ms 47256 KB Output is correct
4 Correct 76 ms 47440 KB Output is correct
5 Correct 73 ms 47440 KB Output is correct
6 Correct 74 ms 47432 KB Output is correct
7 Correct 76 ms 47532 KB Output is correct
8 Correct 76 ms 47516 KB Output is correct
9 Correct 77 ms 47440 KB Output is correct
10 Correct 71 ms 47440 KB Output is correct
11 Correct 69 ms 47432 KB Output is correct
12 Correct 72 ms 47440 KB Output is correct
13 Correct 70 ms 47440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 47176 KB Output is correct
2 Correct 30 ms 47344 KB Output is correct
3 Correct 30 ms 47256 KB Output is correct
4 Correct 76 ms 47440 KB Output is correct
5 Correct 73 ms 47440 KB Output is correct
6 Correct 74 ms 47432 KB Output is correct
7 Correct 76 ms 47532 KB Output is correct
8 Correct 76 ms 47516 KB Output is correct
9 Correct 77 ms 47440 KB Output is correct
10 Correct 71 ms 47440 KB Output is correct
11 Correct 69 ms 47432 KB Output is correct
12 Correct 72 ms 47440 KB Output is correct
13 Correct 70 ms 47440 KB Output is correct
14 Correct 32 ms 47432 KB Output is correct
15 Execution timed out 2076 ms 65760 KB Time limit exceeded
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 47176 KB Output is correct
2 Correct 30 ms 47344 KB Output is correct
3 Correct 30 ms 47256 KB Output is correct
4 Correct 76 ms 47440 KB Output is correct
5 Correct 73 ms 47440 KB Output is correct
6 Correct 74 ms 47432 KB Output is correct
7 Correct 76 ms 47532 KB Output is correct
8 Correct 76 ms 47516 KB Output is correct
9 Correct 77 ms 47440 KB Output is correct
10 Correct 71 ms 47440 KB Output is correct
11 Correct 69 ms 47432 KB Output is correct
12 Correct 72 ms 47440 KB Output is correct
13 Correct 70 ms 47440 KB Output is correct
14 Correct 32 ms 47424 KB Output is correct
15 Correct 32 ms 47448 KB Output is correct
16 Correct 34 ms 47440 KB Output is correct
17 Correct 52 ms 47440 KB Output is correct
18 Correct 52 ms 47440 KB Output is correct
19 Correct 56 ms 47440 KB Output is correct
20 Correct 48 ms 47436 KB Output is correct
21 Correct 59 ms 47432 KB Output is correct
22 Correct 67 ms 47440 KB Output is correct
23 Correct 47 ms 47432 KB Output is correct
24 Correct 48 ms 47324 KB Output is correct
25 Correct 48 ms 47432 KB Output is correct
26 Correct 47 ms 47440 KB Output is correct
27 Correct 53 ms 47440 KB Output is correct
28 Correct 53 ms 47532 KB Output is correct
29 Correct 66 ms 47440 KB Output is correct
30 Correct 30 ms 47440 KB Output is correct
31 Correct 29 ms 47440 KB Output is correct
32 Correct 30 ms 47380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 47176 KB Output is correct
2 Correct 30 ms 47344 KB Output is correct
3 Correct 30 ms 47256 KB Output is correct
4 Correct 76 ms 47440 KB Output is correct
5 Correct 73 ms 47440 KB Output is correct
6 Correct 74 ms 47432 KB Output is correct
7 Correct 76 ms 47532 KB Output is correct
8 Correct 76 ms 47516 KB Output is correct
9 Correct 77 ms 47440 KB Output is correct
10 Correct 71 ms 47440 KB Output is correct
11 Correct 69 ms 47432 KB Output is correct
12 Correct 72 ms 47440 KB Output is correct
13 Correct 70 ms 47440 KB Output is correct
14 Correct 32 ms 47432 KB Output is correct
15 Execution timed out 2076 ms 65760 KB Time limit exceeded
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 47432 KB Output is correct
2 Incorrect 29 ms 47440 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 47432 KB Output is correct
2 Incorrect 29 ms 47440 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 47176 KB Output is correct
2 Correct 30 ms 47344 KB Output is correct
3 Correct 30 ms 47256 KB Output is correct
4 Correct 76 ms 47440 KB Output is correct
5 Correct 73 ms 47440 KB Output is correct
6 Correct 74 ms 47432 KB Output is correct
7 Correct 76 ms 47532 KB Output is correct
8 Correct 76 ms 47516 KB Output is correct
9 Correct 77 ms 47440 KB Output is correct
10 Correct 71 ms 47440 KB Output is correct
11 Correct 69 ms 47432 KB Output is correct
12 Correct 72 ms 47440 KB Output is correct
13 Correct 70 ms 47440 KB Output is correct
14 Correct 32 ms 47424 KB Output is correct
15 Correct 32 ms 47448 KB Output is correct
16 Correct 34 ms 47440 KB Output is correct
17 Correct 52 ms 47440 KB Output is correct
18 Correct 52 ms 47440 KB Output is correct
19 Correct 56 ms 47440 KB Output is correct
20 Correct 48 ms 47436 KB Output is correct
21 Correct 59 ms 47432 KB Output is correct
22 Correct 67 ms 47440 KB Output is correct
23 Correct 47 ms 47432 KB Output is correct
24 Correct 48 ms 47324 KB Output is correct
25 Correct 48 ms 47432 KB Output is correct
26 Correct 47 ms 47440 KB Output is correct
27 Correct 53 ms 47440 KB Output is correct
28 Correct 53 ms 47532 KB Output is correct
29 Correct 66 ms 47440 KB Output is correct
30 Correct 30 ms 47440 KB Output is correct
31 Correct 29 ms 47440 KB Output is correct
32 Correct 30 ms 47380 KB Output is correct
33 Correct 30 ms 47432 KB Output is correct
34 Incorrect 29 ms 47440 KB Output isn't correct
35 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 30 ms 47176 KB Output is correct
2 Correct 30 ms 47344 KB Output is correct
3 Correct 30 ms 47256 KB Output is correct
4 Correct 76 ms 47440 KB Output is correct
5 Correct 73 ms 47440 KB Output is correct
6 Correct 74 ms 47432 KB Output is correct
7 Correct 76 ms 47532 KB Output is correct
8 Correct 76 ms 47516 KB Output is correct
9 Correct 77 ms 47440 KB Output is correct
10 Correct 71 ms 47440 KB Output is correct
11 Correct 69 ms 47432 KB Output is correct
12 Correct 72 ms 47440 KB Output is correct
13 Correct 70 ms 47440 KB Output is correct
14 Correct 32 ms 47432 KB Output is correct
15 Execution timed out 2076 ms 65760 KB Time limit exceeded
16 Halted 0 ms 0 KB -