codeforces 7.22 E Permutation Shift

给出一个1到n的排列,每次可以交换两个数,问在交换最多m次(m <= n/3)之后能不能得到由1 2 3 … n循环右移所得到的的排列,输出所有能得到的排列和循环右移的次数。
数据范围:n <= 3e5

有点脑洞的一道题。

由于最多交换m次,那么最多会有2m个数交换位置,而剩下的n - 2m个数是在原位置不变的。也即是,在循环右移k位之后,有至少n - 2m个数是与给出的排列对应位置的数相同的。

而在循环右移的过程中,对于每一位的数,只存在一个k使得让这一位的数与给出的排列在同一位置是一样的。这就代表着,在循环右移n-1次之后,每一次两个排列相同的位数之和加起来是n。而要满足条件的话,在循环右移k次之后,至少有n - 2m个数是相同的,n - 2m >= n / 3。也就是说最多存在三个k满足条件。所以我们计数统计k,最多三次O(n)O(n)O(n)计算最少交换次数。总复杂度为O(n)O(n)O(n).

const int N = 3e5 + 10;
int a[N], cnt[N], pos[N], b[N];
bool vis[N];int main()
{//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);int T = 1;T = read();while (T --){int n = read(), m = read();for (int i = 0; i < n; i ++)cnt[i] = 0;for (int i = 0; i < n; i ++){a[i] = read();-- a[i];pos[a[i]] = i;int k = (i - a[i] + n) % n;cnt[k] ++; }queue <int> q;for (int i = 0; i < n; i ++){if (cnt[i] < n - 2 * m) continue;for (int j = 0; j < n; j ++){vis[j] = 0;b[(j + i) % n] = j;}int tot = 0;for (int j = 0; j < n; j ++){if (!vis[j]){++ tot;vis[j] = 1;int p = j;while (!vis[pos[b[p]]]){vis[pos[b[p]]] = 1;p = pos[b[p]];}}}tot = n - tot;if (tot <= m)q.push(i);}cout << q.size();while (!q.empty()){int now = q.front(); q.pop();cout << " " << now;}cout << endl;}return 0;
}

codeforces 7.22 E Permutation Shift相关推荐

  1. CodeForces - 1553E Permutation Shift(暴力+置换群求环)

    题目链接:点击查看 题目大意:假设初始时的数组为 [1,2,3,...,n][1,2,3,...,n][1,2,3,...,n],同时 kkk 为偏移量,则原数组会循环右移 kkk 个单位,假设 k= ...

  2. Harbour.Space Scholarship Contest 2021-2022 E. Permutation Shift 思维 + 剪枝

    传送门 文章目录 题意: 思路: 题意: 给你一个初始排列[1,2,3,...,n][1,2,3,...,n][1,2,3,...,n],你可以选择一个kkk,将这个排列循环右移kkk次,让后最多交换 ...

  3. cf1553E. Permutation Shift

    cf1553E. Permutation Shift 题意: 给出一个1到n的排列,每次可以交换两个数,问在交换最多m次(m <= n/3)之后能不能得到由1 2 3 - n循环右移所得到的的排 ...

  4. codeforces 7.22 F Pairwise Modulo

    codeforces 7.22 F Pairwise Modulo 给出n个数的数列a,每个数互不相同且都小于3e5,求出qk=∑1<=i,j<=kaimodajq_k=\sum_{1&l ...

  5. Codeforces Problem 708A Letters Cyclic Shift(implementation)

    此文章可以使用目录功能哟↑(点击上方[+]) 比赛链接→AIM Tech Round 3 (Div. 1)  Codeforces Problem 708A Letters Cyclic Shift ...

  6. CodeForces 361B Levko and Permutation

    题意:有n个数,这些数的范围是[1,n],并且每个数都是不相同的.你需要构造一个排列,使得这个排列上的数与它所在位置的序号的最大公约数满足 > 1,并且这些数的个数恰好满足k个,输出这样的一个排 ...

  7. 【CodeForces - 124C】Prime Permutation(数学,思维,小结论)

    题干: You are given a string s, consisting of small Latin letters. Let's denote the length of the stri ...

  8. 【CodeForces - 483C】Diverse Permutation(思维构造)

    题干: Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of ndistinct posit ...

  9. Codeforces 1754B.Kevin and Permutation

    1754B.Kevin and Permutation 原题地址 For his birthday, Kevin received the set of pairwise distinct numbe ...

最新文章

  1. 如何为linux释放内存和缓存
  2. 成功解决object at 0x000002463192BAC8
  3. 一行Java代码实现混乱排序List集合
  4. 【复杂系统迁移 .NET Core平台系列】之调度服务改造
  5. android led闪烁功能,如何在Android应用层中制作一个LED指示灯效果
  6. Teamwork(The sixth day of the team)
  7. SQL SERVER:开窗函数 SUM() OVER() 数据统计中一例使用
  8. BasicExcel CSpreadSheet 使用感受
  9. python文件命名 数字,当文件存在时,文件名中的数字递增
  10. ASP人事工资管理系统毕设
  11. 46.@弹出点击次数
  12. sql随机取一条数据
  13. 极简Markdown程序员简历模板
  14. 网安自学之路-信息安全数学基础(五)-近世代数
  15. 生产计划排程、车间工序排程、APS高级计划与排程
  16. 推荐育儿书《正面管教》
  17. 戴尔要求DEC雇员扔掉Mac
  18. 客户端连接服务器超时
  19. 将等号作为键值的js字符串转json
  20. Redisson闭锁

热门文章

  1. Python多版本共存配置
  2. 通过结构体某个成员的地址计算结构体首地址 (转)
  3. 诺基亚力邀App开发员加入Ovi以对抗苹果
  4. 程序员面试题准备(1)单链表逆置
  5. Maven虐我千百遍,我待Maven如初恋
  6. fetch + async await 使用原生JS发送网络请求
  7. 一个Objective-C对象如何进行内存布局?(考虑有父类的情况)
  8. 20144306《网络对抗》Web基础
  9. 移动端H5页面注意事项
  10. 开发人员必备:微软发布示例代码浏览器 (Sample Browser) 第五版,让您尽享3500个示例代码...