답안 #979252

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
979252 2024-05-10T12:15:26 Z ALeonidou 가장 긴 여행 (IOI23_longesttrip) C++17
15 / 100
14 ms 688 KB
#include "longesttrip.h"
#include <bits/stdc++.h>

using namespace std;

#define ll int
#define sz(x) (ll)x.size()
#define F first
#define S second
#define endl "\n"
#define pb push_back

typedef vector <ll> vi;
typedef pair <ll,ll> ii;
typedef vector <ii> vii;

#define dbg(x) cout<<#x<<": "<<x<<endl;
#define dbg2(x,y) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<endl;
#define dbg3(x,y,z) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<" "<<#z<<": "<<z<<endl;

void printVct(const vi &v){
    for (ll i =0; i<sz(v); i++)
        cout<<v[i]<<" ";
    cout<<endl;
}

vi longest_trip(int n, int d){
    vi st1, st2;
    st1.pb(0);
    st2.pb(1);

    //create stacks
    for (ll i = 2; i<n; i++){
        ll x = are_connected({st1.back()}, {i});
        ll y = are_connected({st2.back()}, {i});
        if (x){
            st1.pb(i);
        }
        else if (y){
            st2.pb(i);
        }
        else{
            for (ll j = 0; j<sz(st1); j++){
                st2.pb(st1[j]);
            }
            st1.assign(1, i);
        }
    }

    // cout<<"st1: ";
    // printVct(st1);
    // cout<<"st2: ";
    // printVct(st2);

    //merge stacks if possible  
    if (are_connected({st1[0]}, {st2.back()})){
        vi ans;
        for (ll i = 0; i<sz(st2); i++) ans.pb(st2[i]);
        for (ll i = 0; i<sz(st1); i++) ans.pb(st1[i]);
        return ans;
    }
    else if (are_connected({st1.back()}, {st2[0]})){
        vi ans;
        for (ll i = 0; i<sz(st1); i++) ans.pb(st1[i]);
        for (ll i = 0; i<sz(st2); i++) ans.pb(st2[i]);
        return ans;
    }
    else if (are_connected({st1[0]}, {st2[0]})){
        vi ans;
        for (ll i = sz(st1)-1; i>=0; i--) ans.pb(st1[i]);
        for (ll i = 0; i<sz(st2); i++) ans.pb(st2[i]);
        return ans;
    }
    else if (are_connected({st1.back()}, {st2.back()})){
        vi ans;
        for (ll i = 0; i<sz(st1); i++) ans.pb(st1[i]);
        for (ll i = sz(st2)-1; i>=0; i--) ans.pb(st2[i]);
        return ans;
    }


    // cout<<"CHECK 0"<<endl;

    //search for first connnection between stacks
    ll firstConnectionStart = 1;
    while ((firstConnectionStart < sz(st1)-1) && !are_connected({st1[firstConnectionStart]}, st2)){
        firstConnectionStart++;
    }
    // dbg(firstConnectionStart);

    if (firstConnectionStart >= sz(st1) - 1){   //no connection case => 2 strongly/fully connected stacks
        // cout<<"CHECK 2"<<endl;
        if (sz(st1) > sz(st2)){
            return st1;
        }
        else{
            return st2;
        }
    }

    ll firstConnectionEnd = 1;
    while (!are_connected({st2[firstConnectionEnd]}, {firstConnectionStart})){
        firstConnectionEnd++;
    }
    // dbg(firstConnectionEnd);

    // cout<<"CHECK 1"<<endl;
    // return vi();

    vi ans;
    for (ll i = firstConnectionStart+1; i<sz(st1); i++){
        ans.pb(st1[i]);
    }
    for (ll i = 0; i<=firstConnectionStart; i++){
        ans.pb(st1[i]);
    }
    for (ll i = firstConnectionEnd; i<sz(st2); i++){
        ans.pb(st2[i]);
    }
    for (ll i =0; i<firstConnectionEnd; i++){
        ans.pb(st2[i]);
    }
    return ans;
}

/*
2
5 1
1
1 1
0 0 1
0 0 0 1
4 1
1
0 0
0 0 1


5
1 0 2 3 4
4
2
0 1
3
4

1
3 1
0
0 1

1
3 1
1
0 0

*/


















# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 7 ms 344 KB Output is correct
3 Correct 8 ms 344 KB Output is correct
4 Correct 9 ms 344 KB Output is correct
5 Correct 8 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 9 ms 344 KB Output is correct
3 Correct 8 ms 404 KB Output is correct
4 Correct 8 ms 344 KB Output is correct
5 Correct 8 ms 600 KB Output is correct
6 Correct 9 ms 344 KB Output is correct
7 Correct 8 ms 344 KB Output is correct
8 Correct 9 ms 344 KB Output is correct
9 Correct 9 ms 344 KB Output is correct
10 Correct 8 ms 344 KB Output is correct
11 Correct 9 ms 344 KB Output is correct
12 Correct 10 ms 600 KB Output is correct
13 Correct 9 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 8 ms 344 KB Output is correct
3 Correct 8 ms 344 KB Output is correct
4 Correct 10 ms 344 KB Output is correct
5 Correct 10 ms 600 KB Output is correct
6 Correct 10 ms 344 KB Output is correct
7 Correct 9 ms 344 KB Output is correct
8 Correct 9 ms 344 KB Output is correct
9 Correct 9 ms 344 KB Output is correct
10 Correct 8 ms 344 KB Output is correct
11 Correct 8 ms 344 KB Output is correct
12 Correct 8 ms 340 KB Output is correct
13 Correct 9 ms 344 KB Output is correct
14 Correct 11 ms 344 KB Output is correct
15 Incorrect 2 ms 344 KB Incorrect
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 8 ms 344 KB Output is correct
3 Correct 8 ms 344 KB Output is correct
4 Correct 10 ms 344 KB Output is correct
5 Partially correct 9 ms 344 KB Output is partially correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 8 ms 344 KB Output is correct
8 Correct 14 ms 344 KB Output is correct
9 Correct 11 ms 344 KB Output is correct
10 Partially correct 10 ms 440 KB Output is partially correct
11 Partially correct 9 ms 436 KB Output is partially correct
12 Partially correct 10 ms 688 KB Output is partially correct
13 Partially correct 13 ms 600 KB Output is partially correct
14 Correct 10 ms 344 KB Output is correct
15 Incorrect 2 ms 344 KB Incorrect
16 Halted 0 ms 0 KB -