Submission #106271

# Submission time Handle Problem Language Result Execution time Memory
106271 2019-04-17T17:55:50 Z eriksuenderhauf Swap (BOI16_swap) C++11
48 / 100
1000 ms 8076 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 2e5 + 5;
const double eps = 1e-9;
int a[MAXN*4], ans[MAXN];
int n;

void upd() {
	bool fl = 0;
	for (int i = 1; i <= n; i++)
		if (a[i] > ans[i]) return;
		else if (a[i] < ans[i]) {
			fl = 1;
			break;
		}
	if (!fl) return;
	for (int i = 1; i <= n; i++) ans[i] = a[i];
	return;
}

bool check(vi &x, vi &y) {
	for (int i = 1; i <= n; i++)
		if (x[i] > y[i]) return 0;
		else if (x[i] < y[i]) return 1;
	return 0;
}

void solve(int k) {
	if (a[2*k]==-1&&a[2*k+1]==-1)
		return;
	if (a[2*k+1]==-1) {
		if (a[k] > a[2*k]) swap(a[k], a[2*k]);
		return;
	}
	if (a[2*k] == -1) {
		if (a[k] > a[2*k+1]) swap(a[k], a[2*k+1]);
		return;
	}
	int mi = min(a[k], min(a[k*2],a[k*2+1]));
	if (mi == a[k]) {
		solve(k*2);
		solve(k*2+1);
		upd();
	} else if (mi == a[k*2]) {
		swap(a[k], a[k*2]);
		solve(k*2);
		solve(k*2+1);
		upd();
	} else {
		swap(a[k],a[k*2+1]);
		vi tmp, tmp2;
		tmp.resize(n+1);
		tmp2.resize(n+1);
		for (int i = 1; i <= n; i++)
			tmp[i] = a[i];
		solve(k*2);
		solve(k*2+1);
		upd();
		for (int i = 1; i <= n; i++) {
			tmp2[i] = a[i];
			a[i] = tmp[i];
		}
		swap(a[k*2],a[k*2+1]);
		solve(k*2);
		solve(k*2+1);
		upd();
		for (int i = 1; i <= n; i++)
			tmp[i] = a[i];
		if (check(tmp2, tmp)) for (int i = 1; i <= n; i++)
			a[i] = tmp2[i];
	}
}

int main() {
	ni(n);
	memset(a, -1, sizeof a);
	memset(ans, 0x3f, sizeof ans);
	for (int i = 1; i <= n; i++)
		ni(a[i]);
	solve(1);
	for (int i = 1; i <= n; i++)
		printf("%d ", ans[i]);
	enl;
}

Compilation message

swap.cpp: In function 'int main()':
swap.cpp:7:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define ni(n) scanf("%d", &(n))
               ~~~~~^~~~~~~~~~~~
swap.cpp:105:2: note: in expansion of macro 'ni'
  ni(n);
  ^~
swap.cpp:7:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define ni(n) scanf("%d", &(n))
               ~~~~~^~~~~~~~~~~~
swap.cpp:109:3: note: in expansion of macro 'ni'
   ni(a[i]);
   ^~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4224 KB Output is correct
2 Correct 6 ms 4224 KB Output is correct
3 Correct 5 ms 4224 KB Output is correct
4 Correct 5 ms 4224 KB Output is correct
5 Correct 5 ms 4224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4224 KB Output is correct
2 Correct 6 ms 4224 KB Output is correct
3 Correct 5 ms 4224 KB Output is correct
4 Correct 5 ms 4224 KB Output is correct
5 Correct 5 ms 4224 KB Output is correct
6 Correct 7 ms 4224 KB Output is correct
7 Correct 5 ms 4224 KB Output is correct
8 Correct 5 ms 4196 KB Output is correct
9 Correct 6 ms 4224 KB Output is correct
10 Correct 5 ms 4224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4224 KB Output is correct
2 Correct 6 ms 4224 KB Output is correct
3 Correct 5 ms 4224 KB Output is correct
4 Correct 5 ms 4224 KB Output is correct
5 Correct 5 ms 4224 KB Output is correct
6 Correct 7 ms 4224 KB Output is correct
7 Correct 5 ms 4224 KB Output is correct
8 Correct 5 ms 4196 KB Output is correct
9 Correct 6 ms 4224 KB Output is correct
10 Correct 5 ms 4224 KB Output is correct
11 Correct 12 ms 4352 KB Output is correct
12 Correct 37 ms 4352 KB Output is correct
13 Correct 12 ms 4224 KB Output is correct
14 Correct 314 ms 4352 KB Output is correct
15 Correct 322 ms 4324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4224 KB Output is correct
2 Correct 6 ms 4224 KB Output is correct
3 Correct 5 ms 4224 KB Output is correct
4 Correct 5 ms 4224 KB Output is correct
5 Correct 5 ms 4224 KB Output is correct
6 Correct 7 ms 4224 KB Output is correct
7 Correct 5 ms 4224 KB Output is correct
8 Correct 5 ms 4196 KB Output is correct
9 Correct 6 ms 4224 KB Output is correct
10 Correct 5 ms 4224 KB Output is correct
11 Correct 12 ms 4352 KB Output is correct
12 Correct 37 ms 4352 KB Output is correct
13 Correct 12 ms 4224 KB Output is correct
14 Correct 314 ms 4352 KB Output is correct
15 Correct 322 ms 4324 KB Output is correct
16 Execution timed out 1053 ms 8076 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4224 KB Output is correct
2 Correct 6 ms 4224 KB Output is correct
3 Correct 5 ms 4224 KB Output is correct
4 Correct 5 ms 4224 KB Output is correct
5 Correct 5 ms 4224 KB Output is correct
6 Correct 7 ms 4224 KB Output is correct
7 Correct 5 ms 4224 KB Output is correct
8 Correct 5 ms 4196 KB Output is correct
9 Correct 6 ms 4224 KB Output is correct
10 Correct 5 ms 4224 KB Output is correct
11 Correct 12 ms 4352 KB Output is correct
12 Correct 37 ms 4352 KB Output is correct
13 Correct 12 ms 4224 KB Output is correct
14 Correct 314 ms 4352 KB Output is correct
15 Correct 322 ms 4324 KB Output is correct
16 Execution timed out 1053 ms 8076 KB Time limit exceeded
17 Halted 0 ms 0 KB -