답안 #1030016

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1030016 2024-07-21T16:08:59 Z MMihalev Swap (BOI16_swap) C++14
68 / 100
359 ms 262144 KB
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<tuple>
#include<set>
#include<map>
#include<random>
#include<chrono>
#include<array>
#include<unordered_map>
using namespace std;
const int MAX_N=2e5+5;
const int LOG=18;
vector<int>dp[MAX_N][LOG][2],tmp1,tmp2;
bool need[MAX_N][LOG][2];
int a[MAX_N];
int n;
void Merge(vector<int>& res,vector<int>& Left,vector<int>& Right)
{
    int nums=1;
    int idxL=0;
    int idxR=0;
    while(idxL<Left.size() or idxR<Right.size())
    {
        int cur=nums;
        while(idxL<Left.size() && cur--)res.push_back(Left[idxL++]);
        cur=nums;
        while(idxR<Right.size() && cur--)res.push_back(Right[idxR++]);
        nums*=2;
    }
}
signed main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }

    a[n+1]=1e9;

    need[1][0][1]=1;
    for(int node=1;node<=n/2;node++)
    {
        for(int i=0;node>>i;i++)
        {
            for(int j:{0,1})
            {
                if(!need[node][i][j])continue;

                int idx=((node>>i+1)<<1)+j;
                int mn=min(a[2*node+1],min(a[idx],a[2*node]));
                if(a[idx]==mn)
                {
                    need[2*node][0][0]=1;
                    need[2*node+1][0][1]=1;
                }
                else if(a[2*node]==mn)
                {
                    need[2*node][i+1][j]=1;
                    need[2*node+1][0][1]=1;
                }
                else
                {
                    need[2*node][i+1][j]=1;
                    need[2*node+1][0][0]=1;

                    need[2*node][0][0]=1;
                    need[2*node+1][i+1][j]=1;
                }
            }
        }
    }


    for(int node=n;node>=1;node--)
    {
        for(int i=0;node>>i;i++)
        {
            for(int j:{0,1})
            {
                if(!need[node][i][j])continue;

                int idx=((node>>i+1)<<1)+j;

                if(2*node>n)
                {
                    dp[node][i][j].push_back(a[idx]);
                }
                else if(2*node==n)
                {
                    dp[node][i][j].push_back(min(a[idx],a[2*node]));
                    dp[node][i][j].push_back(max(a[idx],a[2*node]));
                }
                else
                {
                    if(a[idx]<min(a[2*node],a[2*node+1]))
                    {
                        dp[node][i][j].push_back(a[idx]);
                        Merge(dp[node][i][j],dp[2*node][0][0],dp[2*node+1][0][1]);
                    }
                    else if(a[2*node]<min(a[idx],a[2*node+1]))
                    {
                        dp[node][i][j].push_back(a[2*node]);
                        Merge(dp[node][i][j],dp[2*node][i+1][j],dp[2*node+1][0][1]);
                    }
                    else
                    {
                        tmp1=tmp2={a[2*node+1]};
                        Merge(tmp1,dp[2*node][0][0],dp[2*node+1][i+1][j]);
                        Merge(tmp2,dp[2*node][i+1][j],dp[2*node+1][0][0]);
                        dp[node][i][j]=min(tmp1,tmp2);
                    }
                }
            }
        }
        if(2*node<n)
        {
            for(int i=0;node>>i;i++)
            {
                for(int j:{0,1})
                {
                    dp[2*node][i][j].clear();
                    dp[2*node+1][i][j].clear();
                }
            }
        }
    }

    for(int x:dp[1][0][1])cout<<x<<" ";
    cout<<"\n";

    return 0;
}

Compilation message

swap.cpp: In function 'void Merge(std::vector<int>&, std::vector<int>&, std::vector<int>&)':
swap.cpp:28:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     while(idxL<Left.size() or idxR<Right.size())
      |           ~~~~^~~~~~~~~~~~
swap.cpp:28:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     while(idxL<Left.size() or idxR<Right.size())
      |                               ~~~~^~~~~~~~~~~~~
swap.cpp:31:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |         while(idxL<Left.size() && cur--)res.push_back(Left[idxL++]);
      |               ~~~~^~~~~~~~~~~~
swap.cpp:33:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |         while(idxR<Right.size() && cur--)res.push_back(Right[idxR++]);
      |               ~~~~^~~~~~~~~~~~~
