답안 #993530

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
993530 2024-06-06T00:21:13 Z kkzyr Global Warming (CEOI18_glo) C++17
100 / 100
979 ms 40024 KB
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
using namespace std;

struct triple{
    int start_number;
    int dp_value;
    bool still_exists;
};

bool cmp(triple t1, triple t2){
    if (t1.start_number < t2.start_number){
        return true;
    }
    if (t1.start_number > t2.start_number){
        return false;
    }
    if (t1.dp_value > t2.dp_value){
        return true;
    }
    if (t1.dp_value < t2.dp_value){
        return false;
    }
    return false;
}

const int MAX_SQRT_N = 1e3;
const int MAX_N = 2e5 + 1;
const int INF = 1e9 + 5;

int n, x;
int nums[MAX_N];

map<triple, set<int>, decltype(&cmp)> m(cmp);

int num_values;
triple values[MAX_N];

int lis[MAX_N];
int dp1[MAX_N];
int dp2[MAX_N];

int block_size, num_blocks;
int which_block[MAX_N];
int block_start[MAX_SQRT_N];
int block_end[MAX_SQRT_N];
int block_max[MAX_SQRT_N];

void input(){
    cin >> n >> x;
    for (int i = 1;i <= n;i++){
        cin >> nums[i];
    }
}

int binary_search(int key){
    int lo = 1;
    int hi = n;
    int ans = 0;
    while (lo <= hi){
        int mid = (lo + hi)/2;
        if (lis[mid] < key){
            ans = mid;
            lo = mid + 1;
        }
        else{
            hi = mid - 1;
        }
    }
    return ans;
}

int binary_search2(int key){
    int lo = 1;
    int hi = n;
    int ans = 0;
    while (lo <= hi){
        int mid = (lo + hi)/2;
        if (lis[mid] > key and lis[mid] != INF){
            ans = mid;
            lo = mid + 1;
        }
        else{
            hi = mid - 1;
        }
    }
    return ans;
}

void lis1(){
    for (int i = 1;i <= n;i++){
        lis[i] = INF;
    }
    for (int i = 1;i <= n;i++) {
        int key = nums[i];
        int index = binary_search(key);
        lis[index + 1] = key;
        dp1[i] = index + 1;
    }
}

void lis2(){
    for (int i = 1;i <= n;i++){
        lis[i] = INF;
    }
    for (int i = n;i >= 1;i--) {
        int key = nums[i];
        int index = binary_search2(key);
        lis[index + 1] = key;
        dp2[i] = index + 1;
    }
}

void process_input(){
    for (int i = 1;i <= n;i++){
        num_values += 1;
        values[num_values] = {nums[i], dp2[i], true};
    }
    sort(values, values + n + 1, cmp);
    for (int i = 1;i <= n;i++){
        m[values[i]].insert(i);
    }
}

void preprocess(){
    for (int i = 1;i * i <= n;i++){
         block_size = i;
    }
    num_blocks = n/block_size;
    if (n % block_size != 0){
        num_blocks += 1;
    }
    for (int i = 1;i <= num_blocks;i++){
        block_start[i] = (i - 1) * block_size + 1;
        block_end[i] = min(i * block_size, n);
        for (int j = block_start[i];j <= block_end[i];j++){
            which_block[j] = i;
            block_max[i] = max(block_max[i], values[j].dp_value);
        }
    }
}

void update_pos(int i){
    values[i].still_exists = false;
    int block = which_block[i];
    block_max[block] = 0;
    for (int j = block_start[block];j <= block_end[block];j++){
        if (values[j].still_exists){
            block_max[block] = max(block_max[block], values[j].dp_value);
        }
    }
}

int range_max(int key){
    int ans = 0;
    for (int i = 1;i <= num_blocks;i++){
        int start = block_start[i];
        int end = block_end[i];
        if (values[start].start_number > key){
            ans = max(ans, block_max[i]);
        }
        else if (values[end].start_number > key){
            for (int j = start;j <= end;j++){
                if (values[j].still_exists and values[j].start_number > key){
                    ans = max(ans, values[j].dp_value);
                }
            }
        }
    }
    return ans;
}

