답안 #660858

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
660858 2022-11-23T10:53:08 Z Bagritsevich_Stepan DEL13 (info1cup18_del13) C++14
100 / 100
11 ms 3928 KB
#include <iostream>
#include <vector>

using namespace std;

const int inf = 1e8;
const int maxN = 2e5 + 5;

vector<int> result;
int dp[maxN][3];
int from[maxN][3];

void UpdateDp(int &value_to_update, int &from, const int i, const int flag, const int value) {
    const int newValue = dp[i][flag] + value;
    if (newValue < value_to_update) {
        value_to_update = newValue;
        from = flag;
    }
}

void UpdateResult(const int l, const int r, int count) {
    const int length = r - l + 1;
    if (l > r || length < 3) {
        return;
    }

    int i = l + 1;
    count = (length - count) / 2;
    while (count-- && i < r) {
        result.push_back(i);
        i += 2;
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;

    int a[maxN];
    int count[maxN];

    while (t--) {
        int n, q;
        cin >> n >> q;

        a[q + 1] = n + 1;
        for (int i = 1; i <= q; i++) {
            cin >> a[i];
        }

        for (int i = 0; i <= q + 1; i++) {
            for (int flag = 0; flag < 3; flag++) {
                dp[i][flag] = inf;
                from[i][flag] = -1;
            }
        }

        dp[0][0] = 0;
        for (int i = 0; i <= q; i++) {
            const int length = a[i + 1] - (a[i] + 1);
            for (int flag = 0; flag < 3; flag++) {
                if (flag >= length) {
                    if (flag == length) {
                        UpdateDp(dp[i + 1][0], from[i + 1][0], i, flag, 0);
                    }
                    continue;
                }

                const int operCount = (length - flag) / 2;
                if ((length - flag) % 2) {
                    UpdateDp(dp[i + 1][1], from[i + 1][1], i, flag, operCount + 1);
                } else {
                    UpdateDp(dp[i + 1][2], from[i + 1][2], i, flag, operCount + 1);
                    if (flag) {
                        UpdateDp(dp[i + 1][0], from[i + 1][0], i, flag, operCount);
                    }
                }
            }
        }

        if (dp[q + 1][0] == inf) {
            cout << "-1\n";
            continue;
        }

        count[q + 1] = 0;
        int index = from[q + 1][0];
        for (int i = q; i >= 0; i--) {
            count[i] = index;
            index = from[i][index];
        }

        for (int i = 0; i <= q; i++) {
            UpdateResult(a[i] + 1, a[i + 1] - 1, count[i] + count[i + 1]);
        }

        for (int i = 1; i <= q; i++) {
            for (int j = 0; j < count[i]; j++) {
                result.push_back(a[i]);
            }
        }

        cout << result.size() << '\n';
        for (int i = 0; i < result.size(); i++) {
            cout << result[i] << ' ';
        }
        cout << '\n';

        result.clear();
    }
    return 0;
}

Compilation message

del13.cpp: In function 'int main()':
del13.cpp:108:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |         for (int i = 0; i < result.size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1876 KB Output is correct
2 Correct 1 ms 1896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1876 KB Output is correct
2 Correct 1 ms 1896 KB Output is correct
3 Correct 4 ms 2004 KB Output is correct
4 Correct 4 ms 2004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2132 KB Output is correct
2 Correct 2 ms 1900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1876 KB Output is correct
2 Correct 1 ms 1896 KB Output is correct
3 Correct 4 ms 2004 KB Output is correct
4 Correct 4 ms 2004 KB Output is correct
5 Correct 1 ms 1876 KB Output is correct
6 Correct 1 ms 1904 KB Output is correct
7 Correct 2 ms 1876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1876 KB Output is correct
2 Correct 1 ms 1896 KB Output is correct
3 Correct 4 ms 2004 KB Output is correct
4 Correct 4 ms 2004 KB Output is correct
5 Correct 1 ms 1876 KB Output is correct
6 Correct 1 ms 1904 KB Output is correct
7 Correct 2 ms 1876 KB Output is correct
8 Correct 8 ms 2296 KB Output is correct
9 Correct 7 ms 2692 KB Output is correct
10 Correct 10 ms 2832 KB Output is correct
11 Correct 11 ms 3928 KB Output is correct