제출 #655875

#제출 시각아이디문제언어결과실행 시간메모리
655875LoboFlight to the Ford (BOI22_communication)C++17
74 / 100
3321 ms2104 KiB
#include"communication.h"
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

//
// --- Sample implementation for the task communication ---
//
// To compile this program with the sample grader, place:
//     communication.h communication_sample.cpp sample_grader.cpp
// in a single folder, then open the terminal in this directory (right-click onto an empty spot in the directory,
// left click on "Open in terminal") and enter e.g.:
//     g++ -std=c++17 communication_sample.cpp sample_grader.cpp
// in this folder. This will create a file a.out in the current directory which you can execute from the terminal
// as ./a.out
// See task statement or sample_grader.cpp for the input specification
//

vector<pair<int,int>> join(vector<pair<int,int>> v1, vector<pair<int,int>> v2) {
    vector<pair<int,int>> v;
    for(auto x : v1) v.pb(x);
    for(auto x : v2) v.pb(x);
    return v;
}
void encode(int N, int X) {

    // 0000 0110 1001 1111
    vector<int> pos[(1<<4)];
    vector<int> masks = {0,6,9,15};
    for(int i = 0; i < masks.size(); i++) {
        for(int msk = 0; msk < (1<<4); msk++) {
            int antw = 0;
            bool ok = true;
            for(int j = 0; j < 4; j++) {
                if(((1<<j)&msk) == ((1<<j)&masks[i])) {
                    antw = 0;
                }
                else {
                    if(antw == 1) ok = false;
                    antw = 1;
                }
            }

            if(ok) {
                pos[msk].pb(i);
            }
        }
    }

    vector<pair<int,int>> vec,vqr; vec.pb(mp(1,N));
    int sz = N;
    while(sz >= 3) {
        vector<pair<int,int>> p[4];
        int sz1 = sz/4; sz-= sz1;
        int sz2 = sz/3; sz-= sz2;
        int sz3 = sz/2; sz-= sz3;
        int sz4 = sz;

        // cout << "  "  << sz1 << " " << sz2 << " " << sz3 << " " << sz4 << endl;

        while(vec.size() && sz1 != 0) {
            int l = vec.back().fr;
            int r = vec.back().sc;
            vec.pop_back();
            int l1 = max(l,r+1-sz1);
            p[0].pb(mp(l1,r));
            sz1-= (r-l1+1);
            if(l1 != l) vec.pb(mp(l,l1-1));
        }
        while(vec.size() && sz2 != 0) {
            int l = vec.back().fr;
            int r = vec.back().sc;
            vec.pop_back();
            int l1 = max(l,r+1-sz2);
            p[1].pb(mp(l1,r));
            sz2-= (r-l1+1);
            if(l1 != l) vec.pb(mp(l,l1-1));
        }
        while(vec.size() && sz3 != 0) {
            int l = vec.back().fr;
            int r = vec.back().sc;
            vec.pop_back();
            int l1 = max(l,r+1-sz3);
            p[2].pb(mp(l1,r));
            sz3-= (r-l1+1);
            if(l1 != l) vec.pb(mp(l,l1-1));
        }
        p[3] = vec;
        vec.clear();

        


        // cout << 1 << endl;
        // for(auto x : p[0]) cout << x.fr << " " << x.sc << " , ";
        // cout << endl;
        // cout << 2 << endl;
        // for(auto x : p[1]) cout << x.fr << " " << x.sc << " , ";
        // cout << endl;
        // cout << 3 << endl;
        // for(auto x : p[2]) cout << x.fr << " " << x.sc << " , ";
        // cout << endl;
        // cout << 4 << endl;
        // for(auto x : p[3]) cout << x.fr << " " << x.sc << " , ";
        // cout << endl;

        // int ans1 = 0;
        // // p[0] p[1] / p[2] p[3]
        // vqr = join(p[0],p[1]);
        // for(auto x : vqr) if(x.fr <= X && x.sc >= X) ans1 = 1;
        // ans1 = send(ans1);
        // int ans2 = 0;
        // // p[0] p[2] / p[1] p[3]
        // vqr = join(p[0],p[2]);
        // for(auto x : vqr) if(x.fr <= X && x.sc >= X) ans2 = 1;
        // ans2 = send(ans2);

        // if(ans1 == 0 && ans2 == 0) p[0].clear();
        // if(ans1 == 0 && ans2 == 1) p[1].clear();
        // if(ans1 == 1 && ans2 == 0) p[2].clear();
        // if(ans1 == 1 && ans2 == 1) p[3].clear();

        int id0;
        for(int i = 0; i < 4; i++) {
            for(auto x : p[i]) {
                if(x.fr <= X && x.sc >= X) id0 = i;
            }
        }

        int msksend = 0;
        for(int i = 3; i >= 0; i--) {
            msksend+= (1<<i)*send(((1<<i)&masks[id0]) != 0);
        }

        int id1;
        for(auto x : pos[msksend]) {
            if(x != id0) id1 = x;
        }
        for(int i = 0; i < 4; i++) {
            if(i != id0 && i != id1) p[i].clear();
        }
        vec.clear();
        for(auto x : p[0]) vec.pb(x);
        for(auto x : p[1]) vec.pb(x);
        for(auto x : p[2]) vec.pb(x);
        for(auto x : p[3]) vec.pb(x);
        sz = 0;
        for(auto x : vec) sz+= x.sc-x.fr+1;
    }
    vector<pair<int,int>> vc1;
    for(auto x : vec) {
        for(int i = x.fr; i <= x.sc; i++) vc1.pb(mp(i,i));
    }
    vec = vc1;
    // if(vec.size() == 0) return {vec[0].fr,vec[0].fr};
    // else return {vec[0].fr,vec[1].fr};
    // int ans1 = 0;
    // // 0 / 1 2
    // vqr = {vec[0]};
    // for(auto x : vqr) if(x.fr <= X && x.sc >= X) ans1 = 1;
    // ans1 = send(ans1);

    // if(ans1 == 0) {
    //     // I have 0
    //     int ans2 = 0;
    //     // 0 | 1 2
    //     vqr = {vec[0]};
    //     for(auto x : vqr) if(x.fr <= X && x.sc >= X) ans2 = 1;
    //     ans2 = send(ans2);
    //     if(ans2 == 0) {
    //         // I take 0 off
    //         // return {vec[1],vec[2]};
    //     }
    //     else {
    //         // I have 1 2
    //         int ans3 = 0;
    //         // 1 | 0 2
    //         vqr = {vec[1]};
    //         for(auto x : vqr) if(x.fr <= X && x.sc >= X) ans3 = 1;
    //         ans3 = send(ans3);
    //         if(ans3 == 0) {
    //             // I take 1 off
    //             // return {vec[0],vec[2]};
    //         }
    //         else {
    //             // I take 2 off
    //             // return {vec[0],vec[1]};
    //         }
    //     }
    // }
    // else {
    //     // I have 1 2
    //     int ans2 = 0;
    //     // 1 | 0 2
    //     vqr = {vec[1]};
    //     for(auto x : vqr) if(x.fr <= X && x.sc >= X) ans2 = 1;
    //     ans2 = send(ans2);
    //     if(ans2 == 0) {
    //         // I take 1 off
    //         // return {vec[0],vec[2]};
    //     }
    //     else {
    //         // I take 2 off
    //         // return {vec[0],vec[1]};
    //     }

    // }
}

