QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#875945#9734. Identify ChordMax_s_xaMRE 0ms3584kbC++174.4kb2025-01-30 14:40:182025-01-30 14:40:19

Judging History

你现在查看的是最新测评结果

  • [2025-01-30 14:40:19]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3584kb
  • [2025-01-30 14:40:18]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <random>
#include <ctime>
#include <chrono>
#include <numeric>
#include <iomanip>
#include <cassert>

typedef long long ll;
typedef double lf;
typedef unsigned long long ull;

#define DEBUG 1
struct IO
{
    #define MAXSIZE (1 << 20)
    #define isdigit(x) (x >= '0' && x <= '9')
    char buf[MAXSIZE], *p1, *p2;
    char pbuf[MAXSIZE], *pp;
    #if DEBUG
    #else
    IO() : p1(buf), p2(buf), pp(pbuf) {}
    ~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
    #endif
    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
    #define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')

    template <typename T>
    void Read(T &x)
    {
        #if DEBUG
        std::cin >> x;
        #else
        bool sign = 0; char ch = gc(); x = 0;
        for (; !isdigit(ch); ch = gc())
            if (ch == '-') sign = 1;
        for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
        if (sign) x = -x;
        #endif
    }
    void Read(char *s)
    {
        #if DEBUG
        std::cin >> s;
        #else
        char ch = gc();
        for (; blank(ch); ch = gc());
        for (; !blank(ch); ch = gc()) *s++ = ch;
        *s = 0;
        #endif
    }
    void Read(char &c) {for (c = gc(); blank(c); c = gc());}

    void Push(const char &c)
    {
        #if DEBUG
        putchar(c);
        #else
        if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
        *pp++ = c;
        #endif
    }
    template <typename T>
    void Write(T x)
    {
        if (x < 0) x = -x, Push('-');
        static T sta[35];
        int top = 0;
        do sta[top++] = x % 10, x /= 10; while (x);
        while (top) Push(sta[--top] ^ 48);
    }
    template <typename T>
    void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)

using namespace std;

int n;

map <pair <int, int>, int> mp;
inline int Ask(int x, int y)
{
    x = (x + n) % n + 1, y = (y + n) % n + 1;
    if (x == y) return 0;
    if (x > y) swap(x, y);
    if (mp.find(make_pair(x, y)) != mp.end()) return mp[{x, y}];
    cout << "? " << x << ' ' << y << endl;
    int ans; cin >> ans;
    return mp[{x, y}] = ans;
}
inline int Dist(int x, int y) { x = (x + n) % n, y = (y + n) % n; return min(abs(x - y), n - abs(x - y)); }

inline void Answer(int x, int y)
{
    x = (x + n) % n + 1, y = (y + n) % n + 1;
    cout << "! " << x << ' ' << y << endl;
    int ans; cin >> ans;
    if (ans == -1) exit(0);
}
inline bool Guess(int x, int d)
{
    if (d <= 1 || d >= n - 1) return 0;
    if (Ask(x, x - d) == 1) return Answer(x, x - d), 1;
    if (Ask(x, x + d) == 1) return Answer(x, x + d), 1;
    return 0;
}

int main()
{
    #ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
    #endif
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n;
        mp.clear();
        int x = 0, y = n / 2;
        while (Ask(x, y) == Dist(x, y))
        {
            if (y - x == n / 2) y++;
            else x++;
            if (~n & 1) x++;
        }
        int d1 = Ask(x - 1, y), d2 = Ask(x + 1, y);
        if (d1 >= Ask(x, y) && d2 >= Ask(x, y)) { assert(Guess(x, Dist(x, y) - Ask(x, y) + 1) || Guess(x, n - Dist(x, y) - Ask(x, y) + 1)); continue; }
        else if (d1 < Ask(x, y))
        {
            int l = y + 1 - n, r = x - 1, mid, res = x;
            while (l <= r)
            {
                mid = (l + r) / 2;
                if (Ask(mid, y) + Dist(x, mid) == Ask(x, y)) res = mid, r = mid - 1;
                else l = mid + 1;
            }
            assert(Guess(res, Dist(res, y) - Ask(res, y) + 1) || Guess(res, n - Dist(res, y) - Ask(res, y) + 1));
        }
        else
        {
            int l = x + 1, r = y - 1, mid, res = x;
            while (l <= r)
            {
                mid = l + r >> 1;
                if (Ask(mid, y) + Dist(x, mid) == Ask(x, y)) res = mid, l = mid + 1;
                else r = mid - 1;
            }
            assert(Guess(res, Dist(res, y) - Ask(res, y) + 1) || Guess(res, n - Dist(res, y) - Ask(res, y) + 1));
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3584kb

input:

2
6
2
2
1
1
2
1
4
1
1
1
1

output:

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

result:

ok ok (2 test cases)

Test #2:

score: -100
Runtime Error

input:

1000
15
5
6
4
2
2
1
3
1
19
5
6
4
5
3
4
5
5
1
1
17
5
6
4
4
3
4
4
4
1
1
15
6
7
6
2
1
1
14
5
6
4
4
5
3
1
1
15
3
4
2
4
3
4
3
1
1
17
8
8
8
7
7
6
5
5
4
3
3
1
1
20
6
7
7
5
1
1
13
5
4
5
2
2
3
3
3
4
1
1
18
3
2
4
5
3
1
1
13
4
3
5
4
4
1
1
14
2
1
3
3
2
1
17
8
7
7
6
3
3
2
3
3
1
1
12
5
5
4
3
3
2
2
1
1
10
5
5
3
4
...

output:

? 1 8
? 8 15
? 2 8
? 4 8
? 6 8
? 5 8
? 2 5
! 5 8
? 1 10
? 10 19
? 2 10
? 5 10
? 3 10
? 4 10
? 3 17
? 3 8
? 3 12
! 3 12
? 1 9
? 9 17
? 2 9
? 5 9
? 3 9
? 4 9
? 3 16
? 3 7
? 3 11
! 3 11
? 1 8
? 8 15
? 2 8
? 1 14
? 1 3
! 1 3
? 1 8
? 8 14
? 2 8
? 4 8
? 3 8
? 2 13
? 2 5
! 2 5
? 1 8
? 8 15
? 2 8
? 4 8
? 3 ...

result: