首页 > 其他分享 >D. Fixed Prefix Permutations(字典树)

D. Fixed Prefix Permutations(字典树)

时间:2023-01-25 20:55:41浏览次数:56  
标签:排列 15 int Permutations start Prefix const Fixed 字典


题意:

  • 给一个n行m列的关于m的排列数组,n个m的排列,设为q[n]
  • 对于q[i],找到最长的q[q[i]]排列是1,2,...,k,美丽值是k
  • 输出每一个的k

思路:

  • 看样例一可以大概知道字典树是该怎么做
  • 对于1~m,找到原先的关于m的排列,数字的位置的坐标,树中插入
  • 然后查询原数组能走的最长路径

#include<bits/stdc++.h>
#define debug1(a) cout<<#a<<'='<< a << endl;
#define debug2(a,b) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<endl;
#define debug3(a,b,c) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<endl;
#define debug4(a,b,c,d) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<"  "<<#d<<" = "<<d<<endl;
#define debug5(a,b,c,d,e) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<"  "<<#d<<" = "<<d<<"  "<<#e<<" = "<<e<<endl;
#define endl "\n"
#define fi first
#define se second

#define int long long
//#define int __int128
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;

//#pragma GCC optimize(3,"Ofast","inline")
//#pragma GCC optimize(2)

//常数定义
const double eps = 1e-4;
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int N = 5e4+10;
int a[N][15];

int son[15*N][15], idx;
int n,m;
int start;

void insert(int a[])          
{
    int p = start;
    for (int i = 1; i <= m; i ++ )
    {
        int u = a[i];
        if (!son[p][u]) son[p][u] = ++ idx;
        p = son[p][u];
    }
}

int query(int a[])
{
    int p = start;
    int step = 0;
    for (int i = 1; i <= m; i ++ )
    {
        int u = a[i];
        if (!son[p][u]) return step;
        p = son[p][u];
        step ++;
    }
    return step;
}


void solve() 
{
    start = idx;

    cin >> n >> m;
    for(int i = 1;i <= n;i ++)
    {
        for(int j = 1;j <= m;j ++)
        {
            cin >> a[i][j];
        }
        int temp[15];
        for(int j = 1;j <= m;j ++)
        {
            temp[a[i][j]] = j;
        }
        insert(temp);
    }

    for(int i = 1;i <= n;i ++)
    {
        cout << query(a[i]) << " ";
    }
    cout << endl;

}

signed main()
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int T = 1;cin >> T;

    while(T--){
        //puts(solve()?"YES":"NO");
        solve();
    }
    return 0;

}
/*

*/

 

标签:排列,15,int,Permutations,start,Prefix,const,Fixed,字典
From: https://www.cnblogs.com/cfddfc/p/17067276.html

相关文章

  • CF1792 D. Fixed Prefix Permutations : Educational Codeforces Round 142 (Rated fo
    给出n个长度为m的排列(a1,a2,a3,...,an)定义一个操作 r=ai•aj:r[k]=a[j][a[i][k]]定义一个排列(p1,p2,...,pn)的beauty为最大的k,使得p[1]=1,p[2]=2,..,p[k......
  • codeforce edu round 142 D. Fixed Prefix Permutations
    题目链接:https://codeforces.com/contest/1792/problem/D题意是给出n个长度为m的排列,定义排列p*q为\(r_j=q_{p_j}\),令一个排列的价值p为最大的k使得\(p_1=1,p_2=2......
  • fixed 定位设置 scroll 不滚动
    我的问题是把容器的高度设置成了100vw,和视口保持同样的高度,所以设置scroll也无法滚动。尽管我试了很多其他方法都不能让其滚动。把高度设置成100%就可以了,结构如下:<......
  • CF1364C-Ehab and Prefix MEXs
    a[i]<=i,否则当a[i]>i时,需要1~i项有数字0~a[i]-1,这一共是a[i]个数字,而1~i项只有i个数字,需要的比拥有的数字多,不成立当a[i]!=a[i-1]时,说明Mex改变了,那么需要b[i]为a[i-1]才......
  • CF 1779C Least Prefix Sum 题解
    CF链接:LeastPrefixSumLuogu链接:Least PrefixSum${\scr\color{CornflowerBlue}{\text{Solution}}}$先来解释一下题意:给定一个数组,问最少把多少个数变成相反数,......
  • AS编译报错:No toolchains found in the NDK toolchains folder for ABI with prefix:
    简单粗暴的解决办法:删除sdk路径下ndk打头的两个文件夹即可,如果不想删除,改名也行(比如在文件夹后面加个1)。两个文件夹分别为:ndk、ndk-bundle......
  • Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context(论文和代
         Transformer模型能够学习长范围依赖,但是在语言模型中受到固定长度上下文限制,本文提出了一个新的结构:Transformer-XL。能够学习超过固定长度的依赖,同时保持了......
  • CodeForces - 1698D Fixed Point Guessing
    CodeForces-1698DFixedPointGuessing题解:二分+交互题题目给出询问次数为15次,而\(3<=n<10^4\),很明显是二分题目想要我们找出在数组长度n为奇数的情况下,交换\(\fra......
  • CF1779C Least Prefix Sum 题解
    可能更好的阅读体验题目传送门题目大意给定一个序列\(a\),长度\(n\)。每次操作可以把\(a_i\)变成\(-a_i\)。要求\(a\)做前缀和之后的序列\(s\)中最小值为\(s......
  • CF1779 Least Prefix Sum
    url:Problem-C-Codeforces题意:给n个数字和一个m给一个操作:每次使得其中一个下标的数字*=-1要求最后在所有前缀和中前m个数字是最小的思路:在所有前缀和中分......