【OJ for Divide and Conquer】OJ题解

文章目录

  • A - Ultra-QuickSort
  • B - Hanoi Tower Troubles Again! [找规律+递归]
  • C - Fibonacci Again[找规律]
  • E - [Fire Net](https://programmerall.com/article/7276104269/)[DFS 搜索 ⭐⭐]
  • F - Gridland[找规律]
  • G - Maximum Subarray Sum[动态规划/分治..经典⭐]
  • I - Quoit Design[最近点对距离]

A - Ultra-QuickSort

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
9 1 0 5 4 ,

Ultra-QuickSort produces the output
0 1 4 5 9 .

Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
简单来说,就是有一组无序的数组,求两两交换的最小次数使得数组升序排列。
分析:要使得数组升序排列,
here 🙋‍ 查看归并算法总结
我们只需要在归并排序的算法中,在合并时,判断前面子数列的值是否大于后面子序列的值。
在这里插入图片描述
注意一个易错点,每轮loop都必须要指定退出条件,在这里条件就是当两个子序列的指针指向同一个数时,退出。

if(l>=r) return;

整体代码如下,

#include<iostream>
#include<vector>
using namespace std;
int arr[500010];
int tmp[500010];
int sum=0;
//归并算法
void mergesort(int l,int r){if(l>=r) return;int mid=(l+r)/2;mergesort(l,mid);mergesort(mid+1,r);int i=l,j=mid+1,k=l;while(i<=mid && j<=r){// cout<<"f";       while(arr[i]<=arr[j] && i<=mid) tmp[k++]=arr[i++];while(arr[i]>arr[j] && j<=r) {tmp[k++]=arr[j++];sum+=mid+1-i;}}while(i<=mid) tmp[k++]=arr[i++];while(j<=r) tmp[k++]=arr[j++];for(int k=l;k<=r;k++)arr[k]=tmp[k];
}
int main(){int n;while(cin>>n && n){for(int i=0;i<n;i++){cin>>arr[i];}sum=0;mergesort(0,n-1);cout << sum << endl;}
}

B - Hanoi Tower Troubles Again! [找规律+递归]

People stopped moving discs from peg to peg after they know the number of steps needed to complete the entire task. But on the other hand, they didn’t not stopped thinking about similar puzzles with the Hanoi Tower. Mr.S invented a little game on it. The game consists of N pegs and a LOT of balls. The balls are numbered 1,2,3… The balls look ordinary, but they are actually magic. If the sum of the numbers on two balls is NOT a square number, they will push each other with a great force when they’re too closed, so they can NEVER be put together touching each other.
The player should place one ball on the top of a peg at a time. He should first try ball 1, then ball 2, then ball 3… If he fails to do so, the game ends. Help the player to place as many balls as possible. You may take a look at the picture above, since it shows us a best result for 4 pegs.

简单来说,看每次提供的柱子数可以承载多少个圆盘,使得每根柱子上相邻两圆盘的加和必定为平方数。
分析:这道题看起来很难,实际上从简单情况入手。先看一根柱子、再看两根柱子…发现能承载圆盘的数量依次为1、3、7、11、17、23,每两个之间相差2、4、4、6、6…于是我们发现了规律。实际上,用数学方法证明也能得出该结论。
在这里插入图片描述

递推公式: H a n o i ( i ) = H a n o i ( i − 1 ) + i + i % 2 Hanoi(i)=Hanoi(i-1)+i+i \% 2 Hanoi(i)=Hanoi(i1)+i+i%2
考虑到题目中有多次求解,所以我们用表格的方式将结果先提前算好存储起来。

void Hanoi(){table[1]=1;table[2]=3;for(int i=3;i<N;i++)table[i]=table[i-1]+i+i%2;
}

全部代码也就很明晰了:

#include<iostream>
using namespace std;
#define N 55
int table[N];
//打表过程 避免重复计算
void Hanoi(){table[1]=1;table[2]=3;for(int i=3;i<N;i++)table[i]=table[i-1]+i+i%2;
}
int main(){int n,x;cin>>n;Hanoi();for(int i=0;i<n;i++){cin>>x;cout<<table[x]<<endl;}
}

补充:

  • 宏定义格式
    #define 宏名 替换文本
#define N 10;               // 宏定义
int c[N];                   // 会被替换为: int c[10;]; 

C - Fibonacci Again[找规律]

There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2)

Input

Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000)

