結果

問題 No.1306 Exactly 2 Digits
ユーザー milanis48663220milanis48663220
提出日時 2020-12-03 23:16:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 5,958 bytes
コンパイル時間 1,537 ms
コンパイル使用メモリ 129,744 KB
実行使用メモリ 25,460 KB
平均クエリ数 1236.78
最終ジャッジ日時 2024-07-17 09:14:06
合計ジャッジ時間 15,858 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
24,812 KB
testcase_01 AC 26 ms
25,196 KB
testcase_02 AC 26 ms
24,580 KB
testcase_03 AC 26 ms
24,836 KB
testcase_04 AC 26 ms
24,580 KB
testcase_05 AC 26 ms
25,220 KB
testcase_06 AC 26 ms
24,580 KB
testcase_07 AC 26 ms
25,220 KB
testcase_08 AC 26 ms
24,964 KB
testcase_09 AC 26 ms
25,460 KB
testcase_10 AC 25 ms
25,220 KB
testcase_11 AC 26 ms
24,836 KB
testcase_12 AC 26 ms
25,220 KB
testcase_13 AC 27 ms
25,452 KB
testcase_14 AC 26 ms
24,812 KB
testcase_15 AC 27 ms
25,196 KB
testcase_16 AC 26 ms
25,196 KB
testcase_17 AC 26 ms
25,196 KB
testcase_18 AC 27 ms
24,668 KB
testcase_19 AC 26 ms
24,556 KB
testcase_20 AC 25 ms
25,196 KB
testcase_21 AC 26 ms
24,812 KB
testcase_22 AC 26 ms
24,940 KB
testcase_23 AC 26 ms
24,544 KB
testcase_24 AC 26 ms
24,812 KB
testcase_25 AC 25 ms
24,812 KB
testcase_26 AC 26 ms
24,940 KB
testcase_27 AC 27 ms
25,196 KB
testcase_28 AC 26 ms
25,196 KB
testcase_29 AC 26 ms
25,196 KB
testcase_30 AC 27 ms
24,940 KB
testcase_31 AC 27 ms
25,196 KB
testcase_32 AC 26 ms
24,556 KB
testcase_33 AC 26 ms
24,844 KB
testcase_34 AC 27 ms
24,928 KB
testcase_35 AC 25 ms
25,196 KB
testcase_36 AC 26 ms
24,556 KB
testcase_37 AC 26 ms
25,196 KB
testcase_38 AC 26 ms
24,556 KB
testcase_39 AC 26 ms
24,812 KB
testcase_40 AC 26 ms
25,196 KB
testcase_41 AC 27 ms
24,812 KB
testcase_42 AC 27 ms
25,196 KB
testcase_43 AC 27 ms
24,940 KB
testcase_44 AC 28 ms
25,184 KB
testcase_45 AC 28 ms
24,556 KB
testcase_46 AC 29 ms
24,556 KB
testcase_47 AC 29 ms
24,812 KB
testcase_48 AC 31 ms
24,940 KB
testcase_49 AC 31 ms
24,556 KB
testcase_50 AC 33 ms
25,196 KB
testcase_51 AC 33 ms
24,940 KB
testcase_52 AC 36 ms
24,940 KB
testcase_53 AC 38 ms
25,196 KB
testcase_54 AC 36 ms
24,812 KB
testcase_55 AC 39 ms
24,556 KB
testcase_56 AC 41 ms
24,812 KB
testcase_57 AC 42 ms
24,940 KB
testcase_58 AC 44 ms
25,052 KB
testcase_59 AC 42 ms
25,196 KB
testcase_60 AC 50 ms
25,196 KB
testcase_61 AC 53 ms
25,196 KB
testcase_62 AC 54 ms
24,812 KB
testcase_63 AC 57 ms
24,556 KB
testcase_64 AC 55 ms
25,452 KB
testcase_65 AC 58 ms
25,196 KB
testcase_66 AC 64 ms
25,196 KB
testcase_67 AC 64 ms
25,196 KB
testcase_68 AC 64 ms
24,940 KB
testcase_69 AC 67 ms
24,796 KB
testcase_70 AC 68 ms
24,940 KB
testcase_71 AC 72 ms
25,196 KB
testcase_72 AC 71 ms
25,324 KB
testcase_73 AC 70 ms
24,812 KB
testcase_74 AC 81 ms
24,940 KB
testcase_75 AC 79 ms
25,196 KB
testcase_76 AC 87 ms
25,196 KB
testcase_77 AC 95 ms
24,556 KB
testcase_78 AC 100 ms
25,196 KB
testcase_79 AC 107 ms
25,196 KB
testcase_80 AC 109 ms
24,796 KB
testcase_81 AC 93 ms
25,196 KB
testcase_82 AC 115 ms
24,556 KB
testcase_83 AC 117 ms
24,940 KB
testcase_84 AC 125 ms
24,556 KB
testcase_85 AC 132 ms
25,196 KB
testcase_86 AC 110 ms
24,812 KB
testcase_87 AC 114 ms
24,556 KB
testcase_88 AC 140 ms
24,940 KB
testcase_89 AC 118 ms
24,556 KB
testcase_90 AC 104 ms
24,940 KB
testcase_91 AC 115 ms
24,812 KB
testcase_92 AC 107 ms
24,940 KB
testcase_93 AC 123 ms
24,556 KB
testcase_94 AC 105 ms
24,940 KB
testcase_95 AC 111 ms
25,324 KB
testcase_96 AC 102 ms
25,196 KB
testcase_97 AC 104 ms
24,940 KB
testcase_98 AC 99 ms
24,940 KB
testcase_99 AC 115 ms
24,812 KB
testcase_100 AC 117 ms
24,940 KB
testcase_101 AC 123 ms
24,812 KB
testcase_102 AC 104 ms
24,812 KB
testcase_103 AC 111 ms
25,196 KB
testcase_104 AC 110 ms
24,812 KB
testcase_105 AC 113 ms
24,940 KB
testcase_106 AC 100 ms
24,812 KB
testcase_107 AC 117 ms
24,812 KB
testcase_108 AC 109 ms
24,940 KB
testcase_109 AC 129 ms
24,812 KB
testcase_110 AC 104 ms
24,940 KB
testcase_111 AC 109 ms
24,812 KB
testcase_112 AC 99 ms
24,556 KB
testcase_113 AC 132 ms
24,812 KB
testcase_114 AC 134 ms
25,184 KB
testcase_115 AC 113 ms
24,812 KB
testcase_116 AC 137 ms
24,812 KB
testcase_117 AC 118 ms
24,940 KB
testcase_118 AC 125 ms
24,556 KB
testcase_119 AC 110 ms
24,812 KB
testcase_120 AC 102 ms
25,196 KB
testcase_121 AC 121 ms
24,812 KB
testcase_122 AC 119 ms
24,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>
#include <random>

