Submission #875374

# Submission time Handle Problem Language Result Execution time Memory
875374 2023-11-19T11:26:21 Z mgl_diamond Just Long Neckties (JOI20_ho_t1) C++17
100 / 100
153 ms 12064 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
 
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define file "input"

template<class T> bool minimize(T &a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> bool maximize(T &a, T b) { if (a < b) { a = b; return 1; } return 0; }

void setIO() {
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  if (fopen(file".inp", "r")) {
    freopen(file".inp", "r", stdin);
    freopen(file".out", "w", stdout);
  }
}

const int N = 2e5+5;
int n, a[N], b[N], ret[N];

int f[1<<21];

void solve1() {
  memset(ret, 0x3f, sizeof(ret));
  memset(f, 0x3f, sizeof(f));
  f[0] = 0;
  int full = (1<<(n+1))-1;
  foru(i, 0, full) {
    int cnt = __builtin_popcount(i)+1, zero;
    foru(j, 0, n) if (!(i >> j & 1)) {
      minimize(f[i+(1<<j)], max(f[i], a[j+1]-b[cnt]));
      zero = j+1;
    }
    if (cnt == n+1) {
      minimize(ret[zero], f[i]);
    }
  }
  foru(i, 1, n+1) {
    cout << ret[i] << " ";
  } cout << "\n";
}

void solve2() {
  sort(b+1, b+n+1);
  foru(i, 1, n+1) {
    vector<int> tmp;
    foru(j, 1, n+1) if (i^j) tmp.push_back(a[j]);
    sort(all(tmp));
    int ans = 0;
    foru(i, 1, n) maximize(ans, tmp[i-1] - b[i]);
    cout << ans << " "; 
  } cout << "\n";
}

void solve3() {
  vector<int> idx;
  foru(i, 1, n+1) idx.push_back(i);
  sort(all(idx), [&](int x, int y){ return a[x] < a[y]; });
  sort(b+1, b+n+1);

  vector<int> suff(n+1);
  ford(i, n-1, 1) {
    suff[i] = suff[i+1];
    maximize(suff[i], a[idx[i]]-b[i]);
  }

  int cur_max = 0;
  foru(i, 1, n) {
    ret[idx[i-1]] = cur_max;
    maximize(ret[idx[i-1]], suff[i]);
    maximize(ret[idx[i-1]], a[idx[n]] - b[n]);
    maximize(cur_max, a[idx[i-1]] - b[i]);
  }

  ret[idx[n]] = cur_max;
  foru(i, 1, n+1) cout << ret[i] << " "; cout << "\n";
}

int main() {
  setIO();

  cin >> n;
  foru(i, 1, n+1) cin >> a[i];
  foru(i, 1, n) cin >> b[i];

  // solve1();
  // solve2();
  // solve3();
  if (n <= 20) solve1();
  else if (n <= 2000) solve2();
  else solve3();
}

Compilation message

ho_t1.cpp: In function 'void solve3()':
ho_t1.cpp:6:23: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
    6 | #define foru(i, l, r) for(int i=(l); i<=(r); ++i)
      |                       ^~~
ho_t1.cpp:85:3: note: in expansion of macro 'foru'
   85 |   foru(i, 1, n+1) cout << ret[i] << " "; cout << "\n";
      |   ^~~~
ho_t1.cpp:85:42: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   85 |   foru(i, 1, n+1) cout << ret[i] << " "; cout << "\n";
      |                                          ^~~~
ho_t1.cpp: In function 'void setIO()':
ho_t1.cpp:22:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |     freopen(file".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ho_t1.cpp:23:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |     freopen(file".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ho_t1.cpp: In function 'void solve1()':
ho_t1.cpp:15:61: warning: 'zero' may be used uninitialized in this function [-Wmaybe-uninitialized]
   15 | template<class T> bool minimize(T &a, T b) { if (a > b) { a = b; return 1; } return 0; }
      |                                                           ~~^~~
ho_t1.cpp:38:40: note: 'zero' was declared here
   38 |     int cnt = __builtin_popcount(i)+1, zero;
      |                                        ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 1 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 2 ms 10840 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 1 ms 10844 KB Output is correct
11 Correct 2 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 1 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 2 ms 10840 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 1 ms 10844 KB Output is correct
11 Correct 2 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 22 ms 2396 KB Output is correct
15 Correct 69 ms 2528 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 120 ms 2648 KB Output is correct
18 Correct 85 ms 2396 KB Output is correct
19 Correct 89 ms 2396 KB Output is correct
20 Correct 87 ms 2520 KB Output is correct
21 Correct 91 ms 2532 KB Output is correct
22 Correct 85 ms 2392 KB Output is correct
23 Correct 85 ms 2516 KB Output is correct
24 Correct 95 ms 2768 KB Output is correct
25 Correct 132 ms 2396 KB Output is correct
26 Correct 120 ms 2392 KB Output is correct
27 Correct 117 ms 2392 KB Output is correct
28 Correct 131 ms 2396 KB Output is correct
29 Correct 94 ms 2396 KB Output is correct
30 Correct 118 ms 2516 KB Output is correct
31 Correct 140 ms 2532 KB Output is correct
32 Correct 153 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 1 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 2 ms 10840 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 2 ms 10844 KB Output is correct
10 Correct 1 ms 10844 KB Output is correct
11 Correct 2 ms 10844 KB Output is correct
12 Correct 2 ms 10844 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 22 ms 2396 KB Output is correct
15 Correct 69 ms 2528 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 120 ms 2648 KB Output is correct
18 Correct 85 ms 2396 KB Output is correct
19 Correct 89 ms 2396 KB Output is correct
20 Correct 87 ms 2520 KB Output is correct
21 Correct 91 ms 2532 KB Output is correct
22 Correct 85 ms 2392 KB Output is correct
23 Correct 85 ms 2516 KB Output is correct
24 Correct 95 ms 2768 KB Output is correct
25 Correct 132 ms 2396 KB Output is correct
26 Correct 120 ms 2392 KB Output is correct
27 Correct 117 ms 2392 KB Output is correct
28 Correct 131 ms 2396 KB Output is correct
29 Correct 94 ms 2396 KB Output is correct
30 Correct 118 ms 2516 KB Output is correct
31 Correct 140 ms 2532 KB Output is correct
32 Correct 153 ms 2652 KB Output is correct
33 Correct 67 ms 7632 KB Output is correct
34 Correct 79 ms 11700 KB Output is correct
35 Correct 70 ms 11400 KB Output is correct
36 Correct 70 ms 11984 KB Output is correct
37 Correct 73 ms 12008 KB Output is correct
38 Correct 72 ms 12064 KB Output is correct
39 Correct 70 ms 11212 KB Output is correct
40 Correct 70 ms 11092 KB Output is correct
41 Correct 70 ms 11196 KB Output is correct
42 Correct 73 ms 11068 KB Output is correct
43 Correct 72 ms 11212 KB Output is correct
44 Correct 69 ms 10704 KB Output is correct
45 Correct 78 ms 10700 KB Output is correct
46 Correct 69 ms 10696 KB Output is correct
47 Correct 55 ms 10800 KB Output is correct
48 Correct 56 ms 10704 KB Output is correct
49 Correct 72 ms 11216 KB Output is correct
50 Correct 72 ms 11188 KB Output is correct
51 Correct 71 ms 11312 KB Output is correct
52 Correct 72 ms 11208 KB Output is correct
53 Correct 71 ms 11216 KB Output is correct