Submission #792692

# Submission time Handle Problem Language Result Execution time Memory
792692 2023-07-25T08:06:48 Z 이성호(#10053) Line Town (CCO23_day1problem3) C++17
6 / 25
45 ms 20156 KB
#include <iostream>
#include <vector>
#define int long long
using namespace std;
const int inf = 1e18;
int N, k, h[500005];
int dp[500005], dp2[500005], cost[500005];
//cost[i]: i~i+k-1���� 0�� ��ġ�� ��� �ּ� cost
vector<int> zero;
vector<int> prf, suf;
vector<int> vc[2];
int sum(int l, int r)
{
    return (r - l + 1) * (r + l) / 2;
}
int count_odd(int l, int r)
{
    return (r + 1) / 2 - l / 2;
}
int count_even(int l, int r)
{
    return r / 2 - (l - 1) / 2;
}
signed main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> N;
    zero.push_back(0);
    for (int i = 1; i <= N; i++) {
        cin >> h[i];
        if (!h[i]) zero.push_back(i);
    }
    k = zero.size() - 1;
    prf.resize(k+2); suf.resize(k+2);
    for (int i = 1; i <= k; i++) prf[i] = prf[i-1] + zero[i];
    for (int i = k; i >= 1; i--) suf[i] = suf[i+1] + zero[i];
    int p = 0;
    for (int i = 1; i <= N - k + 1; i++) {
        while (p < k && zero[p+1] <= i + p) p++;
        cost[i] = sum(i, i+p-1) - prf[p] + suf[p+1] - sum(i+p, i+k-1);
    }
    for (int i = 1; i <= N; i++) {
        if (h[i] == 0) continue;
        vc[(i % 2 == 1) ^ (h[i] == -1)].push_back(i);
    }
    int p1 = 0, p2 = 0;
    for (int i = 1; i <= N; i++) {
        if (i % 2 == 1) {
            if (p1 < vc[0].size()) dp[i] = dp[i-1] + abs(vc[0][p1++] - i);
            else dp[i] = -1;
        }
        else {
            if (p2 < vc[1].size()) dp[i] = dp[i-1] + abs(vc[1][p2++] - i);
            else dp[i] = -1;
        }
        if (dp[i] == -1) {
            for (int j = i + 1; j <= N; j++) dp[j] = -1;
            break;
        }
    }
    p1 = (int)vc[0].size() - 1, p2 = (int)vc[1].size() - 1;
    for (int i = N; i >= 1; i--) {
        if (i % 2 == 0) {
            if (p1 >= 0) dp2[i] = dp2[i+1] + abs(vc[0][p1--] - i);
            else dp2[i] = -1;
        }
        else {
            if (p2 >= 0) dp2[i] = dp2[i+1] + abs(vc[1][p2--] - i);
            else dp2[i] = -1;
        }
        if (dp2[i] == -1) {
            for (int j = i - 1; j >= 1; j--) dp2[j] = -1;
            break;
        }
    }
    int ans = inf;
    for (int i = 0; i <= N - k; i++) {
        if (dp[i] == -1 || dp2[i+k+1] == -1) continue;
        int cst = dp[i] + cost[i+1] + dp2[i+k+1];
        if (vc[0].size() == count_odd(1, i) + count_even(i+k+1, N) && vc[1].size() == count_even(1, i) + count_odd(i+k+1, N)) ans = min(ans, cst);
    }
    cout << (ans == inf ? -1 : ans / 2) << '\n';
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:49:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |             if (p1 < vc[0].size()) dp[i] = dp[i-1] + abs(vc[0][p1++] - i);
      |                 ~~~^~~~~~~~~~~~~~
Main.cpp:53:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |             if (p2 < vc[1].size()) dp[i] = dp[i-1] + abs(vc[1][p2++] - i);
      |                 ~~~^~~~~~~~~~~~~~
Main.cpp:80:26: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   80 |         if (vc[0].size() == count_odd(1, i) + count_even(i+k+1, N) && vc[1].size() == count_even(1, i) + count_odd(i+k+1, N)) ans = min(ans, cst);
      |             ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:80:84: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   80 |         if (vc[0].size() == count_odd(1, i) + count_even(i+k+1, N) && vc[1].size() == count_even(1, i) + count_odd(i+k+1, N)) ans = min(ans, cst);
      |                                                                       ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 37 ms 20016 KB Output is correct
16 Correct 42 ms 19992 KB Output is correct
17 Correct 45 ms 20112 KB Output is correct
18 Correct 37 ms 19960 KB Output is correct
19 Correct 36 ms 20096 KB Output is correct
20 Correct 34 ms 19996 KB Output is correct
21 Correct 33 ms 20000 KB Output is correct
22 Correct 34 ms 20028 KB Output is correct
23 Correct 39 ms 20156 KB Output is correct
24 Correct 42 ms 19948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Incorrect 0 ms 340 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 37 ms 20016 KB Output is correct
16 Correct 42 ms 19992 KB Output is correct
17 Correct 45 ms 20112 KB Output is correct
18 Correct 37 ms 19960 KB Output is correct
19 Correct 36 ms 20096 KB Output is correct
20 Correct 34 ms 19996 KB Output is correct
21 Correct 33 ms 20000 KB Output is correct
22 Correct 34 ms 20028 KB Output is correct
23 Correct 39 ms 20156 KB Output is correct
24 Correct 42 ms 19948 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Incorrect 0 ms 340 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Incorrect 0 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Incorrect 0 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Incorrect 0 ms 340 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 37 ms 20016 KB Output is correct
16 Correct 42 ms 19992 KB Output is correct
17 Correct 45 ms 20112 KB Output is correct
18 Correct 37 ms 19960 KB Output is correct
19 Correct 36 ms 20096 KB Output is correct
20 Correct 34 ms 19996 KB Output is correct
21 Correct 33 ms 20000 KB Output is correct
22 Correct 34 ms 20028 KB Output is correct
23 Correct 39 ms 20156 KB Output is correct
24 Correct 42 ms 19948 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Incorrect 0 ms 340 KB Output isn't correct
27 Halted 0 ms 0 KB -