swap.cpp: In function 'int main()':
swap.cpp:59:34: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   59 |                 int idx=((node>>i+1)<<1)+j;
      |                                 ~^~
swap.cpp:92:34: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   92 |                 int idx=((node>>i+1)<<1)+j;
      |                                 ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 169520 KB Output is correct
2 Correct 87 ms 169528 KB Output is correct
3 Correct 69 ms 169296 KB Output is correct
4 Correct 70 ms 169304 KB Output is correct
5 Correct 75 ms 169304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 169520 KB Output is correct
2 Correct 87 ms 169528 KB Output is correct
3 Correct 69 ms 169296 KB Output is correct
4 Correct 70 ms 169304 KB Output is correct
5 Correct 75 ms 169304 KB Output is correct
6 Correct 103 ms 169536 KB Output is correct
7 Correct 82 ms 169324 KB Output is correct
8 Correct 81 ms 169320 KB Output is correct
9 Correct 80 ms 169300 KB Output is correct
10 Correct 71 ms 169304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 169520 KB Output is correct
2 Correct 87 ms 169528 KB Output is correct
3 Correct 69 ms 169296 KB Output is correct
4 Correct 70 ms 169304 KB Output is correct
5 Correct 75 ms 169304 KB Output is correct
6 Correct 103 ms 169536 KB Output is correct
7 Correct 82 ms 169324 KB Output is correct
8 Correct 81 ms 169320 KB Output is correct
9 Correct 80 ms 169300 KB Output is correct
10 Correct 71 ms 169304 KB Output is correct
11 Correct 65 ms 169556 KB Output is correct
12 Correct 71 ms 169624 KB Output is correct
13 Correct 95 ms 169548 KB Output is correct
14 Correct 102 ms 169956 KB Output is correct
15 Correct 87 ms 169844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 169520 KB Output is correct
2 Correct 87 ms 169528 KB Output is correct
3 Correct 69 ms 169296 KB Output is correct
4 Correct 70 ms 169304 KB Output is correct
5 Correct 75 ms 169304 KB Output is correct
6 Correct 103 ms 169536 KB Output is correct
7 Correct 82 ms 169324 KB Output is correct
8 Correct 81 ms 169320 KB Output is correct
9 Correct 80 ms 169300 KB Output is correct
10 Correct 71 ms 169304 KB Output is correct
11 Correct 65 ms 169556 KB Output is correct
12 Correct 71 ms 169624 KB Output is correct
13 Correct 95 ms 169548 KB Output is correct
14 Correct 102 ms 169956 KB Output is correct
15 Correct 87 ms 169844 KB Output is correct
16 Correct 128 ms 180172 KB Output is correct
17 Correct 130 ms 179916 KB Output is correct
18 Correct 112 ms 180288 KB Output is correct
19 Correct 177 ms 211532 KB Output is correct
20 Correct 185 ms 211188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 169520 KB Output is correct
2 Correct 87 ms 169528 KB Output is correct
3 Correct 69 ms 169296 KB Output is correct
4 Correct 70 ms 169304 KB Output is correct
5 Correct 75 ms 169304 KB Output is correct
6 Correct 103 ms 169536 KB Output is correct
7 Correct 82 ms 169324 KB Output is correct
8 Correct 81 ms 169320 KB Output is correct
9 Correct 80 ms 169300 KB Output is correct
10 Correct 71 ms 169304 KB Output is correct
11 Correct 65 ms 169556 KB Output is correct
12 Correct 71 ms 169624 KB Output is correct
13 Correct 95 ms 169548 KB Output is correct
14 Correct 102 ms 169956 KB Output is correct
15 Correct 87 ms 169844 KB Output is correct
16 Correct 128 ms 180172 KB Output is correct
17 Correct 130 ms 179916 KB Output is correct
18 Correct 112 ms 180288 KB Output is correct
19 Correct 177 ms 211532 KB Output is correct
20 Correct 185 ms 211188 KB Output is correct
21 Correct 307 ms 215872 KB Output is correct
22 Correct 287 ms 216452 KB Output is correct
23 Correct 332 ms 216904 KB Output is correct
24 Runtime error 359 ms 262144 KB Execution killed with signal 9
25 Halted 0 ms 0 KB -