int main(){
    input();
    lis1();
    lis2();
    process_input();
    preprocess();
    int ans = 0;
    for (int i = 1;i <= n;i++){
        update_pos(*(m[{nums[i], dp2[i], true}].begin()));
        m[{nums[i], dp2[i], true}].erase(*(m[{nums[i], dp2[i], true}].begin()));
        int new_sum = dp1[i] + range_max(nums[i] - x);
        /*
        for (int j = 1;j <= n;j++){
            if (values[j].still_exists){
                cout << values[j].start_number << ' ' << values[j].dp_value << '\n';
            }
        }
        */
        ans = max(ans, new_sum);
    }
    cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4472 KB Output is correct
6 Correct 0 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 0 ms 4444 KB Output is correct
9 Correct 0 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4472 KB Output is correct
6 Correct 0 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 0 ms 4444 KB Output is correct
9 Correct 0 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 4440 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4440 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4472 KB Output is correct
6 Correct 0 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 0 ms 4444 KB Output is correct
9 Correct 0 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 4440 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4440 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 2 ms 4700 KB Output is correct
20 Correct 2 ms 4700 KB Output is correct
21 Correct 3 ms 4700 KB Output is correct
22 Correct 2 ms 4700 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4700 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 954 ms 37808 KB Output is correct
2 Correct 979 ms 39744 KB Output is correct
3 Correct 941 ms 39924 KB Output is correct
4 Correct 950 ms 39920 KB Output is correct
5 Correct 434 ms 39248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 145 ms 12632 KB Output is correct
2 Correct 131 ms 12624 KB Output is correct
3 Correct 132 ms 12624 KB Output is correct
4 Correct 63 ms 12720 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 66 ms 12624 KB Output is correct
7 Correct 104 ms 12720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 262 ms 20564 KB Output is correct
2 Correct 257 ms 20564 KB Output is correct
3 Correct 696 ms 37884 KB Output is correct
4 Correct 343 ms 38992 KB Output is correct
5 Correct 147 ms 21328 KB Output is correct
6 Correct 321 ms 37716 KB Output is correct
7 Correct 331 ms 38328 KB Output is correct
8 Correct 198 ms 21588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4472 KB Output is correct
6 Correct 0 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 0 ms 4444 KB Output is correct
9 Correct 0 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 4440 KB Output is correct
14 Correct 1 ms 4440 KB Output is correct
15 Correct 1 ms 4440 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 2 ms 4700 KB Output is correct
20 Correct 2 ms 4700 KB Output is correct
21 Correct 3 ms 4700 KB Output is correct
22 Correct 2 ms 4700 KB Output is correct
23 Correct 1 ms 4700 KB Output is correct
24 Correct 1 ms 4700 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4544 KB Output is correct
27 Correct 954 ms 37808 KB Output is correct
28 Correct 979 ms 39744 KB Output is correct
29 Correct 941 ms 39924 KB Output is correct
30 Correct 950 ms 39920 KB Output is correct
31 Correct 434 ms 39248 KB Output is correct
32 Correct 145 ms 12632 KB Output is correct
33 Correct 131 ms 12624 KB Output is correct
34 Correct 132 ms 12624 KB Output is correct
35 Correct 63 ms 12720 KB Output is correct
36 Correct 1 ms 4444 KB Output is correct
37 Correct 66 ms 12624 KB Output is correct
38 Correct 104 ms 12720 KB Output is correct
39 Correct 262 ms 20564 KB Output is correct
40 Correct 257 ms 20564 KB Output is correct
41 Correct 696 ms 37884 KB Output is correct
42 Correct 343 ms 38992 KB Output is correct
43 Correct 147 ms 21328 KB Output is correct
44 Correct 321 ms 37716 KB Output is correct
45 Correct 331 ms 38328 KB Output is correct
46 Correct 198 ms 21588 KB Output is correct
47 Correct 359 ms 21588 KB Output is correct
48 Correct 348 ms 21584 KB Output is correct
49 Correct 966 ms 39992 KB Output is correct
50 Correct 397 ms 38996 KB Output is correct
51 Correct 249 ms 24540 KB Output is correct
52 Correct 431 ms 39248 KB Output is correct
53 Correct 324 ms 39172 KB Output is correct
54 Correct 338 ms 39760 KB Output is correct
55 Correct 737 ms 40024 KB Output is correct