Submission #1123889

#TimeUsernameProblemLanguageResultExecution timeMemory
1123889Trisanu_DasArchery (IOI09_archery)C++20
36 / 100
2096 ms8228 KiB
#include <bits/stdc++.h>
using namespace std;

int get(vector <int> a, int x, int R) {
    if (a.size() == 2) return 0; 
    int n = a.size() / 2;             
    for (int t = 0; t < R; ++t) {
        for (int i = 0; i < a.size(); i += 2)
            if (a[i] > a[i + 1])
                swap(a[i], a[i + 1]);
        if (a[0] == 0) {
            bool all2 = 1;
            for (int i = 1; i < 2 * n; i += 2) {
                if (a[i] != 2) {
                    all2 = 0;
                    break;
                }   
            }   
            if (all2) {
                int r = R - t;
                for (int i = 0; i < a.size(); ++i)
                    if (a[i] == x) {
                        int ans = i / 2;
                        ans -= r;
                        return (ans % n + n) % n;
                    }
            }            
            if (a[1] == 0 && a[0] != x && a[1] != x) {
                bool all0 = 1;
                for (int i = 0; i < 2 * n; i += 2) {
                    if (a[i]) {
                        all0 = 0;
                        break;
                    }   
                }   
                if (all0)
                    for (int i = 0; i < a.size(); ++i) 
                        if (a[i] == x) 
                            return i / 2;            
            }                 
        }    
        vector <int> b = a;
        b[1] = a[2];
        for (int i = 4; i < a.size(); i += 2) b[i - 2] = a[i];            
        b[(int)b.size() - 2] = a[1]; 
        a = b;
    }
    for (int i = 0; i < a.size(); ++i)
        if (a[i] == x)
            return i / 2;
}   

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n, R; cin >> n >> R;
    R %= n;
    R += 2 * n;
    int my; cin >> my;
    if (my == 1) {
        cout << n << '\n';
        return 0;
    }   
    vector <int> p(2 * n - 1);
    for (int i = 0; i < 2 * n - 1; ++i) {
        cin >> p[i];
        if (p[i] < my) p[i] = 0;
        else p[i] = 2;
    }            
    my = 1;
    int ans = 1e9, start = -1;
    vector <int> nd(2 * n, -1);
    for (int i = 0; i < 2 * n; i += 2) {
        if (nd[i] == -1) {
            vector <int> t = p;
            t.insert(t.begin() + i, my);
            nd[i] = get(t, my, R);
        }   
        if (i && nd[i] > nd[i - 2]) break;
        if (nd[i] < ans || (nd[i] == ans && i / 2 > start)) {
            ans = nd[i];
            start = i / 2;
        }   
    }   
    for (int i = 2 * n - 1; i >= 0; i -= 2) {
        if (nd[i] == -1) {
            vector <int> t = p;
            t.insert(t.begin() + i, my);
            nd[i] = get(t, my, R);
        }   
        if (i + 2 < 2 * n && nd[i] > nd[i + 2]) break;
        if (nd[i] < ans || (nd[i] == ans && i / 2 > start)) {
            ans = nd[i];
            start = i / 2;
        }   
    }   
    cout << start + 1 << '\n';
}

Compilation message (stderr)

archery.cpp: In function 'int get(std::vector<int>, int, int)':
archery.cpp:51:1: warning: control reaches end of non-void function [-Wreturn-type]
   51 | }
      | ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...