当前位置:网站首页>Luogu p1088 [noip2004 popularization group] Martians
Luogu p1088 [noip2004 popularization group] Martians
2022-07-01 08:17:00 【Study_ Study_ X】
Portal :https://www.luogu.com.cn/problem/P1088
A full permutation problem , You can take the valley of Los Angeles P1706 Write again .
P1706: https://www.luogu.com.cn/problem/P1706
My first reaction when I saw the topic was DFS, Simulate the process of full permutation , Then find the order given by the Martians , And then look down M A full permutation will find the answer . But the data of the topic is open to 10000, It is obvious that such an approach will time out . So we can't find the order given by Martians slowly , It should be to lock the position of the array in the whole search process in a very short time , And then look down M Time . Ideas have , How to implement it in code ? Other steps are better , The key is how to quickly find the order given by the Martians ?
This leads to the core code jump of the solution if (!amount) i = ans[step]; Readers can read the code snippet until the line and then turn to the explanation .
This line of core code is to jump to the starting position of this search . Why do you write like this ? Let's start with 1 To 5 For example , In use DFS Realization 1 To 5 When it's all lined up , The first full permutation is 1 2 3 4 5 . Because there are cycles in our search , We were able to enumerate all the cases . So if you put one DFS The recursive work stack of , It's a multiple cycle , So this multi cyclic i It's not the same , Are in a specific position , The number searched is equal to the number . Because we need to quickly find the order given by Martians , Each of the multiple loops of the sequence must be determined i Value . therefore i=ans[step];ans It is the arrangement given by Martians , From this, we can determine each i.
#include<iostream>
#define MAX 10005
using namespace std;
int ans[MAX]; bool visit[MAX];
//ans An array is used to store the sort order ,visit Mark
int M = 0, N = 0, amount = 0;bool flag = false;
//amount Used to store the current full arrangement
//flag It is used to judge whether the answer has been found
void DFS(int step) {
if (flag)return;
// If you have found the answer, always return until DFS End of the function
if (step > N) {// The border
amount++;// A full permutation adds 1
if (amount == M+1) {
// The judgment conditions here should be noted that M+1
// Because the order given by the Martians at the beginning was also added amount in
flag = true;// eureka
for (int i = 1; i <= N; i++)
printf("%d ", ans[i]);
// Output answers and return
return;
}
}
for (int i = 1; i <= N; i++)
{
if (!amount) i = ans[step];
// Core code , The problem will be solved with understanding
// Jump to this DFS The corresponding position of
// There is a detailed explanation outside the code segment
if (!visit[i]) {
visit[i] = true;
ans[step] = i;
DFS(step + 1);
visit[i] = false;
}
// Typical full arrangement template
// If not, you can put P1706 Study the full permutation problem of, and then do this problem
}
}
int main(void)
{
scanf("%d %d", &N, &M);
for (int i = 1; i <= N; i++)
scanf("%d", &ans[i]);
DFS(1);// from 1 Begin your search
return 0;// End of the flower
}边栏推荐
- Transaction method call @transactional
- How to troubleshoot SharePoint online map network drive failure?
- Source code analysis of open source API gateway APIs IX
- [getting started] input n integers and output the smallest K of them
- [getting started] extract non repeating integers
- Analysis of slice capacity expansion mechanism
- [dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)
- 2022.6.30 省赛+蓝桥国赛记录
- Latex table
- slice扩容机制分析
猜你喜欢

Access报表实现小计功能

【Redis】一气呵成,带你了解Redis安装与连接

使用beef劫持用戶瀏覽器

Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization
![[dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)](/img/3e/75a1152f9cdf63c6779fdadec702a0.jpg)
[dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)

【入门】提取不重复的整数

CPU设计实战-第四章实践任务一简单CPU参考设计调试

SQL number injection and character injection

The Windows C disk is full

软键盘高度报错
随机推荐
Scala language learning-07-constructor
Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation
Airsim雷达相机融合生成彩色点云
[force deduction 10 days SQL introduction] Day9 control flow
Book of quantitative trading - reading notes of the man who conquers the market
Aardio - 阴影渐变文字
OJ input and output exercise
Insufficient executors to build thread pool
Anddroid 文本合成语音TTS实现
【入门】输入n个整数,输出其中最小的k个
Basic number theory -- combinatorial number
Gdip - hatchBrush图案表
【入门】取近似值
Scala语言学习-07-构造器
EDA开源仿真工具verilator入门6:调试实例
Sqlalchemy creating MySQL_ Table
Provincial election + noi Part VII computational geometry
5大组合拳,解决校园6大难题,护航教育信息化建设
Deep learning systematic learning
Leetcode T34: 在排序数组中查找元素的第一个和最后一个位置