답안 #198858

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
198858 2020-01-27T22:11:03 Z Bagritsevich_Stepan DEL13 (info1cup18_del13) C++14
100 / 100
22 ms 2420 KB
//#include "/Users/stdc++.h"
#include <bits/stdc++.h>
 
using namespace std;
 
#define mp make_pair
#define pb push_back
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pii pair < int, int >
#define fs first
#define sc second
#define getfiles ifstream cin("input.txt"); ofstream cout("output.txt");
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
 
typedef long long ll;
typedef long double ld;
 
const int INF = 1e8;
const ll BIG_INF = 1e18;
const ll MOD = 1e9 + 7;
 
const int maxn = 2e5 + 5;
int a[maxn], dp[maxn][3], from[maxn][3], cnt[maxn];
vector < int > ans;
 
void upd(int &val_to_upd, int &from, int i, int flag, int val) {
    int new_val = dp[i][flag] + val;
    
    if (new_val < val_to_upd) {
        val_to_upd = new_val;
        from = flag;
    }
}
 
void upd_ans(int l, int r, int cnt) {
    int len = r - l + 1;
    if (l > r || len < 3) return;
    
    int i = l + 1;
    cnt = (len - cnt) / 2;
    
    while (cnt-- && i < r) {
        ans.pb(i);
        i += 2;
    }
}
 
int main() {
    fast_io
    int t;
    cin >> t;
    
    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++) {
            int len = a[i + 1] - (a[i] + 1);
            for (int flag = 0; flag < 3; flag++) {
                if (flag >= len) {
                    if (flag == len) upd(dp[i + 1][0], from[i + 1][0], i, flag, 0);
                    continue;
                }
                
                int cnt_oper = (len - flag) / 2;
                if ((len - flag) % 2)
                    upd(dp[i + 1][1], from[i + 1][1], i, flag, cnt_oper + 1);
                else {
                    upd(dp[i + 1][2], from[i + 1][2], i, flag, cnt_oper + 1);
                    if (flag) upd(dp[i + 1][0], from[i + 1][0], i, flag, cnt_oper);
                }
            }
        }
        
        if (dp[q + 1][0] == INF) {
            cout << "-1\n";
            continue;
        }
        
        cnt[q + 1] = 0;
        int ind = from[q + 1][0];
        for (int i = q; i >= 0; i--) {
            cnt[i] = ind;
            ind = from[i][ind];
        }
        
        for (int i = 0; i <= q; i++)
            upd_ans(a[i] + 1, a[i + 1] - 1, cnt[i] + cnt[i + 1]);
        for (int i = 1; i <= q; i++)
            for (int j = 0; j < cnt[i]; j++)
                ans.pb(a[i]);
        
        cout << sz(ans) << "\n";
        for (int i = 0; i < sz(ans); i++)
            cout << ans[i] << " ";
        cout << "\n";
        
        ans.clear();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 9 ms 376 KB Output is correct
4 Correct 9 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 504 KB Output is correct
2 Correct 6 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 9 ms 376 KB Output is correct
4 Correct 9 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 6 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 9 ms 376 KB Output is correct
4 Correct 9 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 6 ms 376 KB Output is correct
7 Correct 5 ms 376 KB Output is correct
8 Correct 13 ms 632 KB Output is correct
9 Correct 14 ms 1144 KB Output is correct
10 Correct 13 ms 1144 KB Output is correct
11 Correct 22 ms 2420 KB Output is correct