답안 #792707

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
792707 2023-07-25T08:12:08 Z 이성호(#10053) Line Town (CCO23_day1problem3) C++17
6 / 25
54 ms 16228 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 - k + 1; i++) {
        for (int j = 1; j <= k; j++) {
            cost[i] += abs(zero[j] - (i + j - 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:54: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]
   54 |             if (p1 < vc[0].size()) dp[i] = dp[i-1] + abs(vc[0][p1++] - i);
      |                 ~~~^~~~~~~~~~~~~~
Main.cpp:58: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]
   58 |             if (p2 < vc[1].size()) dp[i] = dp[i-1] + abs(vc[1][p2++] - i);
      |                 ~~~^~~~~~~~~~~~~~
Main.cpp:85: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]
   85 |         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:85: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]
   85 |         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);
      |                                                                       ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 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 1 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 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 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 1 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 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 36 ms 16228 KB Output is correct
16 Correct 54 ms 16168 KB Output is correct
17 Correct 36 ms 16192 KB Output is correct
18 Correct 37 ms 16160 KB Output is correct
19 Correct 38 ms 16172 KB Output is correct
20 Correct 40 ms 16064 KB Output is correct
21 Correct 32 ms 16080 KB Output is correct
22 Correct 34 ms 16092 KB Output is correct
23 Correct 37 ms 16212 KB Output is correct
24 Correct 36 ms 16136 KB Output is correct
# 결과 실행 시간 메모리 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 1 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 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Incorrect 0 ms 340 KB Output isn't correct
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 1 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 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 36 ms 16228 KB Output is correct
16 Correct 54 ms 16168 KB Output is correct
17 Correct 36 ms 16192 KB Output is correct
18 Correct 37 ms 16160 KB Output is correct
19 Correct 38 ms 16172 KB Output is correct
20 Correct 40 ms 16064 KB Output is correct
21 Correct 32 ms 16080 KB Output is correct
22 Correct 34 ms 16092 KB Output is correct
23 Correct 37 ms 16212 KB Output is correct
24 Correct 36 ms 16136 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Incorrect 0 ms 340 KB Output isn't correct
27 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 1 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 1 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 1 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 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Incorrect 0 ms 340 KB Output isn't correct
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 1 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 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 36 ms 16228 KB Output is correct
16 Correct 54 ms 16168 KB Output is correct
17 Correct 36 ms 16192 KB Output is correct
18 Correct 37 ms 16160 KB Output is correct
19 Correct 38 ms 16172 KB Output is correct
20 Correct 40 ms 16064 KB Output is correct
21 Correct 32 ms 16080 KB Output is correct
22 Correct 34 ms 16092 KB Output is correct
23 Correct 37 ms 16212 KB Output is correct
24 Correct 36 ms 16136 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Incorrect 0 ms 340 KB Output isn't correct
27 Halted 0 ms 0 KB -