-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path518E.cpp
More file actions
91 lines (91 loc) · 2.65 KB
/
518E.cpp
File metadata and controls
91 lines (91 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define FOR0(i,n) for(int i=0, _##i=(n); i<_##i; ++i)
#define FOR(i,l,r) for(int i=(l), _##i=(r); i<_##i; ++i)
#define FORD(i,l,r) for(int i=(r), _##i=(l); --i>=_##i; )
#define repi(i,a) for(__typeof((a).begin()) i=(a).begin(), _##i=(a).end(); i!=_##i; ++i)
#define dwni(i,a) for(__typeof((a).rbegin()) i=(a).rbegin(), _##i=(a).rend(); i!=_##i; ++i)
#define SZ(a) ((int)((a).size()))
#define printCase() "Case #" << caseNum << ": "
#define pb push_back
#define mp make_pair
#define EPS (1e-9)
#define PI 3.1415926535
#define inf ((int)2e9)
#define INF ((ll)9e18)
#define mod (1000000000 + 7)
#define newl '\n'
#define SYNC std::ios::sync_with_stdio(false); cin.tie(NULL);
#define ff first
#define ss second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
const int N = 1e5 + 5;
int k, n, a[N];
ll brute(int neg, int pos, int st) {
ll ret = 0;
if(neg >= 0 && st <= a[neg]) return INF;
for(int j = neg + k; j < pos; j+=k) {
ret += abs(st);
st++;
}
if(pos < n && st > a[pos]) ret = INF;
return ret;
}
int main() {
SYNC
string s;
cin >> n >> k;
FOR0(i, n) {
cin >> s;
if(s[0] == '?') {
a[i] = inf;
} else {
istringstream iss(s);
iss >> a[i];
}
}
int ans = 1;
FOR0(i, k) {
int neg = i - k, pos = i + k * ((n - i - 1) / k + 1);
for(int j = i; j < n; j+=k) {
if(a[j] != inf) {
if(a[j] <= 0) neg = j;
if(a[j] >= 0) pos = min(pos, j);
}
}
for(int j = neg-k; j >= i; j-=k) {
if(a[j] == inf) {
a[j] = a[j+k] - 1;
} else if(a[j] >= a[j+k]) ans = 0;
}
for(int j = pos+k; j < n; j+=k) {
if(a[j] == inf) {
a[j] = a[j-k] + 1;
} else if(a[j] <= a[j-k]) ans = 0;
}
int l = (pos - neg) / k - 1;
if(l < 0) continue;
set<pair<ll, int>> s;
s.insert(mp(brute(neg ,pos, -l/2), -l/2));
if(neg >= 0) s.insert(mp(brute(neg ,pos, a[neg] + 1), a[neg] + 1));
if(pos < n) s.insert(mp(brute(neg ,pos, a[pos] - l), a[pos] - l));
ll st;
if((*(s.begin())).ff < inf) st = (*(s.begin())).ss;
else ans = 0;
for(int j = neg + k; j < pos; j+=k) {
a[j] = st++;
}
}
if(!ans) {
cout << "Incorrect sequence";
} else {
FOR0(i, n) cout << a[i] << " ";
}
}