답안 #397521

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
397521 2021-05-02T10:25:31 Z ollel Global Warming (CEOI18_glo) C++14
100 / 100
1293 ms 39072 KB
#include <bits/stdc++.h>
using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define pb push_back

typedef vector<int> vi;
typedef long long ll;

class SegTree {
public:
  vi tr; int n;

  int l(int x) {return (x<<1);}
  int r(int x) {return (x<<1)|1;}

  int combine(int a, int b) {return max(a, b);}

  void build(int i, int L, int R, vi& arr) {
    if (L == R) tr[i] = arr[L];
    else {
      int mid = (L + R) / 2;
      build(l(i), L, mid, arr);
      build(r(i), mid + 1, R, arr);
      tr[i] = combine(tr[l(i)], tr[r(i)]);
    }
  }

  void build(vi &arr) {
    build(1, 0, n - 1, arr);
  }

  SegTree(vi &arr) {
    n = arr.size();
    tr.resize(4*n);
    this->build(arr);
  }

  int query(int i, int a, int b, int L, int R) {
    if (b < a) return 0;
    if (L > b || R < a) return 0;
    if (L >= a && R <= b) return tr[i];
    int mid = (L + R) / 2;
    return combine(
      query(l(i), a, b, L, mid),
      query(r(i), a, b, mid + 1, R));
  }

  int query(int i, int j) {
    return query(1, i, j, 0, n - 1);
  }

  void update(int i, int a, int L, int R, int v) {
    if (a < L || a > R) return;
    if (L == R) {tr[i] = v; return;}
    int mid = (L + R) / 2;
    update(l(i), a, L, mid, v);
    update(r(i), a, mid + 1, R, v);
    tr[i] = combine(tr[l(i)], tr[r(i)]);
  }

  void update(int i, int v) {
    update(1, i, 0, n - 1, v);
  }
};

int n, x;
vi arr;
vi LIS, LDS;
map<int,int> cmpr;
vi vals;


void prep(vi&arr) {
  LIS.resize(n); LDS.resize(n); vals.resize(n);

  rep(i,0,n) vals[i] = arr[i];
  rep(i,0,n) vals.pb(vals[i] - x);
  sort(vals.begin(), vals.end());
  rep(i,0,2*n) {
    if (cmpr.find(vals[i]) == cmpr.end()) cmpr.insert({vals[i], i});
  }

  vi trarr(2*n, 0);
  SegTree st(trarr);
  rep(i,0,n) {
    int V = cmpr[arr[i]];
    int ans = st.query(0, V - 1) + 1;
    LIS[i] = ans;
    if (st.query(V, V) < ans) st.update(V, ans);
  }

  SegTree std(trarr);
  for (int i = n - 1; i >= 0; i--) {
    int V = cmpr[arr[i]];
    int ans = std.query(V + 1, 2*n - 1) + 1;
    LDS[i] = ans;
    if (std.query(V, V) < ans) std.update(V, ans);
  }
  // rep(i,0,n) cout << LDS[i] << " ";cout<<endl;
}

void finish() {
  int ans = LIS[n - 1];
  // vi revcmpr(n);
  // for (auto &i : cmpr) revcmpr[i.second] = i.first;
  vi trarr(2*n, 0);
  SegTree st(trarr);
  for (int i = n - 1; i >= 0; i--) {
    int V = cmpr[arr[i]];
    auto mini = lower_bound(vals.begin(), vals.end(), arr[i] - x);
    int M = *mini;
    M = cmpr[M];

    int best = st.query(M + 1, 2*n - 1) + LIS[i];
    // cout << i << ": " << st.query(M + 1, n - 1) << " "<< LIS[i]<<endl;
    ans = max(ans, best);

    st.update(V, LDS[i]);
  }
  cout << ans << endl;
}

int main()
{
  cin >> n >> x;
  arr.resize(n);
  rep(i,0,n) cin>>arr[i];

  prep(arr);
  finish();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 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 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 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 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 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 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 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 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 2 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 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 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 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 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 2 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 4 ms 472 KB Output is correct
20 Correct 3 ms 460 KB Output is correct
21 Correct 3 ms 460 KB Output is correct
22 Correct 3 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1070 ms 27688 KB Output is correct
2 Correct 1084 ms 27760 KB Output is correct
3 Correct 1057 ms 27688 KB Output is correct
4 Correct 1059 ms 27700 KB Output is correct
5 Correct 459 ms 22964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 209 ms 9472 KB Output is correct
2 Correct 213 ms 9456 KB Output is correct
3 Correct 209 ms 9472 KB Output is correct
4 Correct 103 ms 7108 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 121 ms 7428 KB Output is correct
7 Correct 168 ms 9112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 488 ms 18728 KB Output is correct
2 Correct 501 ms 19668 KB Output is correct
3 Correct 1254 ms 39012 KB Output is correct
4 Correct 488 ms 28816 KB Output is correct
5 Correct 288 ms 19260 KB Output is correct
6 Correct 565 ms 36320 KB Output is correct
7 Correct 610 ms 37084 KB Output is correct
8 Correct 375 ms 19736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 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 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 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 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 2 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 4 ms 472 KB Output is correct
20 Correct 3 ms 460 KB Output is correct
21 Correct 3 ms 460 KB Output is correct
22 Correct 3 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 1070 ms 27688 KB Output is correct
28 Correct 1084 ms 27760 KB Output is correct
29 Correct 1057 ms 27688 KB Output is correct
30 Correct 1059 ms 27700 KB Output is correct
31 Correct 459 ms 22964 KB Output is correct
32 Correct 209 ms 9472 KB Output is correct
33 Correct 213 ms 9456 KB Output is correct
34 Correct 209 ms 9472 KB Output is correct
35 Correct 103 ms 7108 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 121 ms 7428 KB Output is correct
38 Correct 168 ms 9112 KB Output is correct
39 Correct 488 ms 18728 KB Output is correct
40 Correct 501 ms 19668 KB Output is correct
41 Correct 1254 ms 39012 KB Output is correct
42 Correct 488 ms 28816 KB Output is correct
43 Correct 288 ms 19260 KB Output is correct
44 Correct 565 ms 36320 KB Output is correct
45 Correct 610 ms 37084 KB Output is correct
46 Correct 375 ms 19736 KB Output is correct
47 Correct 488 ms 19732 KB Output is correct
48 Correct 512 ms 19644 KB Output is correct
49 Correct 1293 ms 38888 KB Output is correct
50 Correct 468 ms 28816 KB Output is correct
51 Correct 391 ms 21660 KB Output is correct
52 Correct 567 ms 29008 KB Output is correct
53 Correct 598 ms 38296 KB Output is correct
54 Correct 663 ms 39072 KB Output is correct
55 Correct 866 ms 35952 KB Output is correct