QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#244481#7199. Bombucup-team004WA 1ms3480kbC++202.3kb2023-11-09 09:54:392023-11-09 09:54:40

Judging History

你现在查看的是最新测评结果

  • [2023-11-09 09:54:40]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3480kb
  • [2023-11-09 09:54:39]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr i64 inf = 1E18;

i64 square(i64 n) {
    return n * n;
}

void chmin(i64 &a, i64 b) {
    if (b < a) {
        a = b;
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n;
    while (std::cin >> n) {
        std::vector<int> x(n);
        for (int i = 0; i < n; i++) {
            std::cin >> x[i];
        }
        
        if (n == 1) {
            std::cout << 0 << "\n";
            continue;
        }
        
        i64 ans = inf;
        std::vector dp(n, std::vector<i64>(n, inf)), f(n, std::vector<i64>(n));
        for (int i = 0; i < n; i++) {
            i64 sum = 0;
            for (int j = i + 1; j < n; j++) {
                sum += square(x[j] - x[j - 1]);
                f[i][j] = sum;
            }
        }
        for (int i = 0; i < n - 1; i++) {
            int k = i;
            i64 min = inf, min1 = inf, min2 = inf;
            std::vector<i64> pre(n, inf);
            for (int j = 0; j < i; j++) {
                pre[j + 1] = std::min(pre[j], dp[i][k]);
                chmin(min1, dp[j][i]);
                chmin(min2, dp[i][j] - square(std::min(x[i + 1] - x[i], x[i] - x[j])));
            }
            for (int j = i + 1; j < n; j++) {
                if (i == 0) {
                    dp[i][j] = dp[j][i] = f[i][j] + square(x[j] - x[i]);
                } else {
                    while (k > 0 && x[i] - x[k - 1] <= x[j] - x[i]) {
                        k--;
                        chmin(min, dp[i][k] - square(x[i] - x[k]));
                    }
                    chmin(dp[i][j], std::min(min + square(x[j] - x[i]), pre[k]) + f[i][j]);
                    chmin(dp[i][j], pre[i] + std::max(0LL, square(x[j] - x[i]) - square(x[i] - x[i - 1])) + f[i][j]);
                    chmin(dp[j][i], min1 + f[i][j] + square(x[j] - x[i]) - square(std::min(x[i] - x[i - 1], x[i + 1] - x[i])));
                    chmin(dp[j][i], min2 + f[i][j] + square(x[j] - x[i]));
                }
            }
        }
        for (int i = 0; i < n - 1; i++) {
            chmin(ans, dp[i][n - 1]);
            chmin(ans, dp[n - 1][i]);
        }
        std::cout << ans << "\n";
    }
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3460kb

input:

5
1 4 5 6 10
3
1 2 6

output:

51
33

result:

ok 2 tokens

Test #2:

score: 0
Accepted
time: 1ms
memory: 3480kb

input:

18
1 2 3 6 11 23 47 106 235 551 1301 3159 7741 19320 48629 123867 317955 823065
5
1 5 6 7 11
2
1 1000000
3
1 12345 1000000
4
1 2 3 1000000

output:

554621353432
59
1999996000002
1951077172386
1999988000020

result:

ok 5 tokens

Test #3:

score: 0
Accepted
time: 0ms
memory: 3468kb

input:

5
1 2 5 6 7
5
1 4 5 7 10
5
1 2 5 7 10
5
1 4 5 7 10
5
1 2 5 6 7
5
1 4 5 7 10
5
1 2 5 6 7
5
1 2 5 6 7
5
2 5 6 7 9
5
2 5 6 7 9
5
1 4 5 7 10
5
2 3 5 6 9
5
2 5 6 7 9
5
1 2 5 6 7
5
2 3 6 8 9
5
1 2 5 7 10
5
2 3 5 6 9
5
1 4 5 7 10
5
1 2 5 6 7
5
1 2 5 7 10
5
2 5 6 7 9
5
1 2 5 7 10
5
1 4 5 7 10
5
2 3 6 8 9
5
...

output:

21
37
37
37
21
37
21
21
27
27
37
24
27
21
24
37
24
37
21
37
27
37
37
24
27
27
27
27
24
24
37
27
24
24
27
21
21
37
37
21
24
27
24
27
21
27
37
37
37
27
37
21
24
37
37
24
24
21
27
24
37
27
21
37
37
24
27
21
27
37
21
37
37
24
37
24
37
37
21
37
21
27
21
24
24
27
27
24
37
24
24
27
37
24
27
37
24
21
24
24

result:

ok 100 tokens

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 3400kb

input:

10
6 8 24 41 42 43 66 81 83 99
10
3 17 28 51 54 58 65 71 77 98
10
31 55 56 59 63 67 78 84 89 93
10
31 55 56 59 63 67 78 85 89 93
10
11 12 13 29 36 50 74 75 94 95
10
2 16 21 23 45 47 69 70 72 96
10
15 16 19 47 49 72 73 79 86 92
10
31 34 55 56 59 63 67 78 84 93
10
1 23 35 45 48 62 64 69 70 90
10
5 22 ...

output:

2158
2469
1512
1516
2274
2495
2362
1259
2188
2235
2027
2580
1498
2107
2188
2049
1500
2672
3627
2487
1348
2100
2233
1495
2107
2078
2083
2340
1495
2396
2496
1516
2258
2173
1500
2100
1555
2107
2523
2221
2276
2478
2202
1984
1555
2100
2153
2833
2202
2580
3735
2562
1196
2083
2083
2146
2146
2396
3735
2440
...

result:

wrong answer 2nd words differ - expected: '2429', found: '2469'