답안 #127847

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
127847 2019-07-10T07:06:00 Z abacaba Zalmoxis (BOI18_zalmoxis) C++14
0 / 100
255 ms 75960 KB
#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;

vector <pnode> fr;
 
pnode root, last;
stack <pair <int, pnode> > st, temp;
 
void processnext() {
	while(last) {
	    int now = last->val;
	    while(!st.empty() && st.top().f == now) {
	        st.pop();
	        ++now;
	    }
	    while(k && !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 addelse() {
	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 checkend() {
	deque <pair <int, pnode> > s;
	while(!temp.empty()) {
	    st.push(temp.top());
	    s.push_back(temp.top());
	    temp.pop();
	}
	while(!s.empty() && s.front().f != 30) {
		if(s.front() < s.back()) {
	   		last = s.front().se;
	   		pnode x = new node(s.front().f);
		   	x->next = last;
		   	int now = x->val;
	    	if(x->next)
   	        	x->next->prev = x;
	       	fr.pb(x);
        	--k;
		    while(!s.empty() && s.front().f == now) {
		       	s.pop_front();
		   		++now;
	    	}
		}
		else {
			last = s.back().se;
	   		pnode x = new node(s.back().f);
		   	x->prev = last;
		   	int now = x->val;
	    	if(x->prev)
   	        	x->prev->next = x;
	       	fr.pb(x);
        	--k;
		    while(!s.empty() && s.back().f == now) {
		       	s.pop_back();
		   		++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;
	}
}

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();
	addelse();
	checkend();
	extend_fr();
	last = root;
	deque <int> cur;
	while(last) {
		cur.push_back(last->val);
		last = last->next;
	}
	if(0) {
		while(root) {
			printf("%d ", root->val);
			root = root->next;
		}
		return 0;
	}
	fr.clear();
	cur.clear();
	while(!temp.empty())
		temp.pop();
	while(!st.empty())
		st.pop();
	root = new node(a[n]);
	last = root;
	for(int i = n - 1; i; --i) {
		pnode x = new node(a[i]);
		x->next = last;
		last->prev = x;
		last = x;
	}
	last = root;
	processnext();
	addelse();
	checkend();
	extend_fr();
	while(last) {
		cur.push_front(last->val);
		last = last->next;
	}
	for(int i = 0; i < cur.size(); ++i)
		printf("%d ", cur[i]);
    return 0;
}

Compilation message

zalmoxis.cpp: In function 'void processnext()':
zalmoxis.cpp:52:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
             if(x->prev)
             ^~
zalmoxis.cpp:54: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:143:6: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
      if(a->prev)
      ^~
zalmoxis.cpp:145: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 'int main()':
zalmoxis.cpp:204:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < cur.size(); ++i)
                 ~~^~~~~~~~~~~~
zalmoxis.cpp:152: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:154: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 Incorrect 228 ms 67248 KB Unexpected end of file - int32 expected
2 Incorrect 251 ms 67504 KB Unexpected end of file - int32 expected
3 Incorrect 235 ms 68016 KB Unexpected end of file - int32 expected
4 Incorrect 233 ms 67632 KB Unexpected end of file - int32 expected
5 Incorrect 255 ms 75960 KB Unexpected end of file - int32 expected
6 Incorrect 229 ms 68016 KB Unexpected end of file - int32 expected
# 결과 실행 시간 메모리 Grader output
1 Incorrect 227 ms 66948 KB Unexpected end of file - int32 expected
2 Incorrect 235 ms 69116 KB Unexpected end of file - int32 expected
3 Incorrect 227 ms 66856 KB Unexpected end of file - int32 expected
4 Incorrect 241 ms 71388 KB Unexpected end of file - int32 expected
5 Incorrect 226 ms 66980 KB Unexpected end of file - int32 expected
6 Incorrect 230 ms 68016 KB Unexpected end of file - int32 expected
7 Incorrect 230 ms 68116 KB Unexpected end of file - int32 expected
8 Incorrect 226 ms 67024 KB Unexpected end of file - int32 expected
9 Incorrect 204 ms 63344 KB Unexpected end of file - int32 expected
10 Incorrect 171 ms 65568 KB Unexpected end of file - int32 expected
11 Incorrect 177 ms 61352 KB Unexpected end of file - int32 expected
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 376 KB Unexpected end of file - int32 expected