Submission #15528

# Submission time Handle Problem Language Result Execution time Memory
15528 2015-07-12T08:52:30 Z yukariko 분배 (kriii3_Q) C++
24 / 24
426 ms 4020 KB
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <list>

using namespace std;

int a[16];
bool visit[1 << 16];
int N,K;
list<int> p[17];

bool end;
int sel[1<<16];
void solve(int pos, int depth, int cnt, int sum)
{
	if(cnt == 0 && sum == 0)
	{
		sort(sel, sel+depth);
		for(int i=0; i < depth; i++)
		{
			cout << sel[i] << " ";
		}
		cout << "\n";
		end = true;
		return;
	}
	
	if(pos > N || cnt == 0 || sum < pos)
		return;
	
	if(p[pos].empty())
		return solve(pos+1, depth, cnt, sum);
	
	// no select
	solve(pos+1, depth, cnt, sum);
	
	if(end)
		return;
		
	// select
	//sel[depth] = p[pos].back();
	sel[depth] = pos;
	p[pos].pop_back();
	solve(pos, depth+1, cnt-1, sum - pos);
	if(!end)
		p[pos].push_back(sel[depth]);
}

int main()
{
	cin >> N >> K;
	
	a[0] = 0;
	for(int i=1; i <= 16; i++)
		a[i] = a[i-1] * 2 + (1 << (i-1));

	int one_put = a[N] / (1 << K);
	int n_put = 1 << (N-K);
	
	for(int i=0, n = 1 << N; i < n; i++)
	{
		int count = 0;
		for(int j = i; j; j >>= 1)
			count += j & 1;
			
		p[count].push_back(i);
	}

	for(int n = 1 << K; n--;)
	{
		int P=0,Q=0;
		for(int j=0,k=0; j <= N && k < n_put; j++)
		{
			if(p[j].empty() || p[N-j].empty())
				continue;
			if(j == N-j && p[j].size() < 2)
				continue;
			cout << p[j].back() << " ";
			p[j].pop_back();
			cout << p[N-j].back() << " ";
			p[N-j].pop_back();
			k+=2;
			P += N;
			j--;
		}
		cout << "\n";
	}
			
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2040 KB Output is correct
2 Correct 0 ms 2040 KB Output is correct
3 Correct 0 ms 2040 KB Output is correct
4 Correct 0 ms 2040 KB Output is correct
5 Correct 0 ms 2040 KB Output is correct
6 Correct 0 ms 2040 KB Output is correct
7 Correct 0 ms 2040 KB Output is correct
8 Correct 0 ms 2040 KB Output is correct
9 Correct 0 ms 2040 KB Output is correct
10 Correct 0 ms 2040 KB Output is correct
11 Correct 0 ms 2040 KB Output is correct
12 Correct 0 ms 2040 KB Output is correct
13 Correct 0 ms 2040 KB Output is correct
14 Correct 0 ms 2040 KB Output is correct
15 Correct 0 ms 2040 KB Output is correct
16 Correct 0 ms 2040 KB Output is correct
17 Correct 0 ms 2040 KB Output is correct
18 Correct 0 ms 2040 KB Output is correct
19 Correct 0 ms 2040 KB Output is correct
20 Correct 0 ms 2040 KB Output is correct
21 Correct 1 ms 2040 KB Output is correct
22 Correct 0 ms 2040 KB Output is correct
23 Correct 0 ms 2040 KB Output is correct
24 Correct 0 ms 2040 KB Output is correct
25 Correct 0 ms 2040 KB Output is correct
26 Correct 2 ms 2040 KB Output is correct
27 Correct 0 ms 2172 KB Output is correct
28 Correct 0 ms 2172 KB Output is correct
29 Correct 2 ms 2172 KB Output is correct
30 Correct 25 ms 2436 KB Output is correct
31 Correct 28 ms 2436 KB Output is correct
32 Correct 23 ms 2436 KB Output is correct
33 Correct 4 ms 2964 KB Output is correct
34 Correct 7 ms 2964 KB Output is correct
35 Correct 6 ms 2964 KB Output is correct
36 Correct 0 ms 2964 KB Output is correct
37 Correct 426 ms 4020 KB Output is correct
38 Correct 403 ms 4020 KB Output is correct
39 Correct 421 ms 4020 KB Output is correct
40 Correct 422 ms 4020 KB Output is correct