Submission #260055

# Submission time Handle Problem Language Result Execution time Memory
260055 2020-08-09T06:55:45 Z arnold518 Swap (BOI16_swap) C++14
68 / 100
844 ms 262148 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 2e5;

int N, A[MAXN+10];
bitset<18> B[MAXN+10];
vector<int> V[MAXN+10];

vector<bool> vis[MAXN+10];
vector<vector<bitset<18>>> dp[MAXN+10];

bitset<18> itob(int x)
{
	bitset<18> ret;
	for(int i=0; i<18; i++) if(x&(1<<i)) ret[i]=1;
	return ret;
}

int btoi(bitset<18> x)
{
	int ret=0;
	for(int i=0; i<18; i++) if(x[i]) ret|=(1<<i);
	return ret;
}

bool operator > (const bitset<18> &p, const bitset<18> &q)
{
	for(int i=17; i>=0; i--)
	{
		if(p[i]==q[i]) continue;
		if(p[i]) return true;
		if(q[i]) return false;
	}
	return false;
}

bool operator < (const bitset<18> &p, const bitset<18> &q)
{
	for(int i=17; i>=0; i--)
	{
		if(p[i]==q[i]) continue;
		if(p[i]) return false;
		if(q[i]) return true;
	}
	return false;
}

vector<bitset<18>> &solve(int now, int pp)
{
	int qq=lower_bound(V[now].begin(), V[now].end(), pp)-V[now].begin();
	if(V[now][qq]!=pp) while(1);
	if(vis[now][qq]) return dp[now][qq];
	vis[now][qq]=1;
	vector<bitset<18>> &ret=dp[now][qq];

	if(now*2>N)
	{
		ret.push_back(itob(A[pp]));
		return ret;
	}
	if(now*2+1>N)
	{
		if(A[pp]>A[now*2])
		{
			ret.push_back(itob(A[now*2]));
			ret.push_back(itob(A[pp]));
		}
		else
		{
			ret.push_back(itob(A[pp]));
			ret.push_back(itob(A[now*2]));
		}
		return ret;
	}

	if(A[pp]<A[now*2] && A[pp]<A[now*2+1])
	{
		vector<bitset<18>> &p=solve(now*2, now*2), &q=solve(now*2+1, now*2+1);
		ret.push_back(B[pp]);
		for(int k=0, i=0, j=0; i<p.size() || j<q.size(); k++)
		{
			for(int t=0; t<(1<<k) && i<p.size(); t++, i++) ret.push_back(p[i]);
			for(int t=0; t<(1<<k) && j<q.size(); t++, j++) ret.push_back(q[j]);
		}
		return ret;
	}
	if(A[now*2]<A[pp] && A[now*2]<A[now*2+1])
	{
		vector<bitset<18>> &p=solve(now*2, pp), &q=solve(now*2+1, now*2+1);
		ret.push_back(B[now*2]);
		for(int k=0, i=0, j=0; i<p.size() || j<q.size(); k++)
		{
			for(int t=0; t<(1<<k) && i<p.size(); t++, i++) ret.push_back(p[i]);
			for(int t=0; t<(1<<k) && j<q.size(); t++, j++) ret.push_back(q[j]);
		}
		return ret;
	}
	if(A[now*2+1]<A[pp] && A[now*2+1]<A[now*2])
	{
		vector<bitset<18>> &p1=solve(now*2, pp), &q1=solve(now*2+1, now*2);
		vector<bitset<18>> &p2=solve(now*2, now*2), &q2=solve(now*2+1, pp);
		ret.push_back(B[now*2+1]);
		int flag=0;
		for(int k=0, i=0, j=0; i<p1.size() || j<q1.size(); k++)
		{
			for(int t=0; t<(1<<k) && i<p1.size(); t++, i++)
			{
				if(flag==1) ret.push_back(p1[i]);
				if(flag==2) ret.push_back(p2[i]);
				if(flag==0)
				{
					if(p1[i]==p2[i]) ret.push_back(p1[i]), flag=0;
					if(p1[i]<p2[i]) ret.push_back(p1[i]), flag=1;
					if(p1[i]>p2[i]) ret.push_back(p2[i]), flag=2;
				}
			}
			for(int t=0; t<(1<<k) && j<q1.size(); t++, j++)
			{
				if(flag==1) ret.push_back(q1[j]);
				if(flag==2) ret.push_back(q2[j]);
				if(flag==0)
				{
					if(q1[j]==q2[j]) ret.push_back(q1[j]), flag=0;
					if(q1[j]<q2[j]) ret.push_back(q1[j]), flag=1;
					if(q1[j]>q2[j]) ret.push_back(q2[j]), flag=2;
				}
			}
		}
		return ret;
	}
}

int main()
{
	scanf("%d", &N);
	for(int i=1; i<=N; i++) scanf("%d", &A[i]);
	for(int i=1; i<=N; i++) B[i]=itob(A[i]);

	for(int i=1; i<=N; i++)
	{
		int now=i;
		while(now!=1)
		{
			V[i].push_back(now);
			if(now%2) V[i].push_back(now-1);
			now/=2;
		}
		V[i].push_back(1);
		reverse(V[i].begin(), V[i].end());
		vis[i].resize(V[i].size());
		dp[i].resize(V[i].size());
	}

	vector<bitset<18>> ans=solve(1, 1);
	for(auto it : ans) printf("%d ", btoi(it));
}

Compilation message

swap.cpp: In function 'std::vector<std::bitset<18> >& solve(int, int)':
swap.cpp:85:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int k=0, i=0, j=0; i<p.size() || j<q.size(); k++)
                          ~^~~~~~~~~
