답안 #320943

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
320943 2020-11-10T10:16:54 Z phathnv Global Warming (CEOI18_glo) C++11
100 / 100
96 ms 7012 KB
#include <bits/stdc++.h>

#define mp make_pair
#define X first
#define Y second
#define taskname "GlobalWarming"

using namespace std;

typedef long long ll;
typedef pair <int, int> ii;

const int N = 2e5 + 1;
const int INF = 1e9 + 10;

struct fenwickTree{
    int d[N];
    void update(int x, int val){
        for(; x < N; x += x & -x)
            d[x] = max(d[x], val);
    }
    int get(int x){
        int res = 0;
        for(; x > 0; x -= x & -x)
            res = max(res, d[x]);
        return res;
    }
} bit;

int n, x, a[N], l[N], r[N], dp[N], ind[N];

void readInput(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> x;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
}

void solve(){
    dp[0] = -INF;
    for(int i = 1; i <= n; i++)
        dp[i] = INF;
    for(int i = 1; i <= n; i++){
        l[i] = lower_bound(dp, dp + n + 1, a[i]) - dp;
        dp[l[i]] = min(dp[l[i]], a[i]);
    }

    dp[0] = INF;
    for(int i = 1; i <= n; i++)
        dp[i] = -INF;
    for(int i = n; i >= 1; i--){
        r[i] = lower_bound(dp, dp + n + 1, a[i], greater<int>()) - dp;
        dp[r[i]] = max(dp[r[i]], a[i]);
    }
    
    for(int i = 1; i <= n; i++)
        ind[i] = i;
    sort(ind + 1, ind + n + 1, [=](const int &x, const int &y){
            return a[x] < a[y];
         });
    int ptr = 1, res = 0;
    for(int i = 1; i <= n; i++){
        int pos = ind[i];
        while (ptr <= n && a[pos] + x > a[ind[ptr]]){
            bit.update(ind[ptr], l[ind[ptr]]);
            ptr++;
        }
        res = max(res, r[pos] + bit.get(pos - 1));
    }
    cout << res;
}

int main(){
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    readInput();
    solve();
    return 0;
}

Compilation message

glo.cpp: In function 'int main()':
glo.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   77 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   78 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 7012 KB Output is correct
2 Correct 90 ms 7012 KB Output is correct
3 Correct 90 ms 7012 KB Output is correct
4 Correct 91 ms 7012 KB Output is correct
5 Correct 59 ms 6244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 2020 KB Output is correct
2 Correct 21 ms 2028 KB Output is correct
3 Correct 21 ms 2020 KB Output is correct
4 Correct 16 ms 1900 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 14 ms 2028 KB Output is correct
7 Correct 22 ms 2028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 3708 KB Output is correct
2 Correct 44 ms 3684 KB Output is correct
3 Correct 89 ms 7012 KB Output is correct
4 Correct 58 ms 6244 KB Output is correct
5 Correct 35 ms 3300 KB Output is correct
6 Correct 45 ms 5988 KB Output is correct
7 Correct 51 ms 6628 KB Output is correct
8 Correct 35 ms 3684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 364 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 364 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 89 ms 7012 KB Output is correct
28 Correct 90 ms 7012 KB Output is correct
29 Correct 90 ms 7012 KB Output is correct
30 Correct 91 ms 7012 KB Output is correct
31 Correct 59 ms 6244 KB Output is correct
32 Correct 20 ms 2020 KB Output is correct
33 Correct 21 ms 2028 KB Output is correct
34 Correct 21 ms 2020 KB Output is correct
35 Correct 16 ms 1900 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 14 ms 2028 KB Output is correct
38 Correct 22 ms 2028 KB Output is correct
39 Correct 44 ms 3708 KB Output is correct
40 Correct 44 ms 3684 KB Output is correct
41 Correct 89 ms 7012 KB Output is correct
42 Correct 58 ms 6244 KB Output is correct
43 Correct 35 ms 3300 KB Output is correct
44 Correct 45 ms 5988 KB Output is correct
45 Correct 51 ms 6628 KB Output is correct
46 Correct 35 ms 3684 KB Output is correct
47 Correct 43 ms 3688 KB Output is correct
48 Correct 52 ms 3684 KB Output is correct
49 Correct 96 ms 6924 KB Output is correct
50 Correct 59 ms 6244 KB Output is correct
51 Correct 51 ms 4836 KB Output is correct
52 Correct 58 ms 6244 KB Output is correct
53 Correct 60 ms 6244 KB Output is correct
54 Correct 52 ms 7012 KB Output is correct
55 Correct 75 ms 7012 KB Output is correct