Codeforces Round #648 (Div. 2) C. Rotation Matching(思维)

题目传送
题意:
给你俩个大小为n的排列,现在你可以任意的选择一个排列,使其全部元素同时向右或者向左平移k(k任选)个单位,问最多能有多少个:当i == j(俩个排列中的位置下标一样时),ai == bj(相同位置的元素值相同)。

思路:
既然是平移,那么向右或者向左平移的结果是一样的,那么我们何不枚举一下,枚举当前向右平移一个单位,俩个单位,直到n个单位时,那种情况最多。但是这样一来时间复杂度就高了,所以我们要转化一下,其实我们只要记录第一个排列中,的1的位置和第二个排列中1的位置的距离之差(向右平移的距离),然后依次记录下去,我们看看在相同距离下,谁的次数最多就可以了

//看代码易懂些哦
AC代码

#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 2e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const unsigned long long mod = 998244353;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
int a[N],b[N],vis[N];
void Put(int n,int ans)
{
    for(int i = 1,c;i <= n;i++)
    {
        cin >> c;
        ans ? a[c] = i : b[c] = i;
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int n,Max = 0;
    cin >> n;
    Put(n,1);Put(n,0);//输入俩个排列
    for(int i = 1;i <= n;i++)
    {
        int x = 0;
        a[i] > b[i] ? x = n-(a[i]-b[i]) : x = b[i]-a[i];//向右平移的距离
        vis[x]++;//记录该距离下,有多少个数相等
        Max = max(vis[x],Max);//取最多的次数
    }
    cout << Max << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值