swap.cpp:85:41: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int k=0, i=0, j=0; i<p.size() || j<q.size(); k++)
                                        ~^~~~~~~~~
swap.cpp:87:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int t=0; t<(1<<k) && i<p.size(); t++, i++) ret.push_back(p[i]);
                             ~^~~~~~~~~
swap.cpp:88:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int t=0; t<(1<<k) && j<q.size(); t++, j++) ret.push_back(q[j]);
                             ~^~~~~~~~~
swap.cpp:96:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int k=0, i=0, j=0; i<p.size() || j<q.size(); k++)
                          ~^~~~~~~~~
swap.cpp:96:41: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int k=0, i=0, j=0; i<p.size() || j<q.size(); k++)
                                        ~^~~~~~~~~
swap.cpp:98:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int t=0; t<(1<<k) && i<p.size(); t++, i++) ret.push_back(p[i]);
                             ~^~~~~~~~~
swap.cpp:99:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int t=0; t<(1<<k) && j<q.size(); t++, j++) ret.push_back(q[j]);
                             ~^~~~~~~~~
swap.cpp:109:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int k=0, i=0, j=0; i<p1.size() || j<q1.size(); k++)
                          ~^~~~~~~~~~
swap.cpp:109:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int k=0, i=0, j=0; i<p1.size() || j<q1.size(); k++)
                                         ~^~~~~~~~~~
swap.cpp:111:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int t=0; t<(1<<k) && i<p1.size(); t++, i++)
                             ~^~~~~~~~~~
swap.cpp:122:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int t=0; t<(1<<k) && j<q1.size(); t++, j++)
                             ~^~~~~~~~~~
swap.cpp:136:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
swap.cpp: In function 'int main()':
swap.cpp:140:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
swap.cpp:141:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1; i<=N; i++) scanf("%d", &A[i]);
                          ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 17536 KB Output is correct
2 Correct 11 ms 17536 KB Output is correct
3 Correct 12 ms 17536 KB Output is correct
4 Correct 11 ms 17536 KB Output is correct
5 Correct 11 ms 17408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 17536 KB Output is correct
2 Correct 11 ms 17536 KB Output is correct
3 Correct 12 ms 17536 KB Output is correct
4 Correct 11 ms 17536 KB Output is correct
5 Correct 11 ms 17408 KB Output is correct
6 Correct 11 ms 17536 KB Output is correct
7 Correct 12 ms 17536 KB Output is correct
8 Correct 12 ms 17536 KB Output is correct
9 Correct 11 ms 17536 KB Output is correct
10 Correct 11 ms 17536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 17536 KB Output is correct
2 Correct 11 ms 17536 KB Output is correct
3 Correct 12 ms 17536 KB Output is correct
4 Correct 11 ms 17536 KB Output is correct
5 Correct 11 ms 17408 KB Output is correct
6 Correct 11 ms 17536 KB Output is correct
7 Correct 12 ms 17536 KB Output is correct
8 Correct 12 ms 17536 KB Output is correct
9 Correct 11 ms 17536 KB Output is correct
10 Correct 11 ms 17536 KB Output is correct
11 Correct 13 ms 18176 KB Output is correct
12 Correct 14 ms 18304 KB Output is correct
13 Correct 13 ms 18176 KB Output is correct
14 Correct 16 ms 18560 KB Output is correct
15 Correct 17 ms 18560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 17536 KB Output is correct
2 Correct 11 ms 17536 KB Output is correct
3 Correct 12 ms 17536 KB Output is correct
4 Correct 11 ms 17536 KB Output is correct
5 Correct 11 ms 17408 KB Output is correct
6 Correct 11 ms 17536 KB Output is correct
7 Correct 12 ms 17536 KB Output is correct
8 Correct 12 ms 17536 KB Output is correct
9 Correct 11 ms 17536 KB Output is correct
10 Correct 11 ms 17536 KB Output is correct
11 Correct 13 ms 18176 KB Output is correct
12 Correct 14 ms 18304 KB Output is correct
13 Correct 13 ms 18176 KB Output is correct
14 Correct 16 ms 18560 KB Output is correct
15 Correct 17 ms 18560 KB Output is correct
16 Correct 164 ms 66928 KB Output is correct
17 Correct 166 ms 66160 KB Output is correct
18 Correct 165 ms 66928 KB Output is correct
19 Correct 531 ms 114864 KB Output is correct
20 Correct 527 ms 114144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 17536 KB Output is correct
2 Correct 11 ms 17536 KB Output is correct
3 Correct 12 ms 17536 KB Output is correct
4 Correct 11 ms 17536 KB Output is correct
5 Correct 11 ms 17408 KB Output is correct
6 Correct 11 ms 17536 KB Output is correct
7 Correct 12 ms 17536 KB Output is correct
8 Correct 12 ms 17536 KB Output is correct
9 Correct 11 ms 17536 KB Output is correct
10 Correct 11 ms 17536 KB Output is correct
11 Correct 13 ms 18176 KB Output is correct
12 Correct 14 ms 18304 KB Output is correct
13 Correct 13 ms 18176 KB Output is correct
14 Correct 16 ms 18560 KB Output is correct
15 Correct 17 ms 18560 KB Output is correct
16 Correct 164 ms 66928 KB Output is correct
17 Correct 166 ms 66160 KB Output is correct
18 Correct 165 ms 66928 KB Output is correct
19 Correct 531 ms 114864 KB Output is correct
20 Correct 527 ms 114144 KB Output is correct
21 Correct 675 ms 236520 KB Output is correct
22 Correct 668 ms 237288 KB Output is correct
23 Correct 696 ms 238648 KB Output is correct
24 Runtime error 844 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
25 Halted 0 ms 0 KB -