std::pair<int, int> decode(int N) {
    
    // 0000 0110 1001 1111
    vector<int> pos[(1<<4)];
    vector<int> masks = {0,6,9,15};
    for(int i = 0; i < masks.size(); i++) {
        for(int msk = 0; msk < (1<<4); msk++) {
            int antw = 0;
            bool ok = true;
            for(int j = 0; j < 4; j++) {
                if(((1<<j)&msk) == ((1<<j)&masks[i])) {
                    antw = 0;
                }
                else {
                    if(antw == 1) ok = false;
                    antw = 1;
                }
            }

            if(ok) {
                pos[msk].pb(i);
            }
        }
    }

    vector<pair<int,int>> vec,vqr; vec.pb(mp(1,N));
    int sz = N;
    while(sz >= 3) {
        vector<pair<int,int>> p[4];
        int sz1 = sz/4; sz-= sz1;
        int sz2 = sz/3; sz-= sz2;
        int sz3 = sz/2; sz-= sz3;
        int sz4 = sz;

        // cout << "  "  << sz1 << " " << sz2 << " " << sz3 << " " << sz4 << endl;

        while(vec.size() && sz1 != 0) {
            int l = vec.back().fr;
            int r = vec.back().sc;
            vec.pop_back();
            int l1 = max(l,r+1-sz1);
            p[0].pb(mp(l1,r));
            sz1-= (r-l1+1);
            if(l1 != l) vec.pb(mp(l,l1-1));
        }
        while(vec.size() && sz2 != 0) {
            int l = vec.back().fr;
            int r = vec.back().sc;
            vec.pop_back();
            int l1 = max(l,r+1-sz2);
            p[1].pb(mp(l1,r));
            sz2-= (r-l1+1);
            if(l1 != l) vec.pb(mp(l,l1-1));
        }
        while(vec.size() && sz3 != 0) {
            int l = vec.back().fr;
            int r = vec.back().sc;
            vec.pop_back();
            int l1 = max(l,r+1-sz3);
            p[2].pb(mp(l1,r));
            sz3-= (r-l1+1);
            if(l1 != l) vec.pb(mp(l,l1-1));
        }
        p[3] = vec;
        vec.clear();


        // cout << 1 << endl;
        // for(auto x : p[0]) cout << x.fr << " " << x.sc << " , ";
        // cout << endl;
        // cout << 2 << endl;
        // for(auto x : p[1]) cout << x.fr << " " << x.sc << " , ";
        // cout << endl;
        // cout << 3 << endl;
        // for(auto x : p[2]) cout << x.fr << " " << x.sc << " , ";
        // cout << endl;
        // cout << 4 << endl;
        // for(auto x : p[3]) cout << x.fr << " " << x.sc << " , ";
        // cout << endl;

        // int ans1 = 0;
        // // p[0] p[1] / p[2] p[3]
        // vqr = join(p[0],p[1]);
        // for(auto x : vqr) if(x.fr <= X && x.sc >= X) ans1 = 1;
        // ans1 = send(ans1);
        // int ans2 = 0;
        // // p[0] p[2] / p[1] p[3]
        // vqr = join(p[0],p[2]);
        // for(auto x : vqr) if(x.fr <= X && x.sc >= X) ans2 = 1;
        // ans2 = send(ans2);

        // if(ans1 == 0 && ans2 == 0) p[0].clear();
        // if(ans1 == 0 && ans2 == 1) p[1].clear();
        // if(ans1 == 1 && ans2 == 0) p[2].clear();
        // if(ans1 == 1 && ans2 == 1) p[3].clear();

        int msksend = 0;
        for(int i = 3; i >= 0; i--) {
            msksend+= (1<<i)*receive();
        }

        int id0,id1;
        for(auto x : pos[msksend]) {
            id0 = x;
        }
        for(auto x : pos[msksend]) {
            if(x != id0) id1 = x;
        }
        for(int i = 0; i < 4; i++) {
            if(i != id0 && i != id1) p[i].clear();
        }
        vec.clear();
        for(auto x : p[0]) vec.pb(x);
        for(auto x : p[1]) vec.pb(x);
        for(auto x : p[2]) vec.pb(x);
        for(auto x : p[3]) vec.pb(x);
        sz = 0;
        for(auto x : vec) sz+= x.sc-x.fr+1;
    }
    vector<pair<int,int>> vc1;
    for(auto x : vec) {
        for(int i = x.fr; i <= x.sc; i++) vc1.pb(mp(i,i));
    }
    vec = vc1;
    if(vec.size() == 1) return {vec[0].fr,vec[0].fr};
    else return {vec[0].fr,vec[1].fr};
    // int ans1 = 0;
    // // 0 / 1 2
    // vqr = {vec[0]};
    // for(auto x : vqr) if(x.fr <= X && x.sc >= X) ans1 = 1;
    // ans1 = send(ans1);

    // if(ans1 == 0) {
    //     // I have 0
    //     int ans2 = 0;
    //     // 0 | 1 2
    //     vqr = {vec[0]};
    //     for(auto x : vqr) if(x.fr <= X && x.sc >= X) ans2 = 1;
    //     ans2 = send(ans2);
    //     if(ans2 == 0) {
    //         // I take 0 off
    //         // return {vec[1],vec[2]};
    //     }
    //     else {
    //         // I have 1 2
    //         int ans3 = 0;
    //         // 1 | 0 2
    //         vqr = {vec[1]};
    //         for(auto x : vqr) if(x.fr <= X && x.sc >= X) ans3 = 1;
    //         ans3 = send(ans3);
    //         if(ans3 == 0) {
    //             // I take 1 off
    //             // return {vec[0],vec[2]};
    //         }
    //         else {
    //             // I take 2 off
    //             // return {vec[0],vec[1]};
    //         }
    //     }
    // }
    // else {
    //     // I have 1 2
    //     int ans2 = 0;
    //     // 1 | 0 2
    //     vqr = {vec[1]};
    //     for(auto x : vqr) if(x.fr <= X && x.sc >= X) ans2 = 1;
    //     ans2 = send(ans2);
    //     if(ans2 == 0) {
    //         // I take 1 off
    //         // return {vec[0],vec[2]};
    //     }
    //     else {
    //         // I take 2 off
    //         // return {vec[0],vec[1]};
    //     }

    // }
}

