Dark Ride

시간 제한1초메모리 제한2048 MB

요약
각 질의가 켜진 방과 꺼진 방 사이의 전환 횟수를 알려줄 때, 30번 이하의 질의로 첫 방과 마지막 방을 제어하는 스위치 두 개를 찾아야 한다.
난이도

어려움10점 중 9점

유형
분할 정복, 비트 연산, 구간
정답자
아직 제출이 없습니다

문제

Erika recently got a summer job at the amusement park Phantasialand near Bonn. She was hired to control the lights in the rooms through which a dark ride passes.

The ride passes through NN rooms, numbered from 00 to N−1N-1. The rooms are traversed in order, starting in room 00 and ending in room N−1N-1. The lights in the rooms are controlled by NN switches (also numbered from 00 to N−1N-1), one for each room. Switch ss (where 0≤s<N0\le s < N) controls the light in room p_sp\_{s}.

Erika's boss has asked her to turn on the lights in the first and last rooms and turn off all the others. Sounds easy, right? She just needs to turn on the two switches AA and BB such that p_A=0p\_{A} = 0 and p_B=N−1p\_{B} = N-1 (or p_B=0p\_{B} = 0 and p_A=N−1p\_{A} = N-1). Unfortunately, Erika did not fully pay attention when her boss described the controls, and she does not remember the array pp -- that is, which switch controls which room.

Erika needs to figure this out before her boss notices. Before the start of each ride, Erika turns off all the lights and can then turn on a subset of the switches. As the ride goes from room to room, whenever the ride goes from a lit room to an unlit room or vice versa, Erika will hear the passengers scream in excitement. The speed of the ride can vary, so Erika cannot directly infer which rooms are lit but at least she will hear the number of screams. That is, she will learn the number of times the ride passes from lit to unlit room, or unlit to lit room.

Can you help Erika figure out which two switches control the lights for the first and last rooms before her boss notices? You can use at most 3030 rides.

제한

  • 3≤N≤30,0003 \le N \le 30\\,000.
  • You can issue at most 3030 rides (printing the final answer does not count as a ride). If you exceed this, you will get the verdict 'Wrong Answer'.

예제3

  1. 예제 1

    입력
    5
    
    3
    
    3
    
    
    예상 출력
    
    ? 10001
    
    ? 10110
    
    ! 2 4
    
  2. 예제 2

    입력
    3
    
    0
    
    2
    
    0
    
    
    예상 출력
    
    ? 111
    
    ? 110
    
    ? 000
    
    ! 1 0
    
  3. 예제 3

    입력
    4
    
    3
    
    
    예상 출력
    
    ? 1010
    
    ! 0 3