2019-05-01ãã1ã¶æéã®è¨äºä¸è¦§
é å¼µã£ã åé¡æ¦è¦ åã®æååãä¸ããã㦠ãã®ãã¡ã®æå®ããã åã«ã¤ãã¦ã«ã¤ãã¦ã¯ããã® prefix ã¨ãªã£ã¦ãã æå®ããã¦ããªã åã«ã¤ãã¦ã¯ prefix ã«ã¯ãªã£ã¦ããªã ãããªæçã®æååãæ±ãããåå¨ããªãå ´å㯠-1 ã¨ããã å¶ç´ åã®æååã®é·â¦
åºéåå²ç³»åé¡ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ æååãä¸ãããããæååãæå°åæ°ã®é£ç¶åºéã«åå²ãã¦ãååºéã®å é æåããã®åºéå ã®ä»ã®ä»»æã®æåãããè¾æ¸é ã§å°ãããªãããã«ããã èããã㨠Greedy ã«åºéãåå²ãã¦ããã° OKã åºéãåå²ãã¦â¦
æé«ã«å¥½ããªåé¡ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é ç¹æ° ã®ããªã¼ä¸ã§ã²ã¼ã ãè¡ããé«æ©åã¯æ®ã£ãé ç¹ãã 1 ã¤é¸ãã§ç½ãå¡ããéæ¨åã¯æ®ã£ãé ç¹ãã 1 ã¤é¸ãã§é»ãå¡ãã ãã®æä½ããã¹ã¦ã®é ç¹ã«è²ãå¡ãããã¾ã§ç¹°ãè¿ããã¨ããé»ãå¡ããããã¹ã¦ã®é â¦
楽ããã£ãã®ã§ã åé¡æ¦è¦ 1 ãã 13 ã¾ã§ã®æ°åãæ¸ãããã«ã¼ãã 1 æãã¤ããããããããã·ã£ããã«ãã¦å±±æã¨ãã¦ä¸¦ã¹ã¦ã1 æãã¤å¼ãã ãã®ã¨ããå¼ããã«ã¼ãããææã«ã¼ãã®æ°å¤ã®æ大å¤ããããå°ããã£ãããã®ã«ã¼ããæ¨ã¦ãããã§ãªãã£ãâ¦
ãã®åé¡ã解ããåå ã¯ããå®é¨ããããã¨æãããã¨ãªæ°ãããã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é«æ©åã¯ï¼ããç¹æ®ãªè£ ç½®ãããããæã£ã¦ãã¾ãï¼ ãã®è£ ç½®ã¯çç¶ã§ï¼å·¦å³ãããã¼ã«ãå ¥ãããã¨ãã§ãã¾ãï¼ ã¾ãï¼ãã®è£ ç½®ã«ã¯ 2 種é¡ã®ç¶æ A, B ãããã¾â¦
é¢ç½ãã£ã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ åã®é·æ¹å½¢ããããå辺ã®é·ãã¯æ´æ°å¤ã§ããã ãã®é·æ¹å½¢ããä½åãé¸ãã§ãããªã§ã¼ã·ã«ãä½ããããæ大ã§ä½éã«ã§ãããï¼ é·æ¹å½¢ã®ç¸¦ã¨æ¨ªãã²ã£ããè¿ãã¦ãããã å¶ç´ èããã㨠ããé·æ¹å½¢ã縦横ã²ã£ããè¿ãâ¦
ããããã®ã»ããã§ä¸çªé£ããã£ãã ããããã®ãã°ã©ãã§èããã®ã¯å ¸åã¨è¨ãã°å ¸åãã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ çµã®æ´æ°çµ ããããããããã®çµããæ´æ°ãé¸ãã 種é¡ã®æ´æ°ã«å«ã¾ããæ´æ°ã®ç¨®é¡æ°ã®æ大å¤ãæ±ããã å¶ç´ èããã㨠æ°å¤ããã¼ãã«â¦
ãæ¢ç´¢åè£ã¯å®ã¯ããå¤ããªããã¨ããæè²çåé¡ï¼ï¼ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ çµã®æ´æ°ã㢠ããããåãã¢ããæ´æ°ãã©ã¡ãããé¸ã¶ããããã¦é¸ã°ãã åã®æ´æ°ã®æå¤§å ¬ç´æ°ã®æ大å¤ãæ±ããã å¶ç´ èããã㨠ç´æ°ã¨è¨ããã¦æãæµ®ãã¹ãã¹ããã¨ã®â¦
é¢ç½ãã£ã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ åããé£ã¹ç©ããæå¼·ã®é£ã¹ç©ã決ããããåé£ã¹ç©ã¯ AP (æ»æå)ï¼ HP (ä½å)ï¼ ãããªããé£ã¹ç© 㯠ãæºããã¨ãã 㯠ããå¼·ãã¨ããããããã®å¤ãçããå ´åã¯å¼ãåãã§ããã 種é¡ã®é£ã¹ç©ã®ãã¡æå¼·ã®ãã® (ä»â¦
çºæ³ä¸çºã²ã¼ï¼ï¼ï¼æ¥½ãã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ åã®ä»¥ä¸ã®åé¡ã«çãã¦ãã ããã æ´æ° ãå²ã£ãä½ãã¨æ´æ° ãå²ã£ãä½ããçãããªããããªæ£æ´æ°ã®ãã¡æ大ã®ãã®ãæ±ãã å¶ç´ èããã㨠ã ã£ãããä»»æã®æ´æ°ãæ¡ä»¶ãæºããã®ã§ã-1ãããã¯ãã â¦
大å¤ã ã£ã ^^; åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ å ¨é¨ã§ è²ã®è²ãããã ä¸å³ã®ãã㪠æã®æ£æ¹å½¢ããã£ã¦ããããã ã®ã©ãã«ãã¤ãã¦ãã¦ãåæ£æ¹å½¢ã®åé ã«ã¯ããããã®è²ãå¡ããã¦ããã æã®ä¸ãã 6 æãé¸ãã§ç«æ¹ä½ãä½ãããã ãã©ã®é ç¹ã«ã¤ãã¦ããããâ¦
ã¡ãã£ã¨ããæ§ç¯ç³»åé¡ã¨ããæãããª... åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ äºæ¬¡å å¹³é¢ä¸ã®äºç¹ ãä¸ããããã ã¯ãã¹ã¦æ´æ°ã§ããã ä»ã ããåºçºãã¦ãä¸ä¸å·¦å³ã« 1 ç§ã« 1 ã ãåããã¨ããåä½ãç¹°ãè¿ããªãã ã«è¡ããã¾ãããããåºçºã㦠ã«æ»ããããã«â¦
ãããå ¸åçãªåé¡ã ç¾ä»£ãªãä¼æ¥ã³ã³ã® 800 ç¹åé¡ã¨ãã«åºãããªé°å²æ°ã ãããã®ããã¯ã¾ã ãã¾ãå ¸åãããªãã£ãã®ããªã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ ä¸ç´ç·ä¸ã« åã®ç¹ ããã£ã¦ãã®é ã«ä¸¦ãã§ãããããã«å·¦å´ã« ãå³å´ã« ãããã ããã¹ã¿ã¼ããã â¦
600 ç¹åé¡ã¨ããªãã¨ããããã«æ£è§£è æ°ãå°ãªãã è²ããªäººãè²ããªæ§ç¯ãã¦ããã ãã©ãåãªãã®æ¹æ³ãã°ã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ ã 以ä¸ã®æ´æ°ã¨ããã ä»¥ä¸ ä»¥ä¸ã®æ´æ°ã 2 åãã¤ãã£ã¦ãããã並ã¹æ¿ãã¦ã§ããé·ã ã®æ°å ã§ãã£ã¦ã ã¨ãªãããâ¦
ããã¾ã Union-Find ãç¨ããæè²çåé¡ï¼ï¼ï¼ï¼ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ æã®ã«ã¼ãããã£ã¦ããããã 1 ã 2 ã®æ°å¤ãæ¸ããã¦ããã æã®ã«ã¼ãã®æ°å¤ ããã¹ã¦å½ã¦ããã ç¾å¨ã以ä¸ã®å½¢å¼ãããæ¡ä»¶ã åä¸ããã¦ãã: æ´æ° ã«å¯¾ãã¦ã ã¯å¶æ°ã§ãâ¦
ã¨ã¦ãæè²çãªããªã¼ã®æ¢ç´¢åé¡ã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é ç¹ã®éã¿ä»ãç¡åã°ã©ããä¸ããããããã®ã°ã©ãã®åé ç¹ãç½é»ã«å¡ãæ¹æ³ã§ãã£ã¦ åè²ã«å¡ããã 2 é ç¹ã¯ã©ããé¸ãã§ãããã®éã®ãã¹ã®é·ãã¯å¶æ°ã§ãã ã¨ããæ¡ä»¶ãæºãããã®ãæ±ããâ¦
確çã確å®ã« åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ æ£ã®æ´æ° ãä¸ããããã æåã« ã®æ´æ°å¤ãã©ã³ãã ã«åºåããã以éã³ã¤ã³ãæ¯ã£ã¦ 表ãåºããæ´æ°å¤ã 2 åãã ( 以ä¸ã«ãªã£ãããåã¡ãã§ã²ã¼ã çµäº) è£ãåºããæ´æ°å¤ã 0 ã«ãã (ãããªã£ãæç¹ã§ãè² ããã§â¦
å½æã¯è§£ããªãã£ããã©ãäºé ä¿æ°ãæ±ãã¹ãã«ãæ ¼æ®µã«é«ããä»ãªã解ããï¼ï¼ï¼ ã¨ããã®ã¯ç½ ã§ããçµè·¯æ°ã«å¸°çãããã¨ããèãæ¹ããã®åé¡ã§å¦ãã ^^; åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ åã®æ£ã®æ´æ°å¤ã®ã㢠ãä¸ããããã ã®å¤ã 109 + 7 ã§å²ã£ããã¾ããâ¦
楽ããã£ãã åæç³»ã®åé¡ããããªæ¡ä»¶ã ã¨çµå±å ¨é¨ä¸ç·ã«ãªããã¨ããåé¡ã®é°å²æ°ãã©ãã¨ãªãæ°ãªãªã£ã½ãã å¿ è¦æ¡ä»¶ãé å¼µã£ã¦åæãããå®ã¯ååãªæããã¾ããæ°ãªãªã£ã½ã㨠AGC ã£ã½ãã®ä¸¡æ¹ãããæãã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ ç·åã ãé·ããâ¦
ãã¢ãªã³ã°ãé å¼µãåé¡ã¨ãã¦é«é£æ度ãªããã楽ããåé¡ã çµæ§æ³¨æåããããå²ã¨ãã°ãã±ã¼ã¹ãããã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ ä¸è¨ã®ãããããã®åæ°ããããããããããããããããå転㯠OK ã§å転㯠NG ã§çµã¿åãã㦠ã®é·æ¹å½¢ãä½ããããä½ãâ¦
ããã® Easy ãã¼ã¸ã§ã³ãããã楽ããã¦å¥½ãï¼ï¼ï¼ï¼ï¼ Easyï¼æä½ã®çµæãææã«ãªãå ¥åãæ±ããéåé¡ Hardï¼æä½ã«å ¥åãä¸ããçµæãæ±ããåé¡ ã¨ãã風ã«ãªã£ã¦ãã¦ãæ®éãæä½ã®çµæãæ±ããã ããã®åé¡ã®æ¹ã絶対簡åãªãã¨ãå¤ãã®ã«ãEasy 㨠â¦
ãã300 ç¹ï¼ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ åã®ä¸ããã©ã³ãã ã«é¸ã¶è©¦è¡ã åè¡ããããããã®ãã®ãåºãåæ°ã åã«ãªã確çã ã¨ããã¨ãã ãæºãããããªæå°ã® ãæ±ããã å¶ç´ èããã㨠確çãæ±ãããã¨èªä½ã¯é£ãããªãã¦ã ã§æ±ããããããããâ¦
ããã§ã²ã¨ã¾ã ABC 100 以éã® CD åé¡ã¯å ¨é¨æ¸ãã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ åã®ãåºããã£ã¦ãååº ã§ã¯ 1 æ¬ åã®ã¨ãã¸ã¼ããªã³ã¯ã æ¬ã¾ã§è²·ããã¨ãã§ããã å ¨é¨ã§ æ¬ã®ããªã³ã¯ãè²·ããããæå°ã§ä½åã§å®ç¾ã§ãããï¼ å¶ç´ èããã㨠åºæ¬çã«å®â¦
å¦ã³å¤ãåé¡ã åã«ã¨ã£ã¦ã¯å¾åã®ãã¼ã¿æ§é ãã¼ããè¦æ¦ãå¼·ããããæ¬å½ã«åå¼·ã«ãªã£ãï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ åã®å¯¿å¸ããã£ã¦ããããããã¿ ã¨ç¾å³ãã ããã£ã¦ããããã®ä¸ãã åã®å¯¿å¸ãé¸ã³ãããé¸ãã 寿å¸éåã®ã¹ã³ã¢ã¯ é¸ãã 寿å¸ã®ç¾â¦
æ°ã¥ãã°ãããªæãã㪠åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ æ¬ã®ãã¼ãããã£ã¦ããããã®é·ã㯠ã§ä¸ãããããæåããã¼ã ã®å³ç«¯ã¨ãã¼ã ã®å·¦ç«¯ãçµã°ãã¦ãããä»ã é·ãã 以ä¸ã®ã²ã¨ã¤ãªããã®ãã¼ããé¸ã³ããã®ä¸ã«çµã³ç®ãããã°ã©ãã 1 ã¤é¸ãã§ã»ã©ã â¦
ãã¾ãã·ãã¥ã¬ã¼ã·ã§ã³ããç³» åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ N åã®ç®±ãããã¾ãã 箱㯠1 ãã N ã¾ã§çªå·ãæ¯ããã¦ãã¾ãã æåã1 çªç®ã®ç®±ã«ã¯èµ¤ããã¼ã«ã 1 åå ¥ã£ã¦ãã¾ãã ã¾ãã2ï½N çªç®ã®ç®±ã«ã¯ç½ããã¼ã«ã 1 åãã¤å ¥ã£ã¦ãã¾ãã é«æ©å㯠M åâ¦
楽ããã£ã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã®æ°å ãä¸ãããããã©ã® 2 è¦ç´ ãäºãã«ç¸ç°ãªããã¨ãä¿è¨¼ããããããã«å¯¾ã é£ãåã 2 è¦ç´ ã swap ãã é£ç¶ãã 3 è¦ç´ ãå転ãã ã¨ããæä½ãè¡ã£ã¦ã½ã¼ããããããã ãã1 ã®ä½¿ç¨åæ°ãæå°ã«ããããâ¦
ããã楽ããã£ãã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ è² ã®ãã¼ã«ããããã åãã¤ããã¾ãã åã®ãã¼ã«ã好ããªé åºã§ä¸¦ã¹ãåè²ã«ã¤ãã¦æãå·¦å´ã«ãããã¼ã«ãè² ã¸ã¨å¡ãæ¿ãã¾ããã æçµçãªè²ã®ä¸¦ã³ã¨ãã¦èããããåæ°ã 1000000007 ã§å²ã£ããã¾ããæ±â¦
ãããã O(1) ãªãã¢ãªã³ã°ãé å¼µãç³»ã®åé¡ãæ¬å½ã«è¦æããã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ ãã¬ãåã¯æåããã¹ã±ããã 1 ææã£ã¦ãããæ¥æ¬åã¯æã£ã¦ãã¾ããã ãã¬ãåã¯ã以ä¸ã®æä½ã好ããªé ã«åè¨ã¡ããã© K åè¡ãã¾ãã æã£ã¦ãããã¹ã±ãããâ¦
å帰é¢æ°ã®ä½¿ãæ¹ã«æ £ããããã®æè²çåé¡ã ãã®æã®å帰çãªãã©ã¯ã¿ã«ã£ã½ãæ§é ãèªã¿è§£ãåé¡ã¯ãåååæ°ãéããã¨æåã®æ¹ã®æ¨¡æ§ãåæãããã¿ãããªãã¨ãããã¤ã¡ã¼ã¸ããããã©ãä»åã¯ããããã®ãããªãã¦ç´ç²ã«å帰é¢æ°ã®ä½¿ãæ¹ãåãããåâ¦