UVA 331 - Mapping the Swaps(回溯)

详细对比模型训练和模型精调 当我们进行模型精调(Fine-Tuning)和模型训练时,我们需要考虑到不同的步骤和目标。 阅读详情

Mapping the Swaps 

Sorting an array can be done by swapping certain pairs of adjacent entries in the array. This is the fundamental technique used in the well-known bubble sort. If we list the identities of the pairs to be swapped, in the sequence they are to be swapped, we obtain what might be called a swap map. For example, suppose we wish to sort the array A whose elements are 3, 2, and 1 in that order. If the subscripts for this array are 1, 2, and 3, sorting the array can be accomplished by swapping A2 and A3, then swapping A1 and A2, and finally swapping A2 and A3. If a pair is identified in a swap map by indicating the subscript of the first element of the pair to be swapped, then this sorting process would be characterized with the swap map 2 1 2.

It is instructive to note that there may be many ways in which swapping of adjacent array entries can be used to sort an array. The previous array, containing 3 2 1, could also be sorted by swapping A1 and A2, then swapping A2 and A3, and finally swapping A1 and A2 again. The swap map that describes this sorting sequence is 1 2 1.

For a given array, how many different swap maps exist? A little thought will show that there are an infinite number of swap maps, since sequential swapping of an arbitrary pair of elements will not change the order of the elements. Thus the swap map 1 1 1 2 1 will also leave our array elements in ascending order. But how many swap maps of minimum size will place a given array in order? That is the question you are to answer in this problem.

Input

The input data will contain an arbitrary number of test cases, followed by a single 0. Each test case will have a integer n that gives the size of an array, and will be followed by the n integer values in the array.

Output

For each test case, print a message similar to those shown in the sample output below. In no test case will n be larger than 5.

Sample Input

2 9 7
2 12 50
3 3 2 1
3 9 1 5
0

Sample Output

There are 1 swap maps for input data set 1.
There are 0 swap maps for input data set 2.
There are 2 swap maps for input data set 3.
There are 1 swap maps for input data set 4.

===================================

就是看有多少种交换方法能够使给出的数列升序排序


注意已经是升序的时候输出0所以要加一句 if ( !check() )


#include <iostream>
#include <cstdio>

using namespace std;

int n,a[11],ans;

bool check()
{
    for(int i=1;i<n;i++)
    {
        if(a[i]<a[i-1])
            return 0;
    }
    return 1;
}

void dfs()
{
    if(check())
    {
        ans++;
        return;
    }
    for(int i=0;i<n;i++)
    {
        if(a[i-1]>a[i])
        {
            swap(a[i],a[i-1]);
            dfs();
            swap(a[i],a[i-1]);
        }
    }
}

int main()
{
    int t=0;
    while(~scanf("%d",&n))
    {
        if(n==0) break;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        ans=0;
        if(!check())
            dfs();
        printf("There are %d swap maps for input data set %d.\n",ans,++t);
    }
    return 0;
}


计算机视觉安防监控升级:人脸识别与行为异常检测 计算机视觉安防监控升级:人脸识别与行为异常检测 ,人工智能,计算机视觉,大模型,AI,本文将深入探讨人脸识别与行为异常检测技术在安防监控中的应用,包括技术原理、算法实现、应用场景以及面临的挑战与解决方案。通过实际案例和代码示例,帮助读者更好地理解和掌握这些技术,为推动安防监控系统的智能化升级提供参考。 阅读详情

相关推荐

别再只看K线了!聊聊“板块联动”和“热点轮动”的跟踪方法与工具(实战派分享)

本文深入解析了如何通过板块联动和热点轮动构建高效交易系统。从三维验证框架到主流工具配置,再到持续性热点的五大特征,实战派交易员分享了识别市场主线和预判轮动方向的高阶技巧,帮助投资者摆脱单纯依赖K线的局限,提升交易决策效率。

weixin_29281941的博客 760

uva 331 Mapping the Swaps(DFS+回溯)

