æ°å
ä¸æã«å¼å¤å½¢ãããï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ æ£ã®æ´æ°ãããªããµã¤ãº ã®æ°å ãä¸ããããã次ã®æ¡ä»¶ãæºããçµ ã®åæ°ãæ±ããã å¶ç´ èããã㨠æ¡ä»¶ãä¸æè°ã ãæ®é㯠ããã ã®æ¹ã大ãããã¨ãå¤ãã®ã«ã ã¨ãªãæ¡ä»¶ãåãã¨ã¯ã ãã¦ããã®æã®æ°â¦
æ¡ä»¶ãä¸æã«è¨ãæãããï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ åã®æ´æ° ãä¸ããããã ãå¹³æ¹æ° ã¨ããæ¡ä»¶ãæºãããããªçµ ã®åæ°ãæ±ããã å¶ç´ èããã㨠ããã¯ãæ¡ä»¶ã®è¨ãæãããé å¼µãåé¡ãã ãå¹³æ¹æ°ãã¨ããæ¡ä»¶ãæ±ããããããã«è¨ãæãããããâ¦
ãåºéã®å¤ã®åããè¦ãããç´¯ç©åãã¨ããï¼ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ æ°å ã¨æ´æ° ãä¸ããããã æ°åã®é£ç¶ããåºéã§ãã£ã¦ããã®ç·åã ã«ä¸è´ãããã®ãä½åããããæ±ããã å¶ç´ èããã㨠0-indexed ã§èããã æ°å ã®ç´¯ç©åã ã¨ãããããã®â¦
é£æ³é åã®è¯ãç·´ç¿åé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ æ£ã®æ´æ°ãããªãæ°åãè¯ãæ°åã§ããã¨ã¯ãæ°åã«å«ã¾ããä»»æã®æ´æ°å¤ ã«ã¤ãã¦ãæ°åä¸ã« ãã¡ããã© åå«ã¾ãã¦ãããã¨ãããã ä¸ããããæ°å ã«å¯¾ãã¦ãããã¤ãã®è¦ç´ ãåé¤ãããã¨ã§ãããæ°â¦
æå¤ã¨æ´çããã®ã大å¤ã ã¨æããmap ã使ã£ã¦æ´çãããã¨ããã®ãèªç¶ã ããããã¡ãã£ã¨ç°¡åã«è§£ããã¨ãèãããã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ æååãæ稿ããã¨å¾ç¹ãä¸ããããã³ã³ãã¹ãã«ã åã®æåå ããã®é ã«æ稿ããããããã ç¹ãä¸ãããâ¦
ã¡ãã£ã¨ããèå¯ãå¿ è¦ã«ãªãåé¡ã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ æ´æ°ãããªãæ°å ãä¸ãããããæ°åã®åè¦ç´ ã«å¯¾ãã¦ãä½ãããªããã1 ã足ããã1 ãå¼ããããã¦ããã æä½å¾ã«ãæ´æ°å¤ ãé¸ã¶ã ã¨ãªã ã®åæ°ã®æ大å¤ãæ±ããã å¶ç´ èããã㨠åé¡â¦
åºéã伸ã°ããã縮ãããããªãããããã«ä¼´ããæ¿å ¥ãããåé¤ãã«å¯¾å¦ãããã¼ã¿æ§é ãèããç³»ã®åé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ æ°å ã«ã¤ãã¦ãé£ç¶ãã åã®è¦ç´ ã®ç¨®é¡æ°ã®æ大å¤ãçããã å¶ç´ èããã㨠æ°åã®å¹ ã®åºéããã¹ã¦èª¿ã¹ãã«ã¯ã次ã®â¦
é£æ³é åã®ç·´ç¿åé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã®æ´æ°å ãä¸ããããããã®æ°åã«å¯¾ãã¦ã次㮠åã®ã¯ã¨ãªã«çããã ãã¯ã¨ãªã æ´æ° ãä¸ããããã®ã§ãæ´æ°åãé ã«è¦ã¦ãã£ãã¨ãã®ã åç®ã®å¤ ã®è¦ç´ ãã® index ãçããï¼åå¨ããªãå ´å㯠-1ï¼â¦
ããé¢ç½ãã£ãï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ æ°å ãä¸ããããããã®æ°åã®é£ç¶ããé¨åæ°åã«ã¤ãã¦ããã®ç·åã ã§å²ã£ãä½ãããèããã é£ç¶ããé¨åæ°åããã¹ã¦èããã¨ãã®ãããã®ç·åã ã§å²ã£ãä½ããã®ç·åãæ±ããã å¶ç´ èããã㨠ãã®åé¡â¦
map ã®ç·´ç¿åé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã®æ°å ãä¸ããããã å ã«ã¤ãã¦ã ã㤠ãæºããæ大ã®æ´æ° ãæ±ããï¼åå¨ããªãå ´å㯠-1ï¼ã å¶ç´ èããã㨠åºæ¬çã«ã¯æ¬¡ã®æ å ±ã管çã§ããé åã欲ããã last[v]ï¼ãã®æç¹ã§ã ãæºããæ大㮠â¦
ã¹ã¿ãã¯ã®å¿ç¨åé¡ï¼ ãã¹ãã°ã©ã ã®æ大é·æ¹å½¢ãæ±ããã¢ã«ã´ãªãºã ã¨ä¼¼ããããªã¹ã¿ãã¯ã®ä½¿ãæ¹ãããï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã®æ°å ãä¸ããããããããã®å¤ã¯äºãã«ç¸ç°ãªãã å ã«å¯¾ãã¦ã次ã®æ¡ä»¶ãæºããæ´æ° ã®åæ°ãæ±ããã ãªãä»»â¦
ãååã®å¤ããä¿æããªããã·ãã¥ã¬ã¼ã·ã§ã³ããç³» åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ ãã¿ã³ãæ¼ãã¨ã1 åã®é£´ãããããããåå飴ãããã£ã¦ãã ç§éã¯ããããªãã é«æ©å㯠åãã¿ã³ãæ¼ããããããã ç§å¾ã«æ¼ããï¼ãããã¯å調å¢å ï¼ã ä½å飴ãããããâ¦
ãã®æ代ããã®æã® Greedy ã¯ãããããã£ãã®ãï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã®æ°å ãä¸ããããããæ°åã®è¦ç´ ã 1 ã¤é¸ã³ã+1 ãããã-1 ãããã¨ããæä½ãè¡ããã¨ã§ã次ã®ç¶æ ãéæãããããã ãã ã¨ããã ãã¹ã¦ã® ã«å¯¾ãã¦ã ã§ãã ãã¹â¦
bit å ¨æ¢ç´¢ãæå¤ã¨æãæµ®ãã³ã¥ãããããããªãã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ åã®æ£ã®æ´æ° ãä¸ããããããããã®æ´æ°ã A, B ã® 2 ã°ã«ã¼ãã«åããã ã®æ大å¤ãæ±ããã å¶ç´ èããã㨠bit å ¨æ¢ç´¢ã使ãã¨ã åã®ãã®ã«å¯¾ãã¦ã0ãã1ãã®å¤ãå²ãå½â¦
ã³ã³ãã¹ãä¸æåã«æãã ã解ããããã®å¾ãçµå±è§£ããªãã£ããgcd convolution ãæãåºããªãã£ãã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã®æ£ã®æ´æ°ãããªãæ°å ã ãä¸ããããããããã®æ°åã«å¯¾ã㦠åã®ã¯ã¨ãªãä¸ããããã ãã¯ã¨ãªã æ£ã®æ´æ° ãä¸ãâ¦
è¨ç®éæ¹åã®ãã¨ãæ¬å½ã«ç¥ããªãã¨ãTLE ã«æ©ããããããªãã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã® 2 ã¤ã®æ°å ã ãä¸ããããã 1 ä»¥ä¸ ä»¥ä¸ã®æ´æ° ãé¸ãã§ã ã®æ大å¤ãæ±ããã å¶ç´ èããã㨠æãç´ ç´ã«èããã¨ã次ã®ããã«äºé for æãç¨ãã¦è§£ãâ¦
for æã®ç·´ç¿ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ åã®æ´æ° ã®ãã¡ãæ大å¤ã¨æå°å¤ãé¤å¤ãã åã®æ´æ°ã®ç·åãæ±ããã å¶ç´ åã®æ´æ°ã¯ãã¹ã¦äºãã«ç¸ç°ãªã è§£æ³ ã¾ã åã®æ´æ°ããé åãã¨ãã¦åãåãã¾ããã (C++ ã§ã¯ vector<int> åãªã©)ã ãã®å¾ãé åã«å¯¾ã</int>â¦
座æ¨å§ç¸®ãããã¨ããåé¡ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã®æ°å ãä¸ããããã®ã§ã座æ¨å§ç¸®ããã å¶ç´ èããã㨠座æ¨å§ç¸®ã¯æ¬¡ã®è¨äºã«è©³ããæ¸ããã drken1215.hatenablog.com ã³ã¼ã #include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; v</bits/stdc++.h>â¦
ã¹ã¿ãã¯ãç¨ãã¦è§£æ±ºã§ããå ¸ååé¡ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã®æ°å ãä¸ããããã å ã«å¯¾ãã¦ã ã㤠ãæºããæ大㮠ãæ±ãã (åå¨ããªãå ´å㯠-1)ã å¶ç´ ã¡ã¢ ã¹ã¿ãã¯ã使ã㨠ã®è¨ç®éã§è§£ããã詳細ã¯éåæ¬ãåç §ã ã³ã¼ã #include <bits/stdc++.h> usi</bits/stdc++.h>â¦
ãã®æã®ã¹ã¿ãã¯ã®åé¡ã¯ãããã£ãã常èã¨åããï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ ãã« ããã®é ã«ä¸¦ãã§ããããã« ã®é«ã㯠ã§ããã å ã«å¯¾ãã¦ããã« ã®éã«ãã« ããé«ããã®ããªããã㪠() ã®åæ°ãæ±ããã å¶ç´ èããã㨠ããã«ãã¹ã¿ãã¯ã使ããâ¦
ãã¡ãã¨ããæç¶ããçµã¦æ±ãããã¨ãã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ åã®æ´æ°ãããªãæ°å ãä¸ããããããã®æ°åã«æ¬¡ã®æä½ãç¹°ãè¿ãã¦ãæ°åã®é ããã¹ã¦ç¸ç°ãªãããã«ããããã§ããããæ°åã®é æ°ã®æ大å¤ãæ±ããã ãæä½ã æ°åãã 3 åã®æ´æ°ãé¸â¦
ãé¢ä¿ãªããããï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é«æ©åã¯è¡ ã®é ã«è¨ªãããè¡ ã§ã¯ãããã®ä¾¡å¤ã¯ åã§ããã é«æ©åã¯è¡ 㧠åã§ãããã好ããªæ°ã ãè²·ããã¨ãã§ãã¦ã ãæºããè¡ ã§ãã®ãããã好ããªæ°ã ã åã§å£²ããã¨ãã§ããããã ããé«æ©åã¯ããâ¦
å¤é for æã®ç·´ç¿ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ 2 ã¤ã®æ°å ã ãä¸ãããããæ°å ãããããã 1 åãã¤é¸ãã§ã§ãã åã®ãã¢ã«ã¤ã㦠ããã®åãã¨ããã®æ大å¤ãã®ç© ãæ±ãã¦ããããã®ç·åãæ±ããã å¶ç´ èããã㨠2 ã¤ã®æ°åããããããè¦ç´ ãã¨â¦
for æãåãå¦çã åããåé¡ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ åã®æ´æ°ãããªãæ°å ãä¸ããããã次㮠åã®ã¯ã¨ãªã«çããã ãã¯ã¨ãªã æ´æ° ãä¸ããããã ã ã«å¤æ´ããã¨ãã®ã ã®å¤ãçãããï¼ãªããã¯ã¨ãªãã¨ã«å¤æ´ã¯å¼ãç¶ãããªããï¼ èããã㨠â¦
second best ãæ±ããç³»ã®åé¡ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ åã®ç¸ç°ãªãæ´æ° ãä¸ããããã ãã®æ°åã®ãã¡ 2 çªç®ã«å¤§ãããã®ã«ã¤ãã¦ãããã ã®ä½çªç®ã®è¦ç´ ã§ããããæ±ããã å¶ç´ è§£æ³ (1)ï¼max 㨠second max ã管çãã 1 ã¤ç®ã®æ¹æ³ã¨ãã¦ãæ大å¤â¦
ãæ大å¤ã¨æå°å¤ã®å·®ããæå°åããã¨è¨ãããããæ大å¤ã¾ãã¯æå°å¤ãåºå®ããã¨ä¸æããããã¨ãå¤ãï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ ãµã¤ãºã ã§ãããã㪠4 ã¤ã®æ°å ãä¸ããããã ãããã®æ°åãã 1 åãã¤è¦ç´ ãé¸ã㧠ã¨ããã ã®å¤ã®æå°å¤ãæ±ãâ¦
ã¡ããã¨è¨¼æãããã¨ããã¨ãçµæ§å¤§å¤ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ 人ã®çå¾ããã¦ãçå¾ ã®èº«é·ã¯ ã§ããã ãããã®çå¾ã 2 人ã㤠çµã«åãã¦ãã©ã®çµã身é·å·®ã 以ä¸ã¨ãªãããã«ãããã ãã®ãããªãã¨ãå¯è½ãã©ãããå¤å®ããã å¶ç´ èããã㨠ãâ¦
ã¨ã¦ãæè²çãªåé¡ï¼ ã¡ããã¨è§£ãã«ã¯ãè¨ç®éã®ç解ãå¿ è¦ã¨ãªãåé¡ã§ããã åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ 3 ã¤ã®æ°å ãä¸ããããããããã®æ°åã«å¯¾ãã¦ã次㮠åã®ã¯ã¨ãªã«çããã ãã¯ã¨ãªã æ´æ° ãä¸ããããã®ã§ã ãããããã 1 åãã¤é¸ãã§åãâ¦
ã¨ã¦ãæè²çã§å ¸åçãªãããã¨ãæ³ã®åé¡ï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ åã®æ´æ°ãããªãæ°å ãä¸ããããã ãçå·®æ°åã§ãããããªçµ ã®åæ°ãæ±ããã å¶ç´ è§£æ³ (1)ï¼ãããã¨ãæ³ ä»åã®åé¡ã®ããã«ãæ°åä¸ã§æ¡ä»¶ãæºããåºéãèããåé¡ã§ã¯ãããâ¦
set ã使ããï¼ åé¡ã¸ã®ãªã³ã¯ åé¡æ¦è¦ é·ã ã®æ´æ°å ã¨ãæ£ã®æ´æ° ãä¸ããããã ä»¥ä¸ ä»¥ä¸ã®æ´æ°ã®ãã¡ãæ°å ã«ä¸åº¦ãå«ã¾ããªããã®ã«ã¤ãã¦ã®ç·åãæ±ããã å¶ç´ èããã㨠ã¾ãã ã¨ãªãã ãã®å¤ããã1 ä»¥ä¸ ä»¥ä¸ã®æ´æ°ã®ãã¡ãæ°å ã«ä¸åº¦ä»¥â¦