UPC --- 2018年第三阶段个人训练赛第六场 --- J题 Derangement (6602)

本文探讨了一道算法题,目标是最小化操作次数以确保序列中每个元素的位置与其数值不一致。通过交换相邻元素实现,特别关注了整数序列完全不符合条件时的特殊情况。

问题 J: Derangement

时间限制: 1 Sec  内存限制: 128 MB
提交: 426  解决: 219
[提交] [状态] [讨论版] [命题人:admin]

题目描述

You are given a permutation p1,p2,…,pN consisting of 1,2,..,N. You can perform the following operation any number of times (possibly zero):
Operation: Swap two adjacent elements in the permutation.
You want to have pi≠i for all 1≤i≤N. Find the minimum required number of operations to achieve this.

Constraints
2≤N≤105
p1,p2,..,pN is a permutation of 1,2,..,N.

 

输入

The input is given from Standard Input in the following format:
N
p1 p2 .. pN

 

输出

Print the minimum required number of operations

 

样例输入

5
1 4 3 5 2

 

样例输出

2

 

提示

Swap 1 and 4, then swap 1 and 3. p is now 4,3,1,5,2 and satisfies the condition. This is the minimum possible number, so the answer is 2.




【题目大意】: 

给你一个序列p,通过交换相邻的两个数字,使得序列里的数字与位置的编号不同,即 p[i] != i (下标从1开始),求最小的交换次数。

思路: 

因为只能相邻的数字两两交换,要使得交换次数最小,数字与后一个交换即可。

用一个数组b来存需要交换的数字。

  1. 如果两个相邻的数字都需要交换,即abs(b[i]-b[i+1])=1,则交换一次就可以使两个数字满足要求,同时 i++,跳过下一个要交换的数b[i+1],因为已经满足要求了;
  2. 如果两个相邻数字仅一个要交换,则直接 交换次数+1 就行了;
  3. 如果b数组长度与n相等,即 整个序列都不满足条件,通过举例可以得到规律,

交换的次数是 :

  • 当 n为偶数, ans = n/2;
  • 当 n为奇数,ans = n/2+1;

 


代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int a[N];
int b[N];
int main()
{
    int n;
    while(cin >> n)
    {
        int ans = 0;
        for(int i=1; i<=n; i++)
            cin >> a[i];
        int cnt = 0;
        for(int i=1; i<=n; i++)
        {
            if(a[i] == i)
                b[cnt++] = i;
        }
        if(cnt == n)
        {
            if(n%2 == 0)
                cout << n/2 << endl;
            else
                cout << n/2+1 << endl;
            continue;
        }
        else
        {
            for(int i=0; i<cnt; i++)
            {
                if(abs(b[i]-b[i+1]) == 1)
                {
                    ans += 1;
                    i ++;
                }
                else
                {
                    ans += 1;
                }
            }
        }
        cout << ans << endl;
    }

    return 0;
}

欢迎指正与交流

好的,以下是第三的 Python 实现。这个问可以通过计算错排(Derangement)的数量来解决。错排是指一种排列方式,其中每个元素都不在其原来的位置上。 ### 第三:水桶盖和桶身不匹配的组合数量 #### 问描述 现有 6 个颜色各不相同的水桶,其桶身和桶盖颜色相同,现在将桶身和桶盖随机打乱组合,求全部桶盖和桶身都不匹配的组合有多少种?请把所有组合都打印出来。 #### 解决方案 我们可以通过生成所有可能的排列,然后过滤掉那些有匹配的情况来得到最终的结果。 ```python from itertools import permutations def is_derangement(arr): """检查是否为错排""" for i, x in enumerate(arr): if i == x: return False return True def find_all_derangements(n): """找到所有 n 个元素的错排""" elements = list(range(n)) all_permutations = permutations(elements) derangements = [perm for perm in all_permutations if is_derangement(perm)] return derangements def print_derangements(derangements): """打印所有的错排组合""" for derangement in derangements: print(derangement) # 主函数 if __name__ == "__main__": n = 6 derangements = find_all_derangements(n) print(f"总共有 {len(derangements)} 种全部桶盖和桶身都不匹配的组合:") print_derangements(derangements) ``` ### 解释 1. **is_derangement**: 检查给定的排列是否为错排。如果任何一个位置上的元素与其索引相同,则返回 `False`,否则返回 `True`。 2. **find_all_derangements**: 生成所有可能的排列,并过滤出那些是错排的排列。 3. **print_derangements**: 打印所有错排的组合。 4. **主函数**: 设置 `n=6`,调用 `find_all_derangements` 获取所有错排,并打印结果。 运行这段代码将会输出所有 6 个水桶盖和桶身都不匹配的组合及其总数。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值