QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#554081#8939. PermutationCharizard2021ML 1ms3720kbC++141.6kb2024-09-09 05:05:212024-09-09 05:05:21

Judging History

This is the latest submission verdict.

  • [2024-09-09 05:05:21]
  • Judged
  • Verdict: ML
  • Time: 1ms
  • Memory: 3720kb
  • [2024-09-09 05:05:21]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
long double phi = (sqrt(5.0) - 1.0)/2.0;
int solve(int low, int high, int cur){
    if(low == high){
        return low;
    }
    if(cur == INT_MIN){
        cout << "? " << low << " " << high << "\n";
        cout.flush();
        cin >> cur;
    }
    if(low + 1 == high){
        if(cur == low){
            return high;
        }
        else{
            return low;
        }
    }
    int val = (int)((long double)(high - low + 1) * (long double)phi + (long double)0.5);
    int mid;
    if(cur <= val + low){
        mid = val + low;
    }
    else{
        mid = high - val;
    }
    if(cur <= mid){
        int x = INT_MIN;
        if(low != mid){
            cout << "? " << low << " " << mid << "\n";
            cout.flush();
            cin >> x;
        }
        if(x == cur){
            return solve(low, mid, x);
        }
        else{
            return solve(mid + 1, high, INT_MIN);
        }
    }
    else{
        int x = INT_MIN;
        if(mid + 1 != high){
            cout << "? " << mid + 1 << " " << high << "\n";
            cout.flush();
            cin >> x;
        }
        if(x == cur){
            return solve(mid + 1, high, x);
        }
        else{
            return solve(low, mid, INT_MIN);
        }
    }
}
int main(){
    // cout << fixed << setprecision(9) << phi << "\n";
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        int res = solve(1, n, INT_MIN);
        cout << "! " << res << "\n";
        cout.flush();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3720kb

input:

3
5
3
3
2
6
6
3
1
4
3
2

output:

? 1 5
? 1 4
? 1 3
! 4
? 1 6
? 3 6
? 1 2
! 2
? 1 4
? 1 3
! 4

result:

ok Correct (3 test cases)

Test #2:

score: -100
Memory Limit Exceeded

input:

10000
10
2
2
2
2
3
10
10
10
7
5
10
5
5
1
6
10
4
4
4
4
4
10
10
6
3
3
3

output:

? 1 10
? 1 7
? 1 5
? 1 4
? 1 3
! 4
? 1 10
? 5 10
? 7 10
? 5 6
! 6
? 1 10
? 1 7
? 1 5
? 6 7
! 7
? 1 10
? 1 7
? 1 5
? 1 4
? 3 4
! 3
? 1 10
? 5 10
? 1 4
? 1 3
? 1 3
? 1 3

result: