답안 #260041

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
260041 2020-08-09T06:02:37 Z arnold518 Swap (BOI16_swap) C++14
68 / 100
850 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];

unordered_map<int, bool> vis[MAXN+10];
unordered_map<int, vector<pii>> dp[MAXN+10];

vector<pii> &solve(int now, int pp)
{
	if(vis[now][pp]) return dp[now][pp];
	vis[now][pp]=1;
	vector<pii> &ret=dp[now][pp];

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

	if(A[pp]<A[now*2] && A[pp]<A[now*2+1])
	{
		vector<pii> &p=solve(now*2, now*2), &q=solve(now*2+1, now*2+1);
		ret.resize(p.size()+q.size()+1);
		ret[0]={now, A[pp]};
		merge(p.begin(), p.end(), q.begin(), q.end(), ret.begin()+1);
		return ret;
	}
	if(A[now*2]<A[pp] && A[now*2]<A[now*2+1])
	{
		vector<pii> &p=solve(now*2, pp), &q=solve(now*2+1, now*2+1);
		ret.resize(p.size()+q.size()+1);
		ret[0]={now, A[now*2]};
		merge(p.begin(), p.end(), q.begin(), q.end(), ret.begin()+1);
		return ret;
	}
	if(A[now*2+1]<A[pp] && A[now*2+1]<A[now*2])
	{
		vector<pii> ret1, ret2;
		vector<pii> &p1=solve(now*2, pp), &q1=solve(now*2+1, now*2);
		ret1.resize(p1.size()+q1.size()+1);
		ret1[0]={now, A[now*2+1]};
		merge(p1.begin(), p1.end(), q1.begin(), q1.end(), ret1.begin()+1);

		vector<pii> &p2=solve(now*2, now*2), &q2=solve(now*2+1, pp);
		ret2.resize(p2.size()+q2.size()+1);
		ret2[0]={now, A[now*2+1]};
		merge(p2.begin(), p2.end(), q2.begin(), q2.end(), ret2.begin()+1);
		ret=min(ret1, ret2);
		return ret;
	}
}

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

	vector<pii> ans=solve(1, 1);
	for(auto it : ans) printf("%d ", it.second);
}

Compilation message

swap.cpp: In function 'std::vector<std::pair<int, int> >& solve(int, int)':
swap.cpp:72:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
swap.cpp: In function 'int main()':
swap.cpp:76:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
swap.cpp:77: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]);
                          ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 22272 KB Output is correct
2 Correct 15 ms 22272 KB Output is correct
3 Correct 14 ms 22272 KB Output is correct
4 Correct 15 ms 22272 KB Output is correct
5 Correct 15 ms 22400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 22272 KB Output is correct
2 Correct 15 ms 22272 KB Output is correct
3 Correct 14 ms 22272 KB Output is correct
4 Correct 15 ms 22272 KB Output is correct
5 Correct 15 ms 22400 KB Output is correct
6 Correct 15 ms 22272 KB Output is correct
7 Correct 15 ms 22272 KB Output is correct
8 Correct 15 ms 22272 KB Output is correct
9 Correct 15 ms 22272 KB Output is correct
10 Correct 17 ms 22272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 22272 KB Output is correct
2 Correct 15 ms 22272 KB Output is correct
3 Correct 14 ms 22272 KB Output is correct
4 Correct 15 ms 22272 KB Output is correct
5 Correct 15 ms 22400 KB Output is correct
6 Correct 15 ms 22272 KB Output is correct
7 Correct 15 ms 22272 KB Output is correct
8 Correct 15 ms 22272 KB Output is correct
9 Correct 15 ms 22272 KB Output is correct
10 Correct 17 ms 22272 KB Output is correct
11 Correct 16 ms 22656 KB Output is correct
12 Correct 20 ms 22912 KB Output is correct
13 Correct 16 ms 22656 KB Output is correct
14 Correct 23 ms 23672 KB Output is correct
15 Correct 21 ms 23680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 22272 KB Output is correct
2 Correct 15 ms 22272 KB Output is correct
3 Correct 14 ms 22272 KB Output is correct
4 Correct 15 ms 22272 KB Output is correct
5 Correct 15 ms 22400 KB Output is correct
6 Correct 15 ms 22272 KB Output is correct
7 Correct 15 ms 22272 KB Output is correct
8 Correct 15 ms 22272 KB Output is correct
9 Correct 15 ms 22272 KB Output is correct
10 Correct 17 ms 22272 KB Output is correct
11 Correct 16 ms 22656 KB Output is correct
12 Correct 20 ms 22912 KB Output is correct
13 Correct 16 ms 22656 KB Output is correct
14 Correct 23 ms 23672 KB Output is correct
15 Correct 21 ms 23680 KB Output is correct
16 Correct 150 ms 49896 KB Output is correct
17 Correct 141 ms 49396 KB Output is correct
18 Correct 149 ms 50160 KB Output is correct
19 Correct 766 ms 150888 KB Output is correct
20 Correct 743 ms 149868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 22272 KB Output is correct
2 Correct 15 ms 22272 KB Output is correct
3 Correct 14 ms 22272 KB Output is correct
4 Correct 15 ms 22272 KB Output is correct
5 Correct 15 ms 22400 KB Output is correct
6 Correct 15 ms 22272 KB Output is correct
7 Correct 15 ms 22272 KB Output is correct
8 Correct 15 ms 22272 KB Output is correct
9 Correct 15 ms 22272 KB Output is correct
10 Correct 17 ms 22272 KB Output is correct
11 Correct 16 ms 22656 KB Output is correct
12 Correct 20 ms 22912 KB Output is correct
13 Correct 16 ms 22656 KB Output is correct
14 Correct 23 ms 23672 KB Output is correct
15 Correct 21 ms 23680 KB Output is correct
16 Correct 150 ms 49896 KB Output is correct
17 Correct 141 ms 49396 KB Output is correct
18 Correct 149 ms 50160 KB Output is correct
19 Correct 766 ms 150888 KB Output is correct
20 Correct 743 ms 149868 KB Output is correct
21 Correct 605 ms 142116 KB Output is correct
22 Correct 579 ms 142132 KB Output is correct
23 Correct 591 ms 143776 KB Output is correct
24 Runtime error 850 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
25 Halted 0 ms 0 KB -