首页 > 其他分享 >E - Permute K times

E - Permute K times

时间:2024-08-18 17:17:35浏览次数:7  
标签:le sequence int times Sample Input Permute

E - Permute K times

Problem Statement

You are given a sequence $X$ of length $N$ where each element is between $1$ and $N$, inclusive, and a sequence $A$ of length $N$.
Print the result of performing the following operation $K$ times on $A$.

  • Replace $A$ with $B$ such that $B_i = A_{X_i}$.

Constraints

  • All input values are integers.
  • $1 \le N \le 2 \times 10^5$
  • $0 \le K \le 10^{18}$
  • $1 \le X_i \le N$
  • $1 \le A_i \le 2 \times 10^5$

Input

The input is given from Standard Input in the following format:

$N$ $K$
$X_1$ $X_2$ $\dots$ $X_N$
$A_1$ $A_2$ $\dots$ $A_N$

Output

Let $A'$ be the sequence $A$ after the operations. Print it in the following format:

$A'_1$ $A'_2$ $\dots$ $A'_N$


Sample Input 1

7 3
5 2 6 3 1 4 6
1 2 3 5 7 9 11

Sample Output 1

7 2 3 5 1 9 3

In this input, $X=(5,2,6,3,1,4,6)$ and the initial sequence is $A=(1,2,3,5,7,9,11)$.

  • After one operation, the sequence is $(7,2,9,3,1,5,9)$.
  • After two operations, the sequence is $(1,2,5,9,7,3,5)$.
  • After three operations, the sequence is $(7,2,3,5,1,9,3)$.

Sample Input 2

4 0
3 4 1 2
4 3 2 1

Sample Output 2

4 3 2 1

There may be cases where no operations are performed.


Sample Input 3

9 1000000000000000000
3 7 8 5 9 3 7 4 2
9 9 8 2 4 4 3 5 3

Sample Output 3

3 3 3 3 3 3 3 3 3

 

解题思路

  比赛时一直想着线性的做法,完全没考虑过倍增,结果发现代码巨难写。

  首先如果按照 $i \to x_i$ 进行连边,就会得到一棵基环树,当 $k$ 足够大的时候其实就是在环上不断循环。对于每个点可以先让 $k$ 减去该点到环的距离,然后对环的大小取模,求出最后停留在环上的哪个位置,但实现起来非常复杂。本质上就是快速求出每个点在走 $k$ 步后最后停留在哪里,由于 $k$ 非常大这启发我们可以往倍增上考虑。

  定义 $X^{j}_{i}$ 表示从点 $i$ 经过 $2^j$ 步后到达的位置,$X^{0}_{i}$ 就是初始输入的数据。想要求得 $X^{j}_{i}$,我们可以先让 $i$ 走 $2^{j-1}$ 步到达 $X^{j-1}_{i}$,再从 $X^{j-1}_{i}$ 走 $2^{j-1}$ 步到达 $X^{j}_{i}$,因此有转移方程 $X^{j}_{i} = X^{j-1}_{X^{j-1}_{i}}$。

  为了求出从 $i$ 走 $k$ 步的结果,先把 $k$ 看作二进制数 $k_{59}k_{58} \cdots k_{0}, \, k_j \in \{0,1\}$,维护一个数组 $p_i$ 表示从 $i$ 经过若干步后到达的位置,初始时定义 $p_i = i$。然后枚举 $k$ 的每一个二进制位,如果 $k_j = 1$,意味着我们需要走 $2^{j}$ 步,更新 $p_i = X^{j}_{p_i}$。

  最后输出 $a_{p_i}$ 即可。

  AC 代码如下,时间复杂度为 $O(n \log{k})$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2e5 + 5;

