ã競ããã©ã¤ãã©ãªã«ãããæ½è±¡åã¨è¨ãã°ã»ã°æ¨ãã¿ãããªé¢¨æ½®ãã§ãã¦ããä¹ ããã§ãããäºåæ¢ç´¢ã«é¢ãã¦ããããæ½è±¡åãæèãã¦ãã人ã¯ãã¾ãå¤ããªãæ°ããã¦ãã¾ãã
ãããã¡
ãã¨ãã°ããã½ã¼ããããé åã欲ãããã®æ°æã¡ã®ã¨ãã«ãã¡ãã¡ã½ã¼ãã®å®è£ ãæ¸ããããã®ã¯ãããããªãã¯ãã§ãã main é¢æ°ã«ãã¡ãã¡ã½ã¼ãã®å®è£ ããã¿æ¸ããã¦ãã人ãæ» å¤ã«ããªãã§ãããã
ãããã¯ãã$0$ ä»¥ä¸ $n$ æªæºã®æ´æ°ãé ã«åããããã¨æã£ã¦ããã¨ãã«ãã¾ã $i = 0$ ã§åæåããã$i \lt n$ ã§ããéã$i \xgets+ 1$ ã§æ´æ°ãããã¨ãã âå é¨å®è£ â ãæ¸ãå¿ è¦ãããã®ããããããªãã§ããC++ ç³»ã®è¨èªã使ã£ã¦ã㦠REP ãã¯ãã使ã£ã¦ããªã人ã«ã¨ã£ã¦ã¯ããããããã¿æ¸ãã¯å½ç¶ã®ãã®ã ã¨æããã¦ããã§ãããããã¡ãã£ã¨è½ã¡ã¤ãã¦èãç´ãã¦ã¿ã¦ã»ããã§ã*1ã
äºåæ¢ç´¢ã«é¢ãã¦ãåæ§ã§ãã main é¢æ°å ã§ã®æ°æã¡ã¨ãã¦ã¯ãä½ã ã®æ¡ä»¶ãæºããæ大ã®å¤ã欲ããããªã©ã ãã§ãã£ã¦ããããããããã®å é¨å®è£ ããã¡ãã¡æ¸ãã®ã¯ãããããªãã§ãã
ã¾ããæèã®ã¬ã¤ã¤ã¼ã®è¦³ç¹ã®ä»ã«ããï¼ãã¨ãã°äºåæ¢ç´¢ã®å é¨ã§äºåæ¢ç´¢ãããå¿ è¦ãããå ´åãªã©ã«ï¼å¤æ°åããã¡ãã¡æ°ã«ããå¿ è¦ãããã®ããã¿æ¸ãã®å«ãªãã¤ã³ãã§ãã
競ãã er ããããè¨èª¬ã¨ãã¦ããã¼ã¦ãæ°è¡ã§æ¸ããããã®ãããªãã®ãããã¾ããã5 è¡ã§æ¸ããã®ã¨é¢æ°å¼ã³åºã 1 ã¤ã§æ¸ããã®ã¨ã§ã¯ããã¯ãæ軽ããéãã¨ãæãã¾ãã
æ´çã»è¨è¨
ãã¦ãäºåæ¢ç´¢ã§ãã£ã¦ãããã¨ãèãã¾ãããã ç¶æ³è¨å®ã«ãã£ã¦äºåæ³ã¨å¼ã³åããæ´¾é¥ãããã¾ãããããã§ã¯ âãã®æã®ãã¤â ãã¡ãäºåæ¢ç´¢ã¨ç§°ãããã¨ã«ãã¾ãã
ä¸æ¦ããã§ã¯æ´æ°ã«éã£ã話ããã¾ããçéã§ããè¨ããã¦ãããããªè¨å®ã¯ä¸è¨ã§ãããã
$\gdef\boolty{\{\top, \bot\}}$
- input
- $x_L\in\Z$,
- $x_U\in\Z$,
- $f\colon \Z\to\boolty$.
- precondition
- output
- $x\in[x_L\lldot x_U]$.
- postcondition
- $f(x) = \bot$,
- $f(x-1) = \top$.
ããã§ã$\top$, $\bot$ 㯠true, false ã«ç¸å½ããè¨å·ã§ãã $f(x) = \bot \implies f(x+1) = \bot$ ã¯ãçéã§ãå調æ§ãã¨è¨ãããã¡ãªæ¡ä»¶ã§ãã
ãå調æ§ãï¼
ã½ã¼ãæ¸ã¿ï¼å調å¢å ï¼ã®åããå¤ãæ¢ãã¨ãã«äºåæ¢ç´¢ãæ¢ãã¨ããã®ãåºæ¬çãªç¨æ³ã¨ãã¦ããããããããå調ãã¨å¼ã°ããããã«ãªã£ããã ããã¨æã£ã¦ãã¾ããã ã¨ã¯ããã[true, true, ..., true, false, ..., false] ã¿ãããªé åã称ããå¼ã³æ¹ã¨ãã¦ãå調æ§ãã¯èªç¶ãªãã®ããï¼ã¨ããæ°æã¡ãããã¾ããã
ãªã®ã§ããã調ã¹ã¦ã¿ãæãã ã¨ãããããç¨æ³ããããããããªãã¾ãããããã¨ããæ°æã¡ã«ãªãã¾ããã en.wikipedia.org $\eod$
ã¨ããã§ãäºåæ¢ç´¢ã®ã¢ã«ã´ãªãºã ã§ä¸è¨ã®äºå¾æ¡ä»¶ãæºãããã㪠$x\in[x_L\lldot x_U]$ ãè¿ãããã«ã¯ãå調æ§ã®æ¡ä»¶ã¯å¿ è¦ãªããã¨ã«æ°ã¥ãã¾ãã
çéã§ã¯ãäºåæ¢ç´¢ãç¨ãã¦å¢çãè¦ã¤ããããã«ã¯å調æ§ãå¿ è¦ã§ãããã¨è¨ãããã¡ã§ãããèªåã®è§£éã¨ãã¦ã¯ããäºåæ¢ç´¢ãè¡ã㨠trueã»false ãè·¨ãç®æãä¸ã¤è¦ã¤ãããã¨ãã§ãããå調æ§ãããã°ãããä¸æã§ãããã¨ããããããäºåæ¢ç´¢ã®å®è¡å¯è½æ§èªä½ã«ã¯å½±é¿ããªããã¨ããæãã§ãã
ãããè¸ã¾ãã¦ãä¸è¨ã®ãããªè¨è¨ã§ããã¾ãã$f(x_L) = \top$ ã®æ¡ä»¶ãé¤ãã¦ãã¾ãæ¹ã便å©ãªç¶æ³ãããã®ã§ãããã¾ã*2ã
- input
- $x_L\in\Z$,
- $x_U\in\Z$,
- $f\colon \Z\to\boolty$.
- precondition
- $x_L \le x_U$,
- $f(x_U) = \bot$.
- output
- $x\in[x_L\lldot x_U]$.
- postcondition
- $f(x) = \bot$,
- $x = x_L \vee f(x-1) = \top$.
$f(x) = \top$ ã¨ãªãæ¹ãèªç¶ã«è¦ããæ°ããã¤ã¤ããï¼å ¸åçãªç¶æ³ã¨ãã¦ï¼$[x_L\lldot x)$ ã§ã¯æ¡ä»¶ãããæãã§ã$[x\lldot x_U)$ ã ã¨ãããããªãæãã«ãªããã®ãããªã¤ã¡ã¼ã¸ã§ãã£ã¦ãããã¨ãå¤ããããããè¨è¨ããã¦ãã¾ãã ããã¯åã ã好ã¿ã«åããã¦è¡ãã°ããã¨æãã¾ãã
å®è£
ããããããã§ã$x_L$, $x_U$, $f$ ã渡ãã¨ãä¸è¨ã®äºå¾æ¡ä»¶ãæºãããã㪠$x$ ãè¿ãã¦ããããããªã©ã¤ãã©ãªãä½ããããªã£ã¦ãã¾ãã
主è¦ã©ããã¨ãããã¨ã§ãRust, C++, Python ã®ä¾ãæãã¦ã¿ããã¨æãã¾ãã ã¯ã¦ãªããã°ã«è²¼ãã¤ãã¦ãè¦ã«ããæããããã®ã§ãAtCoder ã¸ã®å®éã®æåºã³ã¼ããè²¼ãã¾ãã
ãã®ä»
å®è£ ã«é¢ãã¦
$x_U$ ã渡ãããææ°æ¢ç´¢ãè¡ãæ¹éã®ã©ã¤ãã©ãªãä½ã£ã¦ãããã§ãããããã³ã¡ããã¯ãããã¡ãã使ã£ã¦ãã¾ãã
æèåæ¢ã§é©å½ãªå®æ°ã® INF
ã使ãããã¯å¤±æãå°ãªãã¨æãã¾ãã
æéå¤ã§ $\infty$ ã代æ¿ããã¨ãã¯ãæèåæ¢ã§ï¼â åé¡ãã¨ã«æ¡ä»¶ãæèããï¼ä½¿ãã®ã§ã¯ãªãã妥å½ãªå¤ã§ãããã¨ãé½åº¦èããå¿
è¦ãããã¨æã£ã¦ãã¾ãã
ä¸éå¤ã«é¢ãã¦
ã§ããã ãããªã¼ãã¼ããã¼ã«æ°ãã¤ããå®è£ ã«ãã¦ããã¾ãããã ããããã©ã¤ãã©ãªå´ã§ãããããç¶æ³ã§ã¯ãã°ããã ãã©ãã¾ãã©ãã競ããã§åé¡ã«ãªããã¨ã¯å°ãªããå¹³æ°ãã®ãããªæ¬ é¥ãæ®ãã¦ããã¡ãªããã¯å°ãªãã§ãã ãã¨ããããæ¬ é¥ããããã¨èªä½ãå¿ããã¡ãªã®ã§ã
æèãããã¨ããªã人ã¯å¤ãã®ããããã¾ãããããã¨ãã° 1_000_000_000_i32
㨠2_000_000_000_i32
ã®å¹³åå¤ãæ±ãããã¨ããéãåã«ãããã足ãåããã㨠i32
ã§ã¯ãªã¼ãã¼ããã¼ãã¾ãã(low + high) / 2
ã§ã¯ãªã low + (high - low) / 2
ã®ããã«ããæ¹éãæåã§ããã«ã¼ãæ¡ä»¶ã«ã high - low > 1
ãªã®ã§ãããããæèã®ãããããããã¾ãã
ã¨ã¯ããããã§æºè¶³ãããã¨ãã§ãã¾ããã-2_000_000_000_i32
㨠2_000_000_000_i32
ã®å·®ããã£ã±ããªã¼ãã¼ããã¼ãã¾ãã
符å·ã¤ãæ´æ°ã®å¹³åå¤ï¼ ã©ãããã®ããããã§ãããã
C++20 ã§ã¯ midpoint
ã¨ããé¢æ°ãç¨æããã¦ãã¾ãã
Rust ã® std ã®å®è£
ã§ã¯ãfn map(a: $SignedTy) -> $UnsignedTy;
㨠fn demap(a: $UnsignedTy) -> $SignedTy;
ãå®ç¾©ããä¸è¨ã®ããã«ãã¦ããããã§ã (core/num/int_macros.rs, int_impl!
)ã
demap(<$UnsignedTy>::midpoint(map(self), map(other)))
符å·ãªãã«é¢ãã¦ã¯ãèªåãã大ããåã«ãã£ã¹ããã¦è¨ç®ããããu128
ã«é¢ãã¦ã¯æå㪠overflow-free ãªã¢ã«ã´ãªãºã ((a ^ b) >> 1) + (a & b)
ã使ã£ã¦ããããã§ã(core/num/mod.rs, midpoint_impl!
)ã
ä¸è¨ã®æåºã³ã¼ãã§ã¯ãé度ã«è¤éã«ãªãã®ãé²ãããã符å·ãªãæ´æ°ã®ãã¨ã ããèãã¦ãã¾ãã
ãæ°æã¡ã«é¢ãã¦
ããããããã¿æ¸ãã§ããã©ã¤ãã©ãªã使ãã®ã§ãããäºåæ¢ç´¢ãçµããå¾ã«äºå¾æ¡ä»¶ãæèããã®ã大äºã§ãã ãããããªããã¨ã«ã¯ãä½åç¹°ãè¿ããã¨ããã§ãåã§ãã£ã¦ãããã®ã¨å¤§å·®ãªãã¨æãã¾ãã
ãã£ã¨ããäºåæ¡ä»¶ã»äºå¾æ¡ä»¶ãæèããå¿ è¦ãããã®ã¯äºåæ¢ç´¢ã«éããããã¹ã¦ã®ã¢ã«ã´ãªãºã ã»ãã¼ã¿æ§é ãå½ã¦ã¯ã¾ãã¨ã¯æãã¾ãã
ãããå¼ã«é¢ãã¦
ã©ã¤ãã©ãªåãã¦ãã¾ãã°ããæ¯åäºåæ¢ç´¢ã§ãã°ããã¦ãã¾ããããããå¼ã使ã£ã¦ãã¾ããï¼ãã®ãããªä¼è©±ããã¡ãã¡ããå¿ è¦ããªããªãã ãããªã¨æã£ã¦ãã¾ãã
åéãããã¦ãããã¨ãå¤ãã§ããããããã ãããå¼äºåæ¢ç´¢ã®åå
¸ ã¯ã[low, high)
ãåéåºéã§æã¤ãã¨ã ããæãã¦ããã®ã§ã¯ãªãã§ãã
å¤æ°åã§åºéã [ok, ng)
ã»(ng, ok]
ã®ããã«æ示ããã©ã¡ãã®å ´åã§ãã«ã¼ãã®å¼ã abs(ok - ng) > 1
ã®ããã«çµ±åãã¦ãã¾ããã®ã§ããªããªãéæ¿ãªã¹ã¿ã¤ã«ã§ãã
mid = (ok + ng) / 2
ã¯ãªã¼ãã¼ããã¼ãæãã§ããã㨠if (solve(mid))
ã®é¨åã¯ããã¾ããã¾ããªãå称ãªæ°ããã¾ããããã¯ãã³ã¡ããã®ææ
ã§ãã
ãããããã«ã¶ããã®ä¸çã»ä¸çã®é¨åããåºéãã¨æããã®èªä½ããã³ã¡ããã®æè¦ã¨ãã£ã¦ãªãããï¼
ã©ã¤ãã©ãªã»ã¹ããããã«é¢ãã¦
ã«ã¶ããï¼ãããã®ä»ã®ã¢ã«ã´ãªãºã ï¼ã®ãã³ãã¬ã¨ãã¦ä¸è¨ã®ãããªã¹ãããããé½åº¦è²¼ãã¤ãã¦ãã人ãããã§ãããã
let (mut ok, mut bad) = (0, INF); while bad - ok > 1 { let mid = ok + (bad - ok) / 2; let mid_is_ok = { // ããã«å®è£ ãæ¸ã }; *(if mid_is_ok { &mut ok } else { &mut bad }) = mid; }
ããããããæ¹ãããã¾ãããã¨ä¸»å¼µããæ°ã¯ãªãã§ããããã¾ã好ã¾ããã¨ãæã£ã¦ãã¾ããã ããã®ãããªå¼æ°ã渡ãã¨ããããããã®ãè¿ããã¨ããå½¢å¼ã§æ´çã§ãã¦ããªããã®ã»ãããé£ãããã®ã«é¢ãã¦ã¯ããã§å¦¥åããã®ãããã§ãããäºåæ¡ä»¶ã»äºå¾æ¡ä»¶ã®æèãªã©ããã«ãããªãããã¨ããæ°ããã¦ãã¾ãã
åæ§ã®ãã®ã¨ãã¦ãBFS ãªã©ãé©åã«æ½è±¡åã§ããã¨ããããããããã¾ãã*3ã
é¡é¡
ãã¿ãã¬ã«ãªãã®ã§ãã¾ã好ã¾ãããªãããã§ã¯ããã¾ããããããã ABC ã®åé¡æ°ã¯ããããããã®ã§ãæ¹éãããã£ãä¸ã§ãã¡ãã¨å®è£ ãè©°ããç¨ãã¨ãæ¹éãèªåã§èããç¨ãã§åãã¦ä½¿ã£ã¦ãã¾ã£ã¦ãããæ°ããã¾ãã
ãã£ã¨æ¢ããæãã§ããABC 300 以é㧠2 å²ãããã®ã³ã³ãã¹ãã§ã¯åºã¦ããããã§ã常é£ããã ãªãã¨ããæ°æã¡ã§ãã ã¨ããã§ãABC 300 ããã§ã« 1 年以ä¸åã¨ãããã¨ã«æ°ã¥ãã¦ã³ã£ãããã¦ãã¾ãã¾ããã
- ABC 302 D
- ABC 311 E
- ABC 312 C
- ABC 313 C
- ABC 318 F
- ABC 319 D
- ABC 321 D
- ABC 330 F
- ABC 341 D
- ABC 346 F
- ABC 353 C
- ABC 364 D
- ABC 365 C
ææ
ãã¿æ¸ããããã人ã ãæ¹å®ãããã®ã¯å¤§å¤ããã
æè¿ã¯ã©ããããããªãã§ããã競ããçéã®ä¸é¨ã§ã¯ããã½ã³ã³ãè¨èªä»æ§ã«è©³ãããªãããããããã®ã使ã£ã¦é«åº¦ã«ä¾¿å©ãªã©ã¤ãã©ãªã»ãã¼ã«ãæºåãããã¨ã«å¯¾ããå¿é¿æããããã®ãããªé¢¨æ½®ããã£ãããã«æãã¾ãã
oj
ããã®é¡ã®ãã¹ããã¼ã«ã使ããã«æä½æ¥ã§ããã°ã£ã¦ãã人ã¯ã¾ã ã¾ã ãããããããããªæ°ããã¾ã*4ã
ããã
ããæãã®æ¥ææ¥ã
*1:å¥ã« REP ãã¯ããæ¨å¥¨ãããããã§ã¯ãªãã§ããããã¿æ¸ããæ¨å¥¨ãããããã§ãããã¾ããã
*2:$f(x_L-1) = \top$ ã§ããã¨è¦ãªãã¦ããã¨ããè¦æ¹ãããããã
*3:ãããã¡ãªã°ã©ãã«éã£ã BFS ã¨ããæå³ã§ã¯ãªããä¸è¬ã®æ¢ç´¢ã¨ãããã¨ã§ãã
*4:ããããã°ããã ã«ã¬ã¹ããªãã¦ããªããã