// #define DEBUG

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

class Simulator{
    public:
        Simulator(int _n){
            random_device seed_gen;
            mt19937 engine(seed_gen());
            n = _n;
            m = n*(n-1);
            vector<int> v;
            for(int i = 1; i <= n-1; i++){
                for(int j = 0; j <= n-1; j++){
                    v.push_back(i*n+j);
                }
            }
            shuffle(v.begin(), v.end(), engine);
            for(int i : v){
                x.push_back(i/n);
                y.push_back(i%n);
            }
        }
        int n;
        int m;
        int query_count = 0;
        vector<int> x;
        vector<int> y;
        // queryは1-indexed
        P query(int i, int j){
            query_count++;
            assert(query_count <= 3*(n*(n-1))/2);
            i--; j--;
            int p = x[i]-x[j];
            int q = y[i]-y[j];
            if(p > q) swap(p, q);
            return P(p, q);
        }

        bool verify(vector<int> ans){
            assert(ans.size() == m);
            for(int i = 0; i < m; i++){
                if(ans[i] != x[i]*n+y[i]) return false;
            }
            return true;
        }

        void print_correct_ans(){
            for(int i = 0; i < m; i++) cout << x[i]*n+y[i] << ' ';
            cout << endl;
        }
};

int n;

bool is_in_field(int x, int y){
    return x >= 1 && x <= n-1 && y >= 0 && y <= n-1;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> n;
#ifdef DEBUG
    Simulator sim(n);
#endif

    int m = n*(n-1);
    vector<int> sum(m+1), vp(m+1), vq(m+1);
    int min_sum = 100000, min_sum_idx = -1;
    int max_sum = -100000, max_sum_idx = -1;
    map<P, vector<int>> mp;
    for(int i = 2; i <= m; i++){
        int p, q;
        #ifdef DEBUG
            auto x = sim.query(i, 1);
            p = x.first; q = x.second;
        #else
            cout << "? " << i << ' ' << 1 << endl;
            cin >> p >> q;
        #endif
        vp[i] = p; vq[i] = q; sum[i] = p+q;
        mp[P(p, q)].push_back(i);
        if(max_sum < sum[i]){
            max_sum = sum[i];
            max_sum_idx = i;
        }
        if(min_sum > sum[i]){
            min_sum = sum[i];
            min_sum_idx = i;
        }
    }
    vector<int> ans_x(m+1), ans_y(m+1);
    if(min_sum < 0) {
        ans_x[min_sum_idx] = 1; ans_y[min_sum_idx] = 0;
    }
    if(max_sum > 0) {
        ans_x[max_sum_idx] = n-1; ans_y[max_sum_idx] = n-1;
    }
    if(min_sum > 0){
        ans_x[1] = 1; ans_y[1] = 0;
    }else if(max_sum < 0){
        ans_x[1] = n-1; ans_y[1] = n-1;
    }else if(vp[max_sum_idx]-vp[min_sum_idx] == n-1 || vq[max_sum_idx]-vp[min_sum_idx] == n-1){
        ans_y[1] = -vp[min_sum_idx];
        ans_x[1] = 1-vq[min_sum_idx];
    }else if(vp[max_sum_idx]-vq[min_sum_idx] == n-1 || vq[max_sum_idx]-vq[min_sum_idx] == n-1){
        ans_y[1] = -vq[min_sum_idx];
        ans_x[1] = 1-vp[min_sum_idx];
    }else{
        throw "";
    }
    for(auto i : mp){
        int p = i.first.first;
        int q = i.first.second;
        auto v = i.second;
        int x1 = ans_x[1]+p; int y1 = ans_y[1]+q;
        int x2 = ans_x[1]+q; int y2 = ans_y[1]+p;
        if(v.size() == 1){
            if(is_in_field(x1, y1)) {
                ans_x[v[0]] = x1;
                ans_y[v[0]] = y1;
            }else{
                assert(is_in_field(x2, y2));
                ans_x[v[0]] = x2;
                ans_y[v[0]] = y2;
            }
            continue;
        }
        int s = -1, t = -1;
        // (1, 0)では区別がつかないので(n-1, n-1)を投げる
        if(ans_x[1]-ans_y[1] == 1){
        #ifdef DEBUG
            auto x = sim.query(v[0], max_sum_idx);
            s = x.first; t = x.second;
        #else
            cout << "? " << v[0] << ' ' << max_sum_idx << endl;
            cin >> s >> t;
        #endif
            int s_sim = min(x1-(n-1), y1-(n-1));    
            int t_sim = max(x1-(n-1), y1-(n-1));
            if(s_sim == s && t_sim == t){
                ans_x[v[0]] = x1; ans_y[v[0]] = y1;
                ans_x[v[1]] = x2; ans_y[v[1]] = y2;
            }else{
                ans_x[v[0]] = x2; ans_y[v[0]] = y2;
                ans_x[v[1]] = x1; ans_y[v[1]] = y1;
            }
        }else{
        #ifdef DEBUG
            auto x = sim.query(v[0], min_sum_idx);
            s = x.first; t = x.second;
        #else
            cout << "? " << v[0] << ' ' << min_sum_idx << endl;
            cin >> s >> t;
        #endif
            int s_sim = min(x1-1, y1);    
            int t_sim = max(x1-1, y1);
            if(s_sim == s && t_sim == t){
                ans_x[v[0]] = x1; ans_y[v[0]] = y1;
                ans_x[v[1]] = x2; ans_y[v[1]] = y2;
            }else{
                ans_x[v[0]] = x2; ans_y[v[0]] = y2;
                ans_x[v[1]] = x1; ans_y[v[1]] = y1;
            }
        }
    }

    
#ifdef DEBUG
    cout << min_sum_idx << ' ' << max_sum_idx << endl;
    vector<int> ans;
    for(int i = 1; i <= m; i++){
        ans.push_back(ans_x[i]*n+ans_y[i]);
    }
    if(sim.verify(ans)){
        cout << "OK" << endl;
    }else{
        cout << "NG" << endl;
    }
    cout << "! ";    
    sim.print_correct_ans();
#endif
    cout << "! ";
    for(int i = 1; i <= m; i++) cout << ans_x[i]*n+ans_y[i] << ' ';
    cout << endl;
}
0