#include <bits/stdc++.h>
using namespace std;
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define uset unordered_set
#define umap unordered_map
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>
const int inf = 2e9;
const int N = 1e6 + 15;
int n, k, a[N];
struct node {
node *prev, *next;
int val;
node(int val) : val(val) {
prev = next = NULL;
}
};
typedef node* pnode;
map <int, vector <pnode> > is;
vector <pnode> fr;
pnode root, last;
stack <pair <int, pnode> > st, temp;
void processnext() {
while(last && k) {
int now = last->val;
while(!st.empty() && st.top().f == now) {
st.pop();
++now;
}
while(!st.empty() && st.top().f < now) {
pnode x = new node(st.top().f);
x->prev = st.top().se;
x->next = st.top().se->next;
if(x->next)
x->next->prev = x;
if(x->prev)
x->prev->next = x;
fr.pb(x);
int curval = x->val;
while(!st.empty() && st.top().f == curval)
st.pop(), ++curval;
st.push(mp(curval, x));
--k;
}
while(!st.empty() && st.top().f == now) {
st.pop();
++now;
}
st.push(mp(now, last));
last = last->next;
}
}
void addelsenext() {
while(!st.empty()) {
int now = st.top().f;
temp.push(st.top());
st.pop();
while(!st.empty() && st.top().f > now + 1) {
int need = st.top().f - 1;
pnode x = new node(need);
x->prev = st.top().se;
x->next = st.top().se->next;
if(x->next)
x->next->prev = x;
if(x->prev)
x->prev->next = x;
fr.pb(x);
st.push(mp(need, x));
--k;
}
}
}
void addelseback() {
while(!st.empty()) {
int now = st.top().f;
temp.push(st.top());
st.pop();
while(!st.empty() && st.top().f > now + 1) {
int need = st.top().f - 1;
pnode x = new node(need);
x->next = st.top().se;
x->prev = st.top().se->prev;
if(x->next)
x->next->prev = x;
if(x->prev)
x->prev->next = x;
fr.pb(x);
st.push(mp(need, x));
--k;
}
}
}
void checkend() {
while(!temp.empty()) {
st.push(temp.top());
temp.pop();
}
while(!st.empty() && st.top().f != 30) {
last = st.top().se;
pnode x = new node(st.top().f);
x->next = last;
x->prev = last->prev;
if(x->prev)
x->prev->next = x;
if(x->next)
x->next->prev = x;
fr.pb(x);
--k;
int now = x->val;
while(!st.empty() && st.top().f == now) {
st.pop();
++now;
}
}
}
void extend_fr() {
while(!fr.empty() && k) {
pnode x = fr.back();
fr.ppb();
if(x->val <= 1)
continue;
pnode a = new node(x->val - 1), b = new node(x->val - 1);
a->prev = x->prev, a->next = b;
b->prev = a, b->next = x->next;
if(b->next)
b->next->prev = b;
if(a->prev)
a->prev->next = a;
fr.pb(a);
fr.pb(b);
--k;
}
}
void processback() {
while(last && k) {
int now = last->val;
while(!st.empty() && st.top().f == now) {
st.pop();
++now;
}
while(!st.empty() && st.top().f < now) {
pnode x = new node(st.top().f);
x->prev = st.top().se;
x->next = st.top().se->next;
if(x->next)
x->next->prev = x;
if(x->prev)
x->prev->next = x;
fr.pb(x);
int curval = x->val;
while(!st.empty() && st.top().f == curval)
st.pop(), ++curval;
st.push(mp(curval, x));
--k;
}
while(!st.empty() && st.top().f == now) {
st.pop();
++now;
}
st.push(mp(now, last));
last = last->prev;
}
}
int main() {
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
root = new node(a[1]);
last = root;
for(int i = 2; i <= n; ++i) {
pnode x = new node(a[i]);
x->prev = last;
last->next = x;
last = x;
}
last = root;
processnext();
addelsenext();
while(!temp.empty())
temp.pop();
pnode go = root;
while(go) {
last = go;
go = go->next;
}
processback();
addelseback();
checkend();
extend_fr();
while(root) {
printf("%d ", root->val);
root = root->next;
}
return 0;
}
Compilation message
zalmoxis.cpp: In function 'void processnext()':
zalmoxis.cpp:53:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(x->prev)
^~
zalmoxis.cpp:55:10: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
fr.pb(x);
^~
zalmoxis.cpp: In function 'void extend_fr()':
zalmoxis.cpp:148:6: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(a->prev)
^~
zalmoxis.cpp:150:3: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
fr.pb(a);
^~
zalmoxis.cpp: In function 'void processback()':
zalmoxis.cpp:169:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if(x->prev)
^~
zalmoxis.cpp:171:10: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
fr.pb(x);
^~
zalmoxis.cpp: In function 'int main()':
zalmoxis.cpp:188:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~
zalmoxis.cpp:190:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
588 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Incorrect |
302 ms |
52552 KB |
Expected EOF |
3 |
Incorrect |
290 ms |
48196 KB |
Expected EOF |
4 |
Incorrect |
258 ms |
39160 KB |
Expected EOF |
5 |
Incorrect |
615 ms |
147916 KB |
Expected EOF |
6 |
Incorrect |
320 ms |
58744 KB |
Expected EOF |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
275 ms |
37576 KB |
Output is correct |
2 |
Runtime error |
606 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Correct |
274 ms |
37628 KB |
Output is correct |
4 |
Correct |
281 ms |
37828 KB |
Output is correct |
5 |
Incorrect |
275 ms |
37728 KB |
not a zalsequence |
6 |
Correct |
275 ms |
37624 KB |
Output is correct |
7 |
Correct |
282 ms |
37800 KB |
Output is correct |
8 |
Correct |
277 ms |
37860 KB |
Output is correct |
9 |
Correct |
273 ms |
40040 KB |
Output is correct |
10 |
Incorrect |
179 ms |
52660 KB |
Unexpected end of file - int32 expected |
11 |
Correct |
249 ms |
47588 KB |
Output is correct |
12 |
Incorrect |
2 ms |
376 KB |
Unexpected end of file - int32 expected |
13 |
Incorrect |
2 ms |
376 KB |
Unexpected end of file - int32 expected |
14 |
Incorrect |
2 ms |
256 KB |
Unexpected end of file - int32 expected |