답안 #679809

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
679809 2023-01-09T09:47:46 Z nguyentunglam Swap (BOI16_swap) C++14
0 / 100
1 ms 340 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 1e5 + 10;
int a[N], pos[N], b[N], p[N], n;
int dfs(int u) {
    if (!b[u]) return u;
    int ret = n + 1;
    if (!p[u * 2]) ret = min(ret, dfs(u * 2));
    if (!p[u * 2 + 1]) ret = min(ret, dfs(u * 2 + 1));
    return ret;
}
int main() {
    #define task "swap"
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen ("task.inp", "r")) {
        freopen ("task.inp", "r", stdin);
        freopen ("task.out", "w", stdout);
    }
    if (fopen (task".inp", "r")) {
        freopen (task".inp", "r", stdin);
        freopen (task".out", "w", stdout);
    }
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i], pos[a[i]] = i;
    for(int i = 1; i <= n; i++) {
        int j = pos[i];
        if (j != 1 && !b[j / 2]) b[j / 2] = i;
        else if (!b[j]) b[j] = i;
        else {
            int Start = j == 1 ? j : j / 2;
            int k = dfs(Start);
            b[k] = i;
            int v = k;
            while (v != Start) p[v] = 1, v /= 2;
        }
    }
    for(int i = 1; i <= n; i++) cout << b[i] << " ";
}


Compilation message

swap.cpp: In function 'int main()':
swap.cpp:20:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen ("task.inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
swap.cpp:21:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen ("task.out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
swap.cpp:24:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
swap.cpp:25:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -