답안 #106266

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
106266 2019-04-17T17:47:07 Z eriksuenderhauf Swap (BOI16_swap) C++11
0 / 100
7 ms 4224 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;

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

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);
		bool f1 = 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);
		bool f2 = upd();
		if (!f2) 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 'void solve(int)':
swap.cpp:81:8: warning: unused variable 'f1' [-Wunused-variable]
   bool f1 = upd();
        ^~
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:96: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:100:3: note: in expansion of macro 'ni'
   ni(a[i]);
   ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4224 KB Output is correct
2 Correct 7 ms 4224 KB Output is correct
3 Incorrect 6 ms 4224 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4224 KB Output is correct
2 Correct 7 ms 4224 KB Output is correct
3 Incorrect 6 ms 4224 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4224 KB Output is correct
2 Correct 7 ms 4224 KB Output is correct
3 Incorrect 6 ms 4224 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4224 KB Output is correct
2 Correct 7 ms 4224 KB Output is correct
3 Incorrect 6 ms 4224 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4224 KB Output is correct
2 Correct 7 ms 4224 KB Output is correct
3 Incorrect 6 ms 4224 KB Output isn't correct
4 Halted 0 ms 0 KB -