int x[60][N], p[N], a[N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    LL m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> x[0][i];
    }
    for (int i = 1; 1ll << i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            x[i][j] = x[i - 1][x[i - 1][j]];
        }
    }
    iota(p + 1, p + n + 1, 1);
    for (int i = 0; 1ll << i <= m; i++) {
        if (m >> i & 1) {
            for (int j = 1; j <= n; j++) {
                p[j] = x[i][p[j]];
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        cout << a[p[i]] << ' ';
    }
    
    return 0;
}

 

参考资料

  Editorial - AtCoder Beginner Contest 367:https://atcoder.jp/contests/abc367/editorial/10707

标签:le,sequence,int,times,Sample,Input,Permute
From: https://www.cnblogs.com/onlyblues/p/18365805

相关文章

  • 题解:AT_abc367_e [ABC367E] Permute K times
    题意给你一个长度为\(N\)的序列\(X\),其中每个元素都介于\(1\)和\(N\)之间(含),以及一个长度为\(N\)的序列\(A\)。打印在\(A\)上执行以下操作\(K\)次的结果。将\(A_i\)替换为\(A_{X_i}\)。每个操作同时进行。思路元素\(i\)经过\(k\)次变化后的值就是元素......
  • RuntimeError:permute(sparse_coo):张量输入中的维度数与所需维度排序的长度不匹配
    因此,我使用这个剪辑模型来执行一些标记任务。但是当我使用剪辑模型的文本编码器时,它会出现以下错误:<ipython-input-117-4c513cc2d787>inforward(self,batch)34print(y.size())35print(y.dim())--->36y=self.text_encoder(y)37......
  • PostgreSQL 之 to_timestamp函数
    to_timestamp是PostgreSQL中的一个函数,用于将字符串或数字转换为时间戳。以下是关于to_timestamp的详细介绍:引入版本to_timestamp函数在PostgreSQL7.3版本中引入。语法to_timestamp有两种主要的用法:1.将字符串转换为时间戳to_timestamp(text,text)第一......
  • Pandas 中的 pd.Timestamp() 行为
    试图理解为什么t1采用当前日期,而t2采用Pandas中的纪元日期Python任何想法都会有帮助。importpandasaspdt1=pd.Timestamp("23:12:05")print("t1:",t1)t2=pd.Timestamp(1)print("t2:"t2)输出:t1:2024-07-2323:12:05t......
  • 后仿真中《SDF反标必懂连载篇》之 探究 SDF延迟精度 与 timescale 精度问题
    目录一 SDF文件中的延迟数据二 设计文件中的timescale指令三 SDF精度和timescale之间的关系【例子1】【例子2】 【例子3】 【例子4】 本篇文章,同样属于后仿真中的SDF反标系列文章内容之一。今天,将前仿真中的timescale和后仿真中timescale+sdf延迟......
  • TimescaleDB时间序列数据库
    TimescaleDB:这是一款支持完整sql开源的时间序列数据库。用处1、数据量庞大2、只做时间索引类的插入3、很少更新数据TimescaleDB的好处:基于时序优化自动分片(自动按时间、空间分片(chunk))全SQL接口支持垂直于横向扩展支持时间维度、空间维度自动分区。空间维度指属性字......
  • Windows定时器-timeSetEvent
     接口:MMRESULTtimeSetEvent(UINTuDelay,//以毫秒指定事件的周期UINTuResolution,//以毫秒指定延时的精度,缺省值为1msLPTIMECALLBACKlpTimeProc,//指向回调函数的指针WORDdwUser,//用户定义的回调数据,传递给回调函数......
  • MySQL中datetime和timestamp的区别
    #MySQL中datetime和timestamp的区别相同点两个数据类型存储时间的格式一致。均为YYYY-MM-DDHH:MM:SS两个数据类型都包含「日期」和「时间」部分。两个数据类型都可以存储微秒的小数秒(秒后6位小数秒)自动更新和默认值TIMESTAMP:支持默认值为当前时间,且在记录更新时可以......
  • [题解]AT_abc253_g [ABC253G] Swap Many Times
    思路首先,不难看出一个规律,就是对于一个序列\(a\),如果它将操作所有以\(x\)为第一关键字的二元组,那么序列的\(a_{x\simn}\)将循环右移一位。(注意,在这里的\(x\)指的是在\(1\sim(n-1)\)中的任意一个定值)那么,我们就可以将编号分别为\(l\simr\)的这些二元组分为三......
  • flink 如果是有序流,还需要 forMonotonousTimestamps吗
    如果数据是有序的,即数据完全按照时间发生的顺序到达,那么在flink中,虽然理论上不需要额外的Watermark策略来标识数据的有序性,但使用forMonotonousTimestamps策略仍然有其必要性。以下是详细解释:水位的作用即使数据完全有序,flink的窗口计算仍然需要watermark来触发。watermark提......