답안 #146700

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
146700 2019-08-25T11:21:07 Z evpipis Swap (BOI16_swap) C++
100 / 100
80 ms 12284 KB
#include <bits/stdc++.h>
using namespace std;

//#define TEST
#define pb push_back
const int len = 2e5+5;
int val[len], block[len];
vector<int> vec[len];

int fin(int i){
    if (!i) return len;

    int ans = len;
    if (!vec[i].empty())
        ans = vec[i].back();

    if (!block[i])
        ans = min(ans, fin(i/2));
    return ans;
}

void del(int i, int v){
    if (!i) return;

    if (!vec[i].empty() && vec[i].back() == v)
        vec[i].pop_back();
    else
        block[i] = 1, del(i/2, v);
}

int out[len];

void upd(int n){
    int pos = 1;
    while (pos <= n && out[pos] == val[pos])
        pos++;

    if (pos <= n && val[pos] < out[pos])
        for (int i = 1; i <= n; i++)
            out[i] = val[i];
}

void solve(int n){
    for (int i = 1; i <= n; i++)
        out[i] = n;
    for (int bit = 0; bit < (1<<(n-1)); bit++){
        for (int j = 2; j <= n; j++)
            if (bit&(1<<(j-2)))
                swap(val[j/2], val[j]);

        upd(n);

        for (int j = n; j >= 2; j--)
            if (bit&(1<<(j-2)))
                swap(val[j/2], val[j]);
    }

    #ifndef TEST
    printf("brute: ");
    for (int i = 1; i <= n; i++)
        printf("%d ", out[i]);
    printf("\n");
    #endif
}

int main(){
    #ifndef TEST
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &val[i]);
    #endif

    #ifdef TEST
    int rem = 1000;
    while (rem--){
    printf("rem = %d\n", rem);
    int n = rand()%25+1;
    for (int i = 1; i <= n; i++)
        val[i] = i, block[i] = 0;
    random_shuffle(val+1, val+1+n);

    for (int i = 1; i <= n; i++)
        printf("%d ", val[i]);
    printf("\n");
    #endif

    //solve(n);

    for (int i = 1; i <= n; i++){
        //printf("i = %d, tp = %d\n", i, val[i]);
        if (val[i] >= 1){
            block[i] = 1;

            int mn = i;
            if (2*i <= n && val[2*i] <= val[mn])
                mn = 2*i;
            if (2*i+1 <= n && val[2*i+1] <= val[mn])
                mn = 2*i+1;

            //printf("mn = %d with %d\n", mn, val[mn]);

            if (mn == 2*i){
                swap(val[i], val[2*i]);
            }
            if (mn == 2*i+1){
                swap(val[i], val[2*i+1]);
                vec[i].pb(val[2*i]);
                vec[i].pb(val[2*i+1]);
                if (vec[i][0] < vec[i][1])
                    swap(vec[i][0], vec[i][1]);

                val[2*i] = 0;
                val[2*i+1] = 0;
            }
        }
        else{
            val[i] = fin(i);
            int mn = i;
            if (2*i <= n && val[2*i] <= val[mn])
                mn = 2*i;
            if (2*i+1 <= n && val[2*i+1] <= val[mn])
                mn = 2*i+1;

            //printf("mn = %d with %d\n", mn, val[mn]);

            if (mn == i){
                del(i, val[i]);
            }
            if (mn == 2*i){
                val[i] = 0;
                swap(val[i], val[2*i]);
            }
            if (mn == 2*i+1){
                swap(val[i], val[2*i+1]);
                vec[i].pb(val[2*i]);
                val[2*i] = val[2*i+1] = 0;
            }
        }

        //printf("val = %d, vec =", val[i]);
        //for (int j = 0; j < vec[i].size(); j++)
          //  printf(" %d", vec[i][j]);
        //printf("\n\n");
    }

    #ifdef TEST
    int same = 1;
    for (int i = 1; i <= n; i++)
        if (val[i] != out[i])
            same = 0;

    if (!same){
        printf("bru: ");
        for (int i = 1; i <= n; i++)
            printf("%d ", out[i]);
        printf("\n");

        printf("opt: ");
        for (int i = 1; i <= n; i++)
            printf("%d ", val[i]);
        printf("\n");
        return 0;
    }
    }

    printf("its correct\n");
    #endif

    #ifndef TEST
    //printf("optim: ");
    for (int i = 1; i <= n; i++)
        printf("%d ", val[i]);
    printf("\n");
    #endif
    return 0;
}
/*
9
7 8 6 2 3 9 5 1 4

6
5 4 1 6 2 3

5
4 1 5 3 2

19
11 15 2 3 4 7 6 18 10 17 16 12 14 8 9 19 1 5 13

18
4 15 2 17 10 12 8 11 18 6 9 16 7 14 3 13 1 5

24
22 20 3 7 14 13 19 2 15 1 6 12 17 21 10 5 16 23 9 4 11 24 18 8
*/

