답안 #931226

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
931226 2024-02-21T11:55:12 Z LucaIlie The Potion of Great Power (CEOI20_potion) C++17
56 / 100
3000 ms 74740 KB
#include <bits/stdc++.h>

using namespace std;

struct change {
    int time, val;
};

const int MAX_N = 1e5;
const int MAX_M = 2e5;
int n, m, d;
int h[MAX_N], a[MAX_M], b[MAX_M];
unordered_map<int, int> pos[MAX_N];
vector<int> trust[MAX_N];
set<int> hx, hy;
vector<vector<change>> changes[MAX_N];
vector<change> emptyV;

void init( int N, int D, int H[] ) {
    n = N;
    d = D;
    for ( int i = 0; i < n; i++ )
        h[i] = H[i];
    emptyV.push_back( { -1, -1 } );
    for ( int i = 0; i < n; i++ ) {
        trust[i].push_back( i );
        changes[i].push_back( emptyV );
    }
}

void curseChanges( int M, int A[], int B[] ) {
    m = M;
    for ( int i = 0; i < M; i++ ) {
        a[i] = A[i];
        b[i] = B[i];
    }

    for ( int i = 0; i < m; i++ ) {
        if ( changes[a[i]].size() <= trust[a[i]].size() )
            changes[a[i]].push_back( emptyV );
        if ( changes[b[i]].size() <= trust[b[i]].size() )
            changes[b[i]].push_back( emptyV );

        if ( pos[a[i]][b[i]] == 0 ) {
            pos[a[i]][b[i]] = trust[a[i]].size();
            changes[a[i]][trust[a[i]].size()].push_back( { i, b[i] } );
            trust[a[i]].push_back( b[i] );

            pos[b[i]][a[i]] = trust[b[i]].size();
            changes[b[i]][trust[b[i]].size()].push_back( { i, a[i] } );
            trust[b[i]].push_back( a[i] );
        } else {
            int p, l;

            p = pos[a[i]][b[i]], l = trust[a[i]].size() - 1;
            swap( pos[a[i]][b[i]], pos[a[i]][trust[a[i]][l]] );
            swap( trust[a[i]][p], trust[a[i]][l] );
            changes[a[i]][p].push_back( { i, trust[a[i]][p] } );
            changes[a[i]][l].push_back( { i, -1 } );
            pos[a[i]][trust[a[i]][l]] = 0;
            trust[a[i]].pop_back();

            p = pos[b[i]][a[i]], l = trust[b[i]].size() - 1;
            swap( pos[b[i]][a[i]], pos[b[i]][trust[b[i]][l]] );
            swap( trust[b[i]][p], trust[b[i]][l] );
            changes[b[i]][p].push_back( { i, trust[b[i]][p] } );
            changes[b[i]][l].push_back( { i, -1 } );
            pos[b[i]][trust[b[i]][l]] = 0;
            trust[b[i]].pop_back();
        }
    }

    /*for ( int i = 0; i < n; i++ ) {
        printf( "%d: \n", i );
        for ( int j = 1; j <= d; j++ ) {
            for ( change ch: changes[i][j] )
                printf( "%d %d\n", ch.time, ch.val );
            printf( "\n" );
        }
        printf( "\n" );
    }*/
}

int question( int x, int y, int t ) {
    hx.clear();
    for ( int i = 1; i < changes[x].size(); i++ ) {
        int l = 0, r = changes[x][i].size();
        while ( r - l > 1 ) {
            int mid = (l + r) / 2;

            if ( changes[x][i][mid].time < t )
                l = mid;
            else
                r = mid;
        }
        if ( changes[x][i][l].val != -1 )
            hx.insert( h[changes[x][i][l].val] );
    }
    hy.clear();
    for ( int i = 1; i < changes[y].size(); i++ ) {
        int l = 0, r = changes[y][i].size();
        while ( r - l > 1 ) {
            int mid = (l + r) / 2;

            if ( changes[y][i][mid].time < t )
                l = mid;
            else
                r = mid;
        }
        if ( changes[y][i][l].val != -1 )
            hy.insert( h[changes[y][i][l].val] );
    }

    int minDist = 1e9;
    for ( int l: hx ) {
        auto p = hy.upper_bound( l );
        if ( p != hy.end() )
            minDist = min( minDist, *p - l );
        if ( p != hy.begin() ) {
            p--;
            minDist = min( minDist, l - *p );
        }
    }

    return minDist;
}

Compilation message

potion.cpp: In function 'int question(int, int, int)':
potion.cpp:86:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<change> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for ( int i = 1; i < changes[x].size(); i++ ) {
      |                      ~~^~~~~~~~~~~~~~~~~~~
potion.cpp:100:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<change> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |     for ( int i = 1; i < changes[y].size(); i++ ) {
      |                      ~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 10840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11096 KB Output is correct
2 Correct 5 ms 11096 KB Output is correct
3 Correct 4 ms 11096 KB Output is correct
4 Correct 25 ms 21524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 533 ms 74552 KB Output is correct
2 Correct 560 ms 74560 KB Output is correct
3 Correct 280 ms 31808 KB Output is correct
4 Execution timed out 3061 ms 43236 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 512 ms 74740 KB Output is correct
2 Correct 1561 ms 45776 KB Output is correct
3 Correct 1091 ms 58564 KB Output is correct
4 Correct 1860 ms 58260 KB Output is correct
5 Correct 522 ms 74440 KB Output is correct
6 Correct 1982 ms 58504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 14936 KB Output is correct
2 Correct 113 ms 13656 KB Output is correct
3 Correct 222 ms 12632 KB Output is correct
4 Correct 1698 ms 14168 KB Output is correct
5 Correct 1639 ms 14616 KB Output is correct
6 Correct 150 ms 13400 KB Output is correct
7 Correct 1455 ms 13332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 10840 KB Output is correct
2 Correct 6 ms 11096 KB Output is correct
3 Correct 5 ms 11096 KB Output is correct
4 Correct 4 ms 11096 KB Output is correct
5 Correct 25 ms 21524 KB Output is correct
6 Correct 533 ms 74552 KB Output is correct
7 Correct 560 ms 74560 KB Output is correct
8 Correct 280 ms 31808 KB Output is correct
9 Execution timed out 3061 ms 43236 KB Time limit exceeded
10 Halted 0 ms 0 KB -