UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#214140#2031. aKXG60101ms2484kbC++2.1kb2024-11-15 19:53:162024-11-15 23:25:11

answer

#include <cstdio>
#include <algorithm>
using namespace std;
struct node {
    int l, r;
} x[100010];
bool cmp(node a, node b) {
    if (a.l == b.l) {
        return a.r > b.r;
    }
    return a.l < b.l;
}
int n, k;
int l[100010], r[100010], d[100010];
int dp[100010][110][2];
int main() {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &x[i].l, &x[i].r);
    }
    sort(x + 1, x + n + 1, cmp);
    int m = 0;
    int nowr = 0, nowl = 0, ans = 0;
    for (int i = 1; i <= n; i++) {
        if (x[i].l > nowr) {
            ans += nowr - nowl;
            nowl = x[i].l;
            nowr = x[i].r;
            x[++m] = x[i];
        } else if (x[i].r > nowr) {
            nowr = x[i].r;
            x[++m] = x[i];
        } else {
            k--;
        }
    }
    ans += nowr - nowl;
    if (k <= 0) {
        printf("%lld\n", ans);
        return 0;
    }
    for (int i = 1; i <= m; i++) {
        l[i] = x[i].l;
        r[i] = x[i].r;
    }
    for (int i = 1; i < m; i++) {
        if (x[i + 1].l > x[i].r) continue;
        int rightpos = upper_bound(l + 1, l + m + 1, x[i].r) - l - 1;
        d[i]++;
        d[rightpos]--;
    }
    n = 0;
    for (int i = 1; i <= m; i++) {
        d[i] += d[i - 1];
        if (d[i]) {
            k--;
        } else {
            x[++n] = x[i];
        }
    }
    if (k <= 0) {
        printf("%lld\n", ans);
        return 0;
    }
    for (int i = 0; i <= k; i++) {
        dp[0][i][0] = dp[0][i][1] = 1e9;
    }
    dp[0][0][0] = 0;
    x[n + 1].l = 1e9;
    x[n + 1].r = 1e9;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= k; j++) {
            dp[i][j][0] = min(dp[i - 1][j][0], dp[i - 1][j][1]);
            dp[i][j][1] = 1e9;
            if (j != 0) {
                dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j - 1][1] + min(x[i].r, x[i + 1].l) - x[i].l);
                dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j - 1][0] + min(x[i].r, x[i + 1].l) - max(x[i].l, x[i - 1].r));
            }
        }
    }
    printf("%d\n", ans - min(dp[n][k][0], dp[n][k][1]));
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 528kb

input:

20 8
941374267 958239792
636546050 949277063
30572593 458894768
377940690 585510776
595907552 909524...

output:

982023824

result:

ok single line: '982023824'

Test #2:

score: 10
Accepted
time: 0ms
memory: 532kb

input:

20 15
76365869 433406218
633171333 786737074
131600137 929106040
44288699 688307921
419471992 769278...

output:

923804666

result:

ok single line: '923804666'

Test #3:

score: 10
Accepted
time: 0ms
memory: 532kb

input:

20 2
358841118 908572644
771680732 777280263
232627681 399317312
146158799 355582975
95552785 629032...

output:

898648766

result:

ok single line: '898648766'

Test #4:

score: 10
Accepted
time: 0ms
memory: 572kb

input:

5000 49
383739070 641316367
609140743 773905546
333655225 869528584
456526030 666877251
488787058 91...

output:

999604831

result:

ok single line: '999604831'

Test #5:

score: 10
Accepted
time: 0ms
memory: 572kb

input:

5000 56
6389143 923791616
594084401 918014476
192256209 434682769
125655174 766893261
348541120 7426...

output:

999844854

result:

ok single line: '999844854'

Test #6:

score: 10
Accepted
time: 0ms
memory: 568kb

input:

5000 63
206266865 481555569
431544412 914639759
535710313 662467481
77260492 436949450
208295182 418...

output:

999740177

result:

ok single line: '999740177'

Test #7:

score: 0
Wrong Answer
time: 30ms
memory: 2480kb

input:

100000 95
31394575 31395303
35035434 35036166
64896609 64897277
10992021 10992677
17899328 17900254
...

output:

74902061

result:

wrong answer 1st lines differ - expected: '74878135', found: '74902061'

Test #8:

score: 0
Wrong Answer
time: 27ms
memory: 2484kb

input:

100000 83
233039267 233039367
916657359 916657459
864715109 864715209
936842396 936842496
562959930 ...

output:

9880673

result:

wrong answer 1st lines differ - expected: '9880370', found: '9880673'

Test #9:

score: 0
Wrong Answer
time: 23ms
memory: 2484kb

input:

100000 90
816642137 816642237
185449925 185450025
619425312 619425412
335954840 335954940
14523568 1...

output:

9879309

result:

wrong answer 1st lines differ - expected: '9879012', found: '9879309'

Test #10:

score: 0
Wrong Answer
time: 21ms
memory: 2480kb

input:

100000 85
932471751 932471851
591128329 591128429
632579047 632579147
902529780 902529880
377540930 ...

output:

9876989

result:

wrong answer 1st lines differ - expected: '9876663', found: '9876989'