Compilation message

swap.cpp: In function 'int main()':
swap.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
swap.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &val[i]);
         ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5112 KB Output is correct
5 Correct 6 ms 4984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5112 KB Output is correct
5 Correct 6 ms 4984 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 5104 KB Output is correct
8 Correct 8 ms 4984 KB Output is correct
9 Correct 6 ms 4984 KB Output is correct
10 Correct 6 ms 5288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5112 KB Output is correct
5 Correct 6 ms 4984 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 5104 KB Output is correct
8 Correct 8 ms 4984 KB Output is correct
9 Correct 6 ms 4984 KB Output is correct
10 Correct 6 ms 5288 KB Output is correct
11 Correct 7 ms 4984 KB Output is correct
12 Correct 6 ms 4984 KB Output is correct
13 Correct 7 ms 4988 KB Output is correct
14 Correct 7 ms 5112 KB Output is correct
15 Correct 7 ms 4984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5112 KB Output is correct
5 Correct 6 ms 4984 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 5104 KB Output is correct
8 Correct 8 ms 4984 KB Output is correct
9 Correct 6 ms 4984 KB Output is correct
10 Correct 6 ms 5288 KB Output is correct
11 Correct 7 ms 4984 KB Output is correct
12 Correct 6 ms 4984 KB Output is correct
13 Correct 7 ms 4988 KB Output is correct
14 Correct 7 ms 5112 KB Output is correct
15 Correct 7 ms 4984 KB Output is correct
16 Correct 21 ms 6264 KB Output is correct
17 Correct 22 ms 6364 KB Output is correct
18 Correct 21 ms 6308 KB Output is correct
19 Correct 24 ms 6776 KB Output is correct
20 Correct 25 ms 6776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5112 KB Output is correct
3 Correct 6 ms 5112 KB Output is correct
4 Correct 7 ms 5112 KB Output is correct
5 Correct 6 ms 4984 KB Output is correct
6 Correct 6 ms 5112 KB Output is correct
7 Correct 6 ms 5104 KB Output is correct
8 Correct 8 ms 4984 KB Output is correct
9 Correct 6 ms 4984 KB Output is correct
10 Correct 6 ms 5288 KB Output is correct
11 Correct 7 ms 4984 KB Output is correct
12 Correct 6 ms 4984 KB Output is correct
13 Correct 7 ms 4988 KB Output is correct
14 Correct 7 ms 5112 KB Output is correct
15 Correct 7 ms 4984 KB Output is correct
16 Correct 21 ms 6264 KB Output is correct
17 Correct 22 ms 6364 KB Output is correct
18 Correct 21 ms 6308 KB Output is correct
19 Correct 24 ms 6776 KB Output is correct
20 Correct 25 ms 6776 KB Output is correct
21 Correct 68 ms 10688 KB Output is correct
22 Correct 68 ms 10488 KB Output is correct
23 Correct 68 ms 10360 KB Output is correct
24 Correct 80 ms 12284 KB Output is correct
25 Correct 79 ms 12280 KB Output is correct