ä½ããã®é åºãæã£ãå T: Ord
ããã£ã¦ãããã«é¢ããä½ããã®ï¼å¤éï¼éåã®ä¸å¤®å¤ãæ±ããã©ã¤ãã©ãªãä½ãããã¨ãã¾ãã
ãã¨ãã°åã«é
å a: [T]
ã®ä¸ããä¸å¤®å¤ãæ¢ãã ãã ã£ãããa: [T], b: [T]
ãã a[i] + b[j]
ã¨ãã¦èãããããã®ã®ä¸å¤®å¤ (cf. ARC 037 C) ã ã£ãããéåãä½ããã¼ãã¯å ´åã«ããã¾ãã
ããããï¼ãã¨ãã°ä¸å¦ã»é«æ ¡ãªã©ã§ç¿ãï¼ä¸å¤®å¤ã®å®ç¾©ã§ã¯ã
- è¦ç´ æ° $n$ ãå¥æ°ã®ã¨ããå°ããæ¹ããï¼1-indexed ã§ï¼$(n+1)/2$ çªç®ã®å¤
- ãã¨ãã° $n=3$ ãªã $(n+1)/2=2$ çªç®ã®å¤ã¨ãªã
- $n$ ãå¶æ°ã®ã¨ããå°ããæ¹ãã $n/2$ çªç®ã®å¤ã¨ $n/2+1$ çªç®ã®å¤ã®å¹³åå¤
- ãã¨ãã° $n=4$ ãªã $n/2=2$ çªç®ã¨ $2+1=3$ çªç®ã®å¤ã®å¹³åã¨ãªã
ã®ããã«ãªãã®ãã¡ã¸ã£ã¼ãã¨æãã¾ãã
ä¸æ¹ãã©ã¤ãã©ãªè¨è¨ã®æèã«ããã¦ã¯ããT
ãæ´æ°åã ã£ãã¨ãã¦ããããäºã¤ã®å¹³å㯠T
ã§è¡¨ããã¨ã¯éããªãããå¶æ°ã¨å¥æ°ã§å ´ååãããã®ã¯é¢åããè¿ãå¤ã®åã T
以å¤ã«ããã¨ãªãã¨åä»ããªã©è«¸ã
ä¸é½åãå¤ãã§ãã
ã¨ããã§ãå ã®å®ç¾©ã¯ $n$ ã®å¶å¥ã«ãããã次ã®ããã«è¨ãæãããã¾ãã
- å°ããæ¹ãã $\ceil{n/2}$ çªç®ã®å¤ã¨ $\ceil{(n+1)/2}$ çªç®ã®å¤ã®å¹³åå¤
- ãããã¯ã0-indexed 㧠$\floor{(n-1)/2}$ çªç®ã¨ $\floor{n/2}$ çªç®ã®å¹³åå¤ãªã©
- ãããã¯ã0-indexed 㧠$\floor{(n-1)/2}$ çªç®ã¨ $\ceil{(n-1)/2}$ çªç®ã®å¹³åå¤ãªã©
ããã§ã$n$ ã®å¶å¥ã«ããã $\ceil{n/2}$ çªç®ã®å¤ã¨ $\ceil{(n+1)/2}$ çªç®ã®å¤ã® pair ãè¿ããã¨ã«ããã°ãå¼ã³åºãå´ã§ã®è¨ç®ã«ä»»ãããã¨ãã§ãããã§ãã
è¿ãå¤ã®å㯠(T, T)
ã«ãªãã¾ããåé¡ã«ãã£ã¦ã¯ $\ceil{n/2}$ ã®æ¹ã ããè¦æ±ãããããã¾ããããããããã¨ãã¯çæ¹ãæ¨ã¦ãã ãã§ããã§ãã
ã¾ããRust ã®ãããªè¨èªã§ã¯ãReverse
ã¨ãã wrapper ããããReverse(T)
ã¨æ¸ãã¨éåãã®é åºã§è¨ç®ãã¦ãããããã«ãªãã¾ããããããåã使ãã¤ã¤ãå¿
è¦ã«å¿ã㦠2 åå¼ã³åºãåæ㧠$\ceil{n/2}$ çªç®ã ããè¿ãã¨ããè¨è¨ãããã ã¨æãã¾ã*1ã
ãããã¯ãuse_ceil: bool
ã®ãããªãã©ã°ã使ããããªæ¹éãããã§ããããããã¾ããããããªãæ°ããã¦ãã¾ãããããã¾ããã
$-\infty$ ã«ç¸å½ããå¤ã $+\infty$ ã«ç¸å½ããå¤ã追å ãããã¨ã§å¶å¥ãé©å½ã«åãããã¨ããæ¹éãä¸ç¬èãã¾ããããå¤ãªã³ã¼ãã¼ã±ã¼ã¹ãã§ããã¡ãªæ°ãããã®ã§ãã¾ãããããã¯ãªãã§ããã
ã¨ããã§ã空ã®é
åã®å ´åã¯ä¸å¤®å¤ãåå¨ãã¾ããããããã¯ãè¿ãå¤ã®å㯠Option<(T, T)>
ã«ããã¹ããªã®ã§ã¯ãªãã§ãããããã¨ããæ´¾é¥ãããæ°ããã¦ãããå°ãã¾ãããï¼ç«¶ããæèã§ã¯ãã¾ããããããã¨ã¯ãªãããï¼ï¼ã§ããã¾ããªãã±ã¼ã¹ã§åæã«ãã°ãè¸ãã§æ³£ããã¨ãå¤ãã®ã§ãèãã¦ããå¿
è¦ã¯ããï¼ã
人ã«ã¯äººã®ã©ã¤ãã©ãªè¨è¨ãªã®ã§ãåèã«ãããããªãã£ãããã¦ãã ããã
*1:è¨ç®éç¨ã§ååæ¼ç®ãªã©ãå¿ è¦ã«ãªãå ´åã¯é¢åããã