[关闭]
@wsndy-xx 2018-05-11T08:34:08.000000Z 字数 1991 阅读 797

模拟赛整理 - 环

题解


题意

给出1-n的全排列 a[i], 给出 q 个询问 (x, m)


输出qAns


算法1

考虑倍增
预处理出每个点跳2^i步到达的点
对于每次询问logn查询
时间复杂度nlogn

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #define f(x, y, z) for(int x = (y); x <= (z); ++x)
  5. int a[1048576], F[31][1048576], *an = a;
  6. bool vi[1048576];
  7. int main(){
  8. freopen("kengdie.in", "r", stdin);
  9. freopen("kengdie.out", "w", stdout);
  10. while(scanf("%d", an) != EOF) ++an;
  11. *an = 0; *(an + 1) = 1000086;
  12. memset(vi, 0, sizeof(vi));
  13. int n = 0, *i = a;
  14. if((an - a) & 1){
  15. F[0][++n] = *i; vi[*i] = 1; ++i;
  16. }
  17. for(; i != an; i += 2){
  18. int *j = i + 1;
  19. if(*j > 1000000 || vi[*i]) break;
  20. F[0][++n] = *i; vi[*i] = 1;
  21. F[0][++n] = *j; vi[*j] = 1;
  22. }
  23. f(j, 1, 30) f(i, 1, n) F[j][i] = F[j - 1][F[j - 1][i]];
  24. for(; i != an; i += 2){
  25. int x = *i, T = *(i + 1);
  26. f(j, 0, 30) if(T & (1 << j)) x = F[j][x];
  27. printf("%d\n", x);
  28. }
  29. return 0;
  30. }

算法2

我们考虑对于每次询问,只会在该点所在的环内查询,那么就可以将所有的环记录下来,就可以O(1)查询,记录环的时候,用vector存储每个环,记录环的时间复杂度为O(n),整个算法的时间复杂度为O(n).

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <vector>
  5. using namespace std;
  6. const int N = 1e5 + 10;
  7. vector <int> vec[N];
  8. int wei[N], bel[N], A[N];
  9. bool vis[N];
  10. int n, x, m;
  11. int each[N];
  12. int vecjs;
  13. #define gc getchar()
  14. inline int read() {
  15. int x = 0; char c = gc;
  16. while(c < '0' || c > '9') c = gc;
  17. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = gc;
  18. return x;
  19. }
  20. void Calc() {
  21. if(m == 0) {printf("%d\n", x); return ;}
  22. int a = A[x];
  23. m --;
  24. int which_vec = bel[a];
  25. int which_wei = wei[a];
  26. int Size = vec[which_vec].size();
  27. int ans = (m + which_wei) % Size;
  28. if(!ans) printf("%d\n", vec[which_vec][Size - 1]);
  29. else printf("%d\n", vec[which_vec][ans - 1]);
  30. }
  31. void Work_3() {
  32. Calc();
  33. while(scanf("%d%d", &x, &m) == 2) Calc();
  34. exit(0);
  35. }
  36. void Work_2() {
  37. for(int i = 1; i <= n; i ++) vis[i] = 0;
  38. vecjs = 0;
  39. for(int i = 1; i <= n; i ++) {
  40. if(vis[A[i]]) continue ;
  41. vecjs ++;
  42. vec[vecjs].push_back(A[i]);
  43. each[vecjs] ++;
  44. bel[A[i]] = vecjs;
  45. wei[A[i]] = each[vecjs];
  46. vis[A[i]] = 1;
  47. A[0] = A[i];
  48. for(int j = 0; (j = A[j]) && (!vis[A[j]]); ) {
  49. vec[vecjs].push_back(A[j]);
  50. each[vecjs] ++;
  51. bel[A[j]] = vecjs;
  52. wei[A[j]] = each[vecjs];
  53. vis[A[j]] = 1;
  54. }
  55. }
  56. Work_3();
  57. }
  58. int main() {
  59. freopen("kengdie.in", "r", stdin);
  60. freopen("kengdie.out", "w", stdout);
  61. int a = read(), b = read();
  62. n = 2;
  63. A[1] = a, A[2] = b;
  64. vis[a] = 1, vis[b] = 1;
  65. n = 2;
  66. for(; ;) {
  67. int a = read();
  68. if(vis[a]) x = a, m = read(), Work_2();
  69. else A[++ n] = a, vis[a] = 1;
  70. }
  71. return 0;
  72. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注