Mapping the Swaps  Sorting an array can be done by swapping certain pairs of adjacent entries in the array. This is the fundamental technique used in the well-known bubble sort. If we l

不慌不忙、不急不躁 1121

DeepSeek-R1模型+Dify实战:5步打造能读PDF的智能客服(附BGE-M3嵌入模型配置)

本文详细介绍了如何利用DeepSeek-R1大模型与Dify平台,结合BGE-M3嵌入模型,构建企业级智能文档问答系统。通过5个步骤,从环境准备到知识库构建,再到智能问答应用开发,帮助企业快速从PDF等文档中提取关键信息,提升工作效率。特别适合处理产品手册、内部规程等专业内容。

convnet3designer的博客 720

uva 331 - Mapping the Swaps

Mapping the Swaps  Sorting an array can be done by swapping certain pairs of adjacent entries in the array. This is the fundamental technique used in the well-known bubble sort. If we list

uva code 871

LVGL仪表盘实战:5分钟用lv_meter打造汽车仪表UI(附完整代码)

本文详细介绍了如何使用LVGL的lv_meter组件在5分钟内构建专业汽车仪表盘UI。从环境配置、刻度定制到指针动画和性能优化,提供完整代码示例和实战技巧,帮助开发者快速实现嵌入式设备上的高效仪表盘设计。

k5l6m的博客 422

别再只盯着YOLOv8了!手把手带你用PaddleDetection的PP-YOLOE+搞定你的第一个检测项目

本文介绍了PP-YOLOE+在目标检测领域的优势与应用实践,对比YOLOv8展示其在精度、速度和部署便利性上的突破。通过详细的代码示例和实战教程,帮助开发者快速上手PaddleDetection工具链,实现产业级目标检测项目,提升开发效率。

weixin_42701915的博客 185

UVA331 Mapping the Swaps

讲道理,这都能A我也是醉了。。。 题目就是给你一串序列 然后要你给这个序列排序(排序方式是两两交换) 一个是要算出最少的交换步数 另一个是在步数最少的情况下求出有几种交换方式 递归暴力#include #include #include using namespace std; int con[10]; int num; int bestnum; void swap(int

再见孙悟空 389

武汉大学朱玉贤院士团队再发Nature Genetics,阐述棉花独特折叠胚胎形成的分子调控与演化机制

武汉大学朱玉贤院士团队再发Nature Genetics,阐述棉花独特折叠胚胎形成的分子调控与演化机制。

Pangenes的博客 1335

uva 331 - Mapping the Swaps

点击打开链接 题目意思:给定一个无序的序列,要求找到一个交换次数最少的方案使得序列为有序列,然后再找到以该最少次数为方案的总数是多少,输出方案数 解题思路:根据冒泡排序的方法,我们将可以得到最少的交换次数,然后问我们在这个次数下的交换方式有几种。我们可以利用冒泡排序的思想,就是我们每一次递归下去的时候都从0这个点开始搜索,只要有递归就判断当前的序列是否排好序,如果序列排好那么ans++...

编程资料大全 178

当预编译包失效时:手把手教你在Nvidia Orin (JetPack 5.1.1) 上源码编译onnxruntime-gpu

本文详细指导在Nvidia Orin (JetPack 5.1.1) 上源码编译onnxruntime-gpu的全流程,解决预编译包兼容性问题。涵盖环境准备、工具链配置、源码编译、常见问题解决及性能优化技巧,帮助开发者在边缘计算设备上实现高效AI模型推理。

weixin_29062823的博客 135

网络变更:APIC 节点替换

Fabric Name,Controller ID,TEP Address Pool 等信息必须相同;如果故障设备是 APIC1 还需要重新配置管理密码。此过程可能需要 10 分钟左右,在此期间目标 APIC 可能在 Available/Unavailable 状态翻滚。APIC 获取 TEP 地址后,进入 fully-fit。通过 CLI 以及配置的密码登录目标 APIC。

m0_54931486的博客 321

uva 331 Mapping the Swaps 求交换排序的map 纯DFS