Output

Print the word “yes” if 3 divide evenly into F(n).

Print the word “no” if not.

分析:注意 斐波那契数列会议非常快的速度增长,因此直接计算出结果并和3运算取余的做法不可取。我们考虑列出前面部分结果,试图找规律。嘿,还真成功了!

012345678910
71118294776123199322521843

观察表格:发现每4个为一组。在数组的第3个数时,可以被整除,也就是输出yes.

int main(){int n;int f[4]={0,0,1,0};while(cin>>n){if(f[n%4]) cout<<"yes"<<endl;else cout<<"no"<<endl;}
}

E - Fire Net[DFS 搜索 ⭐⭐]

Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.
A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.
Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.
The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.
Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.
分析:
map中各个符号的意义:

X.%
有墙(可以避开💣)啥也没有有碉堡(可以四处散布💣)

采用DFS的思想

深度优先搜索的步骤分为
1.递归下去 2.回溯上来。
顾名思义,深度优先,则是以深度为准则,先一条路走到底,直到达到目标。这里称之为递归下去。否则既没有达到目标又无路可走了,那么则退回到上一步的状态,走其他路。这便是回溯上来。

总结DFS模板:【注意恢复现场这一步】
在这里插入图片描述

这道题的IDEA:

从左上角的元素开始进行深度搜索,一直搜索到右下角位置。使用check检查函数判断当前搜索到的位置能不能放置碉堡,若是可以放置,则将本轮的总和加一,并将此处标记为%,意思是有一座碉堡。然后继续进行深度搜索。如果已经到了最右下角位置,则回溯并记录最大值。

#include<iostream>
#include<algorithm>
using namespace std;
int n,ans=0;
char arr[5][5];int check(int x,int y){for(int i=x-1;i>=0;i--){if(arr[i][y]=='%') return false;if(arr[i][y]=='X') break;}for(int i=x+1;i<n;i++){if(arr[i][y]=='%') return false;if(arr[i][y]=='X') break;}for(int i=y-1;i>=0;i--){if(arr[x][i]=='%') return false;if(arr[x][i]=='X') break;}for(int i=x+1;i<n;i++){if(arr[x][i]=='%') return false;if(arr[x][i]=='X') break;}return true;
}//s表示当前的状态吧
void dfs(int s,int sum){if(s==n*n){ans=max(ans,sum);return;}int x=s/n;int y=s%n;if(arr[x][y]=='.' && check(x,y)){arr[x][y]='%'; //表示成功加入一个碉堡dfs(s+1,sum+1); //继续深度搜索arr[x][y]='.'; //!还原标记!}dfs(s+1,sum);
}int main(){while(cin>>n && n){ans=0;for(int i=0;i<n;i++)cin>>arr[i]; dfs(0,0);cout<<ans<<endl;}
}

F - Gridland[找规律]

For years, computer scientists have been trying to find efficient solutions to different computing problems. For some of them efficient algorithms are already available, these are the “easy” problems like sorting, evaluating a polynomial or finding the shortest path in a graph. For the “hard” ones only exponential-time algorithms are known. The traveling-salesman problem belongs to this latter group. Given a set of N towns and roads between these towns, the problem is to compute the shortest path allowing a salesman to visit each of the towns once and only once and return to the starting point.

The president of Gridland has hired you to design a program that calculates the length of the shortest traveling-salesman tour for the towns in the country. In Gridland, there is one town at each of the points of a rectangular grid. Roads run from every town in the directions North, Northwest, West, Southwest, South, Southeast, East, and Northeast, provided that there is a neighbouring town in that direction. The distance between neighbouring towns in directions North–South or East–West is 1 unit. The length of the roads is measured by the Euclidean distance. For example, Figure 7 shows 2 × 3-Gridland, i.e., a rectangular grid of dimensions 2 by 3. In 2 × 3-Gridland, the shortest tour has length 6.
在这里插入图片描述
分析:初一看很难,但只要从简单情况出发,找寻规律,就可得到意外之喜😊
只要多做几组你就会发现规律:只要n,m有一个为偶数,答案为n*m;都为基数,答案为n*m-1+sqrt(2)
下图是当时很潦草的草稿…浅浅看一下吧❤
在这里插入图片描述

#include<iostream>
#include<math.h>using namespace std;int main(){int N;double ans;cin>>N;for(int i=1;i<=N;i++){int n,m;cin>>m>>n;if((m*n)%2) ans=m*n*1.0-1+sqrt(2);else ans=m*n*1.0;cout<<"Scenario #"<<i<<":\n";printf("%.2f\n\n",ans);}
}

