1. 程式人生 > >交互題 筆記

交互題 筆記

n-2 不同 arr 個數 may ini lin 格子 整體二分


yyy loves Math VI

記錄兩個數x, cnt,初始x = ?1, cnt = 0。
? 碰到一個數a時:
? 若a = x,則cnt ← cnt + 1
? 否則:
? 若cnt = 0,則x ← a, cnt ← 1
? 否則cnt ← cnt ? 1
? 最後的x就是答案。

US Open 2018 Train Tracking

記錄\(f(i)\)表示區間[i,i+k-1]的最小值的下標的位置,顯然f(i)具有單調性
第一次統計出\(f(\sqrt n \times j)\),第二次按段維護答案
單調隊列優化
In this problem, we are asked to find all sliding-window minima in an array, for some fixed-length window. We are given two passes through the array. The catch is that we are given only O(√N) memory.

Let f(i) be the location of the smallest element in the range [i,i+K), breaking ties by smallest index. Observe that f(i)≤f(i+1) for each i. So if we compute f(0),f(B),f(2B),…,f(N) for some block size B in the first pass, this gives us some information which we may be able to use in the second pass to compute the remaining f(i)s.

Let‘s ignore the constraint on calls to set/get for now. If we let B=√N, then we can compute f(0),f(√N),…,f(N) in our first pass using only O(√N) memory and O(N√N) time. Now, in the second pass, observe that for any fixed array element i, there are only O(√N) windows for which the minimum could possibly be at position i.

This suggests the following algorithm for the second pass: read and ignore all elements with index less than f(0). Now maintain running minima for f(0),f(1),f(2),…,f(√N). Once we‘ve read in the element with index f(√N), we know that we have computed the correct minima for the first √N windows. Output these, start maintaining minima for the next √N windows, and continue in the same fashion.
The memory usage of the second pass is also √N, as desired. Unfortunately, the time complexity of each pass is O(N√N). In particular, the above algorithm would use O(N√N) calls to set and get, which would exceed the given bound. To improve the time complexity, we can use a monotonic queue in each pass. In the first pass, for instance, the boundaries of the √N windows define 2√N subarrays, and for each subarray we can simply compute the minimum of the subarray and put that into the monotonic queue. The length of the monotonic queue therefore never exceeds O(√N), and we can compute the √N minima-locations in O(N) time. For the second pass, it suffices to observe that in each segment of the array (say, between f(i√N) and f((i+1)√N)) all but O(√N) of the array elements are in all √N windows of interest. This once again allows us to improve the pass to O(N) time will not exceeding O(√N) memory.

UR#4 元旦激光炮

每次查詢三個數組中每個數組的k/3的位置的數
把最小的那個和它前面的所有數刪掉
所以每次可以把k*2/3
最多31次可以把k變成2
然後每個數組詢問前兩個數找到最小值

尋找山峰

第一次在正中間豎切一刀
然後找到最大值 然後向兩邊各探索一個格子
如果是山峰=>end
否則在兩邊的比他大的那邊一定有一個山峰
在那一邊橫著切
重復操作
第一次n次查詢
第二次n/2次
第三次n/2次
第四次n/4次
...
n+n/2+n/2+n/4+n/4+n/8...=3n
每一次探索兩個格子 一共2log次
一共4
log次探索

CF727 C Guess the Array

查詢a1+a2,a2+a3,a1+a3
把a1,a2,a3解出來
然後查詢a1+ai得出ai

AIM Tech Round 4 B. Interactive Lowerbound

隨機1000次找到比x小的最大的數
然後從這個數開始向後找1000個
找不到答案的概率是(1-1e3/1e5)^1000=4e-5

CF810 D Glad to see you

我們可以將(1~n)分成兩份(1~mid)(mid+1~r),顯然要猜的數肯定是在這兩個區間內。
然後怎麽判斷我們可以找mid和mid+1兩個數來判斷,如果|mid-a|<=|mid+1-b|,那麽顯然要猜的數肯定是mid或者mid的左側。如果|mid-a|>|mid+1-b|很顯然要猜的數肯定是mid+1或者mid+1的右側然後就是二分,根據題目的回答進行二分

CF862 D Mahmoud and Ehab and the binaru string

先查兩次 0000..0, 100...0
然後可以確定第一位是0/1
假設第一位為0
我們要找一個1
每次把一半的位設成1
比如11111..100000..0
然後與000..000的答案配合
這樣我們可以知道前一半的位置上是1的個數
如果是0個就在後一半查找
否則在前一半查找
log次

CF744 B Hongcow‘s Game

兩種做法

  1. 整體二分
  2. 按位分類
    按位分類的例子:
    給n個數其中兩個數出現了奇數次, 其他的數都出現偶數次
    空間限制O(1) 可以從頭到尾掃log次 找出這兩個數
    sol:
    先掃一遍求出異或和
    然後對於每一位 我們掃一遍找出這一位為0的數的異或和,這一位為1的數的異或和
    然後如果兩個不同的數在這一位上相同 那麽肯定一個是0一個是總的異或和
    如果在這一位上不同(一定存在這樣的一位) 那麽這兩個異或和就是這兩個數

    CF835 E The penguin‘s game

    Effulgence

    GoodBye 2016 F

    Cina National Team Training Tsinghua Training 2015 Flea

交互題 筆記