首页 > 其他分享 >Educational Codeforces Round 119 E

Educational Codeforces Round 119 E

时间:2022-09-21 21:35:38浏览次数:62  
标签:Educational const int Codeforces cin op 119 define

E. Replace the Numbers

开始想到了一个二分的做法 好像是O(nlog)的 后来才想了一下可以在两个数组之间反复横跳
那我是不是像记忆化搜索一样 记录一个路径即可
我们记录f[i]表示的就是当前i点的颜色为什么
显然我们正着做需要知道后面的状况 那我们就可以离线下来倒着做

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
const int M = 998244353;
const int mod = 998244353;
#define int long long
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define pi acos(-1)
#define INF 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);
vector<int>op(N),f(N),x(N),y(N);
void solve() {
    int q;cin>>q;
    for(int i=1;i<=N;i++)f[i]=i;
    for(int i=1;i<=q;i++){
        cin>>op[i];
        if(op[i]==1){
            cin>>x[i];
        }else {
            cin>>x[i]>>y[i];
        }
    }
    vector<int>ans;
    for(int i=q;i>=1;i--){
        if(op[i]==1){
            ans.push_back(f[x[i]]);
        }else{
            f[x[i]]=f[y[i]];
        }
    }
    for(int i=ans.size()-1;i>=0;i--)cout<<ans[i]<<' ';
}
signed main(){
    fast
    int T;T=1;
    while(T--) {
        solve();
    }
    return ~~(0^_^0);
}

标签:Educational,const,int,Codeforces,cin,op,119,define
From: https://www.cnblogs.com/ycllz/p/16717193.html

相关文章

  • Codeforces Round #818 (Div. 2) - D. Madoka and The Corruption Scheme
    思维+组合数学Problem-D-Codeforces题意有\(2^n\)个人进行锦标赛,编号1~\(2^n\),每一场输的人失去比赛资格,赢的人继续。Madoka可以选择他们进行的顺序,以及决定哪一......
  • Codeforces 821 Div2
    T1:大小为n的数组,最多进行k次操作:下标模k意义下相等则可进行交换。求操作后连续k个元素的最大值固定最大值的k个连续因素小标为[0,k),现在只需使得它为最大即可,将可交换位......
  • Codeforces Round #821 (Div. 2) - D2. Zero-One (Hard Version)
    区间DPProblem-D2-Codeforces题意给一个长度为\(n(5<=n<=5000)\)的01串,每次操作可选择一个\(l,r(l<r)\),把\(s[l],s[r]\)反转。如果\(l+1==r\),花费为x,否......
  • Codeforces Round #807 (Div. 2) - D. Mark and Lightbulbs
    思维Problem-D-Codeforces题意给两个长度为\(n(3<=n<=2*10^5)\)的01串s与t,求最小操作次数,使s变成t;不存在则输出-1操作为:对于2<=i<=n-1,若\(s_......
  • Codeforces Round #820 (Div. 3) G. Cut Substrings
    DPProblem-G-Codeforces题意给一个长度为\(n(1<=n<=500)\)的主串s,一个长度为\(m(1<=m<=500)\)的模式串t,每次可以将当前的s中与t相同的子串变成一串"."(如......
  • Codeforces Round #821 (Div. 2) ABCD1
    A.ConsecutiveSumhttps://codeforces.com/contest/1733/problem/A题目大意:给定一个长度为n的数组a。最多可以执行k次以下操作:选择两个指数i和j,其中imodk=jmodk......
  • Codeforces Round #821(Div.2) (A-C) 题解
    CodeforcesRound#821(Div.2)(A-C)  A.ConsecutiveSum大致题意给定一组共n个数据,如果俩个数的下标在modk意义下同余,则可以交换a[I]和a[j] ,求操作后一段......
  • Codeforces Round #821 (Div. 2)(持续更新)
    Preface唉LOL打到一半被迫营业开打,感觉这算是第一次自己认真的在线打CF,以我的老年人手速感觉要罚时飞起了10:35开始打到12:10顶不住了睡觉去了,E大概看了个题感觉挺有意思的......
  • Codeforces Round #819 (Div. 1 + Div. 2) and Grimoire of Code Annual Contest 2022
    Preface明天的CF一定打啊(绝对不咕),今天白天补现代作业,英语作业到哭,而且还要准备四六级,每天逼着自己背单词A.MainakandArray不难发现换中间的数不会影响答案,因此操作......
  • Codeforces Round #821 D2
    D2.Zero-One(HardVersion)我们由D1可知当我们的y小于x/2时我们可以用2y来减小相邻的cost那我们考虑要是y比较大的时候我们也可以用多个x来减小cost我们可以轻松的......