Replace the Numbers

Replace the Numbers(VOJ)

Replace the Numbers(codeforces)

题目大意

有一个空序列和 q q q次操作,操作分如下两类:

  • 1 , x 1,x 1,x,意为在数列末尾加一个数字
  • 2 , x , y 2,x,y 2,x,y,意为将当前数列中的所有值为 x x x的数替换为 y y y

求出 q q q次操作后的序列。


题解

我们可以倒着做,从最后一个开始。设 f x f_x fx表示此时的 x x x在最后是什么值。从后往前遍历每次询问,对于操作1,将答案中这个位置的值赋为 f x f_x fx;对于操作2,令 f x = f y f_x=f_y fx=fy,也就是说在下一次对 x x x修改之前, x x x的实际值等于 y y y的实际值。大家可以自己举几个例子来理解。

code

#include<bits/stdc++.h>
using namespace std;
int n,fa[500005],ans[500005];
struct node{
	int tp,x,y;
}w[500005];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=500000;i++){
		fa[i]=i;
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&w[i].tp);
		if(w[i].tp==1) scanf("%d",&w[i].x);
		else scanf("%d%d",&w[i].x,&w[i].y);
	}
	for(int i=n;i>=1;i--){
		if(w[i].tp==2){
			fa[w[i].x]=fa[w[i].y];
		}
		else{
			ans[++ans[0]]=fa[w[i].x];
		}
	}
	for(int i=ans[0];i;i--) printf("%d ",ans[i]);
	return 0;
}
Fibonacci numbers are the sequence of integers: f 0 = 0 f 0 ​ =0 , f 1 = 1 f 1 ​ =1 , f 2 = 1 f 2 ​ =1 , f 3 = 2 f 3 ​ =2 , f 4 = 3 f 4 ​ =3 , f 5 = 5 f 5 ​ =5 , . . . ... , f n = f n − 2 + f n − 1 f n ​ =f n−2 ​ +f n−1 ​ . So every next number is the sum of the previous two. Bajtek has developed a nice way to compute Fibonacci numbers on a blackboard. First, he writes a 0. Then, below it, he writes a 1. Then he performs the following two operations: operation "T": replace the top number with the sum of both numbers; operation "B": replace the bottom number with the sum of both numbers. If he performs n n operations, starting with "T" and then choosing operations alternately (so that the sequence of operations looks like "TBTBTBTB . . . ... "), the last number written will be equal to f n + 1 f n+1 ​ . Unfortunately, Bajtek sometimes makes mistakes and repeats an operation two or more times in a row. For example, if Bajtek wanted to compute f 7 f 7 ​ , then he would want to do n = 6 n=6 operations: "TBTBTB". If he instead performs the sequence of operations "TTTBBT", then he will have made 3 mistakes, and he will incorrectly compute that the seventh Fibonacci number is 10. The number of mistakes in the sequence of operations is the number of neighbouring equal operations («TT» or «BB»). You are given the number n n of operations that Bajtek has made in an attempt to compute f n + 1 f n+1 ​ and the number r r that is the result of his computations (that is last written number). Find the minimum possible number of mistakes that Bajtek must have made and any possible sequence of n n operations resulting in r r with that number of mistakes. Assume that Bajtek always correctly starts with operation "T". 输入格式 The first line contains the integers n n and r r ( 1 < = n , r < = 1 0 6 ) 1<=n,r<=10 6 ) . 输出格式 The first line of the output should contain one number — the minimum possible number of mistakes made by Bajtek.用c++
07-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值