컴파일 시 표준 에러 (stderr) 메시지

communication.cpp: In function 'void encode(int, int)':
communication.cpp:38:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for(int i = 0; i < masks.size(); i++) {
      |                    ~~^~~~~~~~~~~~~~
communication.cpp:65:13: warning: unused variable 'sz4' [-Wunused-variable]
   65 |         int sz4 = sz;
      |             ^~~
communication.cpp: In function 'std::pair<int, int> decode(int)':
communication.cpp:223:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  223 |     for(int i = 0; i < masks.size(); i++) {
      |                    ~~^~~~~~~~~~~~~~
communication.cpp:250:13: warning: unused variable 'sz4' [-Wunused-variable]
  250 |         int sz4 = sz;
      |             ^~~
communication.cpp: In function 'void encode(int, int)':
communication.cpp:148:30: warning: 'id1' may be used uninitialized in this function [-Wmaybe-uninitialized]
  148 |             if(i != id0 && i != id1) p[i].clear();
      |                            ~~^~~~~~
communication.cpp:148:18: warning: 'id0' may be used uninitialized in this function [-Wmaybe-uninitialized]
  148 |             if(i != id0 && i != id1) p[i].clear();
      |                ~~^~~~~~
communication.cpp: In function 'std::pair<int, int> decode(int)':
communication.cpp:327:30: warning: 'id1' may be used uninitialized in this function [-Wmaybe-uninitialized]
  327 |             if(i != id0 && i != id1) p[i].clear();
      |                            ~~^~~~~~
communication.cpp:327:18: warning: 'id0' may be used uninitialized in this function [-Wmaybe-uninitialized]
  327 |             if(i != id0 && i != id1) p[i].clear();
      |                ~~^~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...