結果

問題 No.1830 Balanced Majority
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2022-02-04 22:28:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 1,805 bytes
コンパイル時間 496 ms
コンパイル使用メモリ 87,304 KB
実行使用メモリ 87,684 KB
平均クエリ数 8.31
最終ジャッジ日時 2023-09-02 05:05:54
合計ジャッジ時間 4,745 ms
ジャッジサーバーID
(参考情報)
judge16 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
87,424 KB
testcase_01 AC 102 ms
87,628 KB
testcase_02 AC 104 ms
87,648 KB
testcase_03 AC 100 ms
87,368 KB
testcase_04 AC 99 ms
87,400 KB
testcase_05 AC 99 ms
87,588 KB
testcase_06 AC 99 ms
86,988 KB
testcase_07 AC 104 ms
87,320 KB
testcase_08 AC 102 ms
87,408 KB
testcase_09 AC 103 ms
87,064 KB
testcase_10 AC 103 ms
87,040 KB
testcase_11 AC 103 ms
87,408 KB
testcase_12 AC 102 ms
87,684 KB
testcase_13 AC 105 ms
87,240 KB
testcase_14 AC 105 ms
87,176 KB
testcase_15 AC 103 ms
87,180 KB
testcase_16 AC 106 ms
87,204 KB
testcase_17 AC 105 ms
87,560 KB
testcase_18 AC 103 ms
87,048 KB
testcase_19 AC 102 ms
87,304 KB
testcase_20 AC 101 ms
87,368 KB
testcase_21 AC 104 ms
86,976 KB
testcase_22 AC 101 ms
87,172 KB
testcase_23 AC 106 ms
87,344 KB
testcase_24 AC 107 ms
87,476 KB
testcase_25 AC 100 ms
87,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

https://yukicoder.me/problems/no/1830

前半と後半で分ける?
まず、0項目をAとする

ラストがBの場合、最初と最後を除いた区間が答え
ラストもAの場合は

真ん中で切る
左右のうち、Bが多いほうを角から二分探索。
A=Bとなる区間を見つける

補集合が答え


"""

import sys
from sys import stdin

N = int(stdin.readline())
askdic = {}
askdic[N] = N//2

def ask(i,side):

    if side == "L":
        if i in askdic:
            return askdic[i]
        print ("?",i,flush=True)
        cat = int(stdin.readline())
        askdic[i] = cat
        return askdic[i]

    else:
        askind = N-i
        if askind in askdic:
            return N//2 - askdic[askind]
        print ("?",askind,flush=True)
        cat = int(stdin.readline())
        askdic[askind] = cat
        return N//2 - askdic[askind]

def getAB(rangenum,askans):

    if l1 == 1:
        return askans , rangenum - askans
    else:
        return rangenum - askans, askans

#N = int(stdin.readline())

l1 = ask(1,"L")
r1 = ask(1,"R")

if l1 != r1:
    print ("!",2,N-1,flush=True)
    sys.exit()

else:

    leftA,leftB = getAB(N//2, ask(N//2,"L") )
    #print (leftA,leftB)
    
    if leftA <= leftB:
        l = 1
        r = N//2
        while r-l != 1:
            m = (l+r)//2
            mA,mB = getAB(m , ask(m,"L") )
            if mA <= mB:
                r = m
            else:
                l = m

        print ("!",r+1,N,flush=True)
        sys.exit()

    else:
        l = 1
        r = N//2
        while r-l != 1:
            m = (l+r)//2
            mA,mB = getAB(m , ask(m,"R") )
            if mA <= mB:
                r = m
            else:
                l = m

        print ("!",1,N-r,flush=True)
        sys.exit()

    
0