补充一个知识点:

  • prinf输出浮点数

>% - 0 m.n 格式字符
下面对组成格式说明的各项加以说明:
①%:表示格式说明的起始符号,不可缺少。
②-:有-表示左对齐输出,如省略表示右对齐输出。
③0:有0表示指定空位填0,如省略表示指定空位不填。
④m.n:m指域宽,即对应的输出项在输出设备上所占的字符数。N指精度。用于说明输出的实型数的小数位数。为指定n时,隐含的精度为n=6位。

G - Maximum Subarray Sum[动态规划/分治…经典⭐]

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
寻找和最大的子序列,并返回最大值
分析:
刚拿到这道题就想到了用动态规划的方法做。在每一步,我们维护两个变量,一个是全局最优,就是到当前元素为止最优的解是,一个是局部最优,就是必须包含当前元素的最优的解。其中这个局部最优,要么是从这个元素开始的新序列,要么是包含上一个元素的序列。
dp[i]=max(dp[i-1]+arr[i],arr[i]) dp是维护的局部最优解,arr是输入的数组。

#include<iostream>
#include<algorithm>
#include<climits> 
using namespace std;
#define N 200100
int main(){int dp[N];int arr[N];int ans=INT_MIN;//INT_MIN:-2147483648 || INT_MAX:2147483647int n;cin>>n;for(int i=0;i<n;i++)cin>>arr[i];dp[0]=arr[0];for(int i=1;i<n;i++){dp[i]=max(dp[i-1]+arr[i],arr[i]);ans=max(ans,dp[i]);}cout<<ans<<endl;}

注意这道题目易错点是数组大小的开辟,小心审题就是了。

  1. 做题时候遇到了:runtime error (运行时错误)就是程序运行到一半,程序就崩溃了。可能有以下几种情况:

    ①除以零
    ②数组越界:int a[3]; a[10000000]=10;
    ③指针越界:int * p; p=(int *)malloc(5 * sizeof(int)); *(p+1000000)=10;
    ④使用已经释放的空间:int * p; p=(int *)malloc(5 * sizeof(int));free§; *p=10;
    ⑤数组开得太大,超出了栈的范围,造成栈溢出:int a[100000000];

I - Quoit Design[最近点对距离]

Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a configuration of the field, you are supposed to find the radius of such a ring.

Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered to be 0.
Input
The input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated by N = 0.
Output
For each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.

分析:这道题的难点在于读懂题!想要最大的圈直径,使得圈只能套到一个物品。其实就是找最近的两个点,之间的距离就是目标圈的直径。那么就变成了最近点对问题。链接 这个链接展示了详细的求解过程,可以仔细研究。
仔细看

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/123115.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++深度优先(DFS)算法的应用:收集所有金币可获得的最大积分

涉及知识点 深度优化(DFS) 记忆化 题目 节点 0 处现有一棵由 n 个节点组成的无向树&#xff0c;节点编号从 0 到 n - 1 。给你一个长度为 n - 1 的二维 整数 数组 edges &#xff0c;其中 edges[i] [ai, bi] 表示在树上的节点 ai 和 bi 之间存在一条边。另给你一个下标从 0…

单目深度估计之图像重构原理解析

一、参考资料 浅析自监督深度估计中的光度损失(Photometric Loss) 二、图像重构原理 设输入位姿估计网络的3帧连续单目序列为 < I t − 1 , I t , I t 1 > <I_{t-1},I_{t},I_{t1}> <It−1​,It​,It1​>&#xff0c;其中 t t t 为时间索引&#xff0c;…

SA实战 ·《SpringCloud Alibaba实战》第12章-服务网关:网关概述与核心架构

作者:冰河 星球:http://m6z.cn/6aeFbs 博客:https://binghe.gitcode.host 文章汇总:https://binghe.gitcode.host/md/all/all.html 大家好,我是冰河~~ 一不小心《SpringCloud Alibaba实战》专栏都更新到第12章了,再不上车就跟不上了,小伙伴们快跟上啊! 在《SpringClou…

电商API接口——实现数据同步的实时数据封装接口

常见的数据同步/集成场景多发生于不同的存储系统、不同的存储格式&#xff0c;如从 mysql 同步数据至数仓、excel 或 csv 导入数据库中&#xff0c;但是众多数据同步解决方案很少涉及从 http 接口同步数据。 如淘宝、拼多多等电商平台平台&#xff0c;平台内部不同团队之间的数…

pre-existing shared memory block

发生原因: 1.服务器cpu、内存进行扩容 2.非正常关闭,导致任在占用共享内存段 解决方案: 根据shmid进行关闭 ipcs -mipcrm -m xxx

JavaScript进阶知识汇总~

JavaScript 进阶 给大家推荐一个实用面试题库 1、前端面试题库 &#xff08;面试必备&#xff09; 推荐&#xff1a;★★★★★ 地址&#xff1a;web前端面试题库 1.原型链入门 1) 构造函数 当我们自定义一个函数时(箭头函数与生成器函数除外)&#xff0c;这个函…

LV.12 D13 C工程与寄存器封装 学习笔记

一、C语言工程简介 把模板在linux解压出来 代码写在interface.c就可以了。 map.lds是链接脚本文件&#xff08;负责代码的排布&#xff09; include中是头文件&#xff0c;src中是写好的源代码 start.s是启动代码&#xff0c;在interface.c之前运行&#xff0c;把cpu和栈做一…

mac vscode 使用 clangd

C 的智能提示 IntelliSense 非常不准&#xff0c;我们可以使用 clangd clangd 缺点就是配置繁琐&#xff0c;优点就是跳转和提示代码精准 开启 clangd 之后会提示你关闭 IntelliSense 1、安装插件 clangd 搜索第一个下载多的就是 2、配置 clangd 可执行程序路径 clangd 插…

【数据结构】面试OJ题——时间复杂度2

目录 一&#xff1a;移除元素 思路&#xff1a; 二&#xff1a;删除有序数组中的重复项 思路&#xff1a; 三&#xff1a;合并两个有序数组 思路1&#xff1a; 什么&#xff1f;你不知道qsort&#xff08;&#xff09; 思路2&#xff1a; 一&#xff1a;移除元素 27. 移…

L2-1 插松枝

L2-1 插松枝 分数 25 全屏浏览题目 切换布局 作者 陈越 单位 浙江大学 人造松枝加工场的工人需要将各种尺寸的塑料松针插到松枝干上&#xff0c;做成大大小小的松枝。他们的工作流程&#xff08;并不&#xff09;是这样的&#xff1a; 每人手边有一只小盒子&#xff0c;初始…

IDEA 2023.2.2 使用 Scala 编译报错 No scalac found to compile scala sources

一、问题 scala: No scalac found to compile scala sources 官网 Bug 链接 二、临时解决方案 Incrementality Type 先变成 IDEA 类型 Please go to Settings > Build, Execution, Deployment > Compiler > Scala Compiler and change the Incrementality type to …

如何使用批量重命名的方法替换重复文件名内容

在文件管理过程中&#xff0c;我们有时会遇到文件名中包含相同部分内容的情况&#xff0c;这不仅会使文件显得混乱&#xff0c;而且还会给文件检索和使用带来不便。为了解决这个问题&#xff0c;我们可以使用云炫文件管理器批量重命名进行批量替换。下面是如何使用这种方法进行…

机器学习---使用 TensorFlow 构建神经网络模型预测波士顿房价和鸢尾花数据集分类

1. 预测波士顿房价 1.1 导包 from __future__ import absolute_import from __future__ import division from __future__ import print_functionimport itertoolsimport pandas as pd import tensorflow as tftf.logging.set_verbosity(tf.logging.INFO) 最后一行设置了Ten…

面试测试工程师一般问什么问题?

面试和项目一起&#xff0c;是自学路上的两大拦路虎。面试测试工程师一般会被问什么问题&#xff0c;总结下来一般是下面这4类&#xff1a; 1.做好自我介绍 2.项目相关问题 3.技术相关问题 4.人事相关问题 接下来&#xff0c;主要从以上四个方向分别展开介绍。为了让大家更有获…

[双指针](一) Leetcode 283.移动零和1089.复写零

[双指针] Leetcode 283.移动零和1089.复写零 移动零 283. 移动零 1.题意分析 (1) 给你一个数组&#xff0c;将数组中的所有0移动到数组的末尾 (2) 保证非0元素在数组中相对位置不变 (3) 在原数组中操作 2.解题思路 由于题目要求我们移动数组内容&#xff08;也就是交换两…

离线语音通断器开发-稳定之后顺应新需求

使用云知声的US516p6方案开发了一系列的离线语音通断器&#xff0c;目前已经取得了不小的收获&#xff0c;有1路的&#xff0c;3路的&#xff0c;4路的&#xff0c;唛头和扬声器包括唛头线材也在不断的更新打磨中找到了效果特别好的供应商。 离线语音通断器&#xff0c;家用控…

Beyond Compare比较规则设置 Beyond Compare怎么对比表格

在对文件进行比较时&#xff0c;文件夹内的文件可能存在不同类型、不同后缀名、不同内容等差异&#xff0c;这些差异会影响具体的比较结果&#xff0c;因此需要我们对软件的比较规则进行一些设置。接下来就让我们一起来学习一下Beyond Compare比较规则设置&#xff0c;Beyond C…

重构之美:Java Swing中 如何对指定行文本进行CSS样式渲染,三种实现思路分享

文章目录 需求分析Document 应用彩蛋 需求分析 在Swing中&#xff0c;如果期望实现对JTextArea 或者 TextPane等文本区域实现单行渲染改怎么做&#xff1f;如上图所示 总的来说有两种实现方案 文本行数可控&#xff0c;那么构造一组JLabel集合按表单顺序添加&#xff0c;这样可…

松下A6B伺服 马达不动问题解决

本人在用信捷XDH plc ethercat总线&#xff0c;连松下A6B伺服&#xff0c;轴配置完成轴调试时&#xff0c;出现能使能&#xff0c;但 马达不动的情况。 开始总怀疑时信捷PLC的原因&#xff0c;后面查明是输入口定义引起的。 用USB线连接伺服&#xff0c;打开PANAPARM软件,自…

在Mac上安装MongoDB 5.0

MongoDB 5.0安装 1、环境描述 操作系统&#xff1a;macOS 14.0 (23A344) 2、安装MongoDB 2.1、tar解压包安装 下载地址&#xff1a;Download MongoDB Community Server | MongoDB 创建一个目录&#xff0c;以便数据库将文件放入其中。&#xff08;默认情况下&#xff0c;数据…