Submission #421910

# Submission time Handle Problem Language Result Execution time Memory
421910 2021-06-09T13:38:34 Z juancarlovieri Sequence (BOI14_sequence) C++17
67 / 100
1000 ms 2280 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

int ans = 1000000000000000000;
int pw[20];

void rek(vector<int> sections, int curr = 0, int level = 0, bool flag = false) {
  if (curr >= ans) return;
  int n = sections.size();

  if (n == 1) {
    int mask = sections[0];
    if (!mask) {
      if (!curr || flag) curr += pw[level];
    } else if (mask == 1) curr += pw[level + 1];
    else {
      if (mask & 1) {
        int mn = 10;
        for (int i = 9; i; --i) {
          if (mask & (1 << i)) mn = i;
        }
        for (int i = 9; i; --i) {
          if ((mask & (1 << i)) && i != mn) curr += i * pw[level++];
        }
        curr += mn * pw[++level];
      } else {
        for (int i = 9; i; --i) {
          if (mask & (1 << i)) curr += i * pw[level++];
        }
      }
    }
    ans = min(ans, curr);
    return;
  }

  for (int i = 0; i < 10; ++i) {
    vector<int> merged;
    int ins = 0;
    bool zero = false;
    int piv = i;
    for (int j = 0; j < n; ++j) {
      zero |= (piv == 0 && (sections[j] & 1));
      ins |= (sections[j] & (~(1 << piv)));
      piv++;
      if (piv == 10) {
        merged.push_back(ins);
        ins = piv = 0;
      }
    }
    if (piv) merged.push_back(ins);
    rek(merged, curr + pw[level] * i, level + 1, zero);
  }
}

signed main() {
  int n;
  cin >> n;
  vector<int> sections(n);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    sections[i] = (1 << x);
  }
  pw[0] = 1;
  for (int i = 1; i < 18; ++i) pw[i] = 10 * pw[i - 1];
  rek(sections);
  cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 5 ms 292 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 2 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 5 ms 204 KB Output is correct
11 Correct 2 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 3 ms 204 KB Output is correct
14 Correct 2 ms 204 KB Output is correct
15 Correct 2 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 4 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 2 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 2 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 7 ms 204 KB Output is correct
12 Correct 4 ms 204 KB Output is correct
13 Correct 2 ms 204 KB Output is correct
14 Correct 2 ms 204 KB Output is correct
15 Correct 2 ms 204 KB Output is correct
16 Correct 2 ms 292 KB Output is correct
17 Correct 2 ms 204 KB Output is correct
18 Correct 2 ms 204 KB Output is correct
19 Correct 3 ms 204 KB Output is correct
20 Correct 14 ms 312 KB Output is correct
21 Correct 3 ms 204 KB Output is correct
22 Correct 10 ms 204 KB Output is correct
23 Correct 10 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 24 ms 476 KB Output is correct
3 Correct 30 ms 492 KB Output is correct
4 Correct 24 ms 376 KB Output is correct
5 Correct 36 ms 492 KB Output is correct
6 Correct 11 ms 436 KB Output is correct
7 Correct 82 ms 1524 KB Output is correct
8 Correct 77 ms 1124 KB Output is correct
9 Correct 128 ms 2080 KB Output is correct
10 Correct 142 ms 2076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 5 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 194 ms 1112 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 2 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 2 ms 204 KB Output is correct
11 Correct 203 ms 2280 KB Output is correct
12 Correct 174 ms 2276 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 11 ms 204 KB Output is correct
15 Correct 6 ms 252 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 2 ms 204 KB Output is correct
19 Correct 2 ms 204 KB Output is correct
20 Correct 2 ms 204 KB Output is correct
21 Correct 2 ms 204 KB Output is correct
22 Correct 2 ms 204 KB Output is correct
23 Correct 9 ms 312 KB Output is correct
24 Correct 4 ms 204 KB Output is correct
25 Correct 10 ms 204 KB Output is correct
26 Correct 14 ms 312 KB Output is correct
27 Correct 25 ms 460 KB Output is correct
28 Correct 23 ms 524 KB Output is correct
29 Correct 22 ms 528 KB Output is correct
30 Correct 28 ms 460 KB Output is correct
31 Correct 12 ms 332 KB Output is correct
32 Correct 102 ms 1612 KB Output is correct
33 Correct 86 ms 1196 KB Output is correct
34 Correct 115 ms 2280 KB Output is correct
35 Correct 116 ms 2252 KB Output is correct
36 Correct 463 ms 1732 KB Output is correct
37 Correct 774 ms 2228 KB Output is correct
38 Correct 714 ms 1416 KB Output is correct
39 Execution timed out 1072 ms 2268 KB Time limit exceeded
40 Halted 0 ms 0 KB -