Submission #875375

# Submission time Handle Problem Language Result Execution time Memory
875375 2023-11-19T11:26:54 Z mgl_diamond Just Long Neckties (JOI20_ho_t1) C++17
100 / 100
73 ms 8144 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 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4532 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4444 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4440 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4532 KB Output is correct
25 Correct 2 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4444 KB Output is correct
31 Correct 1 ms 4444 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
33 Correct 67 ms 7572 KB Output is correct
34 Correct 70 ms 7756 KB Output is correct
35 Correct 70 ms 7628 KB Output is correct
36 Correct 69 ms 8144 KB Output is correct
37 Correct 72 ms 8144 KB Output is correct
38 Correct 71 ms 8132 KB Output is correct
39 Correct 69 ms 7372 KB Output is correct
40 Correct 69 ms 7340 KB Output is correct
41 Correct 69 ms 7364 KB Output is correct
42 Correct 69 ms 7340 KB Output is correct
43 Correct 72 ms 7396 KB Output is correct
44 Correct 68 ms 7064 KB Output is correct
45 Correct 68 ms 7064 KB Output is correct
46 Correct 69 ms 7096 KB Output is correct
47 Correct 54 ms 6732 KB Output is correct
48 Correct 53 ms 6860 KB Output is correct
49 Correct 71 ms 7384 KB Output is correct
50 Correct 73 ms 7372 KB Output is correct
51 Correct 73 ms 7380 KB Output is correct
52 Correct 71 ms 7364 KB Output is correct
53 Correct 72 ms 7368 KB Output is correct