给出一个序列,每次交换两个数,求有几种交换方法能使序列变成升序。 n不大于5,用dfs做。 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int num[8], ans, n; bool check() { //check if...

weixin_30335353的博客 145

2025年上半年第1批信息系统项目管理师论文真题解析与范文

文章首先分析了项目经理在交付绩效域中应关注的重点领域,然后按照项目管理五大过程组顺序,阐述了各过程组中与交付绩效域协同的绩效域及协调目标,最后制定了各过程组中交付绩效域的测量指标。本项目的实践表明,系统化的交付绩效管理不仅能保证项目成功,还能创造超出预期的业务价值。需要结合具体项目案例,按照五大过程组(启动、规划、执行、监控、收尾)顺序,分析各过程组中与交付绩效域协同的其他绩效域及其协调目标。价值交付连续性:建立敏捷交付机制,将项目分解为12个迭代周期,每周期交付可验证成果,确保价值持续流动。

Programming Talk 822

uva 331 Mapping the Swaps (回溯)

uva 331 Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the array. This is the fundamental technique used in the well-known bubble sort. If we li

总有那么一群人喜欢盯着我的博客看,估计这是他们见过最长的博客名,我必须得告诉他们博客名最长可以写多长 744

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

[回溯]Mapping the Swaps UVA331

回溯求不同交换排序有多少种。  Mapping the Swaps  Sorting an array can be done by swapping certain pairs of adjacent entries in the array. This is the fundamental technique used in the well-known bu

九问的烦恼 1309

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

UVA - 331 Mapping the Swaps(回溯)

Mapping the Swaps  Sorting an array can be done by swapping certain pairs of adjacent entries in the array. This is the fundamental technique used in the well-known bubble sort. If we lis

小G的ACM之路 676

UVA 331 Mapping the Swaps (回溯+dfs)

题意是:求一个数列经最少步骤变为由大到小一共有多少种排法。 思路:用冒泡法所需步骤就最少。然后用dfs搜索求出排法数。    Mapping the Swaps  Sorting an array can be done by swapping certain pairs of adjacent entries in the array. This

risker_2013的专栏 689

UVA 331 Mapping the Swaps

UVA 331 Mapping the Swaps 题意。给一串数字。求其冒泡排序(最少次数的)方案的种数。。 标准的深搜回溯 每次交换值后进行一次judge。如果从小到大了,就种数加1. #include #include int min; int n; int num[5]; int judge() { for(int i = 0; i

Remilia's 1137

UVa 331 - Mapping the Swaps

题目链接:UVa 331 - Mapping the Swaps 最开始做这道题想着是dfs中循环一直向前走,后来想这样不对,因为可能后边交换过后再交换前边的。然后又想双向回溯,也是不对。 再后来发现只要交换前的两个元素是降序排列的,那么这么交换肯定是具有最少交换次数的。这一点题目中也已经隐含了,题目中举了一个例子说明什么样的不是最少次数,这个例子就出现了交换前的两个元素已经是升序排列了,所以

fobdddf的专栏 665

uva 331 Mapping the Swaps

求给定数组相邻元素的交换次数!简单的回溯! #include #include #include #include #include #include using namespace std; int n,ans,minn; int num[6]; inlin

Xiao_Peng 395

UVa 331 - Mapping the Swaps

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=267 题目类型: 回溯 原题: Sorting an array can be done by swapping certain pairs

D_Double's Journey 1558

UVA - 331 Mapping the Swaps

题目大意:只能左右互换,要求从小到大排序,问有几种移动方法 及

静静地码 636

UVA 331 - Mapping the Swaps

题目大意: #include #include using namespace std; int arr[10], sum, n; bool check() { for (int i = 0; i < n - 1; i++) if (arr[i] > arr[i + 1]) return false; return true; } int dfs() { for (

Bingo 1729
上一篇: UVA 10344 - 23 Out of 5(回溯)
下一篇: UVA 10763 - Foreign Exchange(二分查找)
coco430
博客等级 码龄14年 9粉丝 107原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值