10-18 noip提高组模拟赛(codecomb)T1倍增[未填]
T1只想到了找环,> <倍增的思想没有学过,所以看题解看得雨里雾里的(最近真的打算学一下!
题目出的挺好的,觉得noip极有可能出现T1T2T3,所以在此mark
刚开始T1以为是模拟,还妄想这可以一遍处理出所有的环,和各个数之间的关系(> <结果自己把自己绕进去了,因为根本不可以!)
然后后面改了暴力,每问一次就处理一次;
正解没看懂QAQ..应该是不太懂思想的缘故
暴力:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int maxn=100005; int n,m,x; long long k; int a[maxn]; bool v[maxn]; int main(){ scanf ( "%d%d" ,&n,&m); for ( int i=1;i<=n;i++){ scanf ( "%d" ,&a[i]); } for ( int i=1;i<=m;i++){ cin>>x>>k; for ( int i=1;i<=n;i++) v[i]=0; int temp; for ( int i=x;!v[i];i=a[i]){ v[i]=1; temp=a[i]; } while (k && x!=temp){ x=a[x]; k--; } if (k){ int cnt=1; while (a[temp]!=x){ cnt++; temp=a[temp]; } k%=cnt; } while (k--) x=a[x]; cout<<x; } return 0; } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步