当前位置:网站首页>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
}边栏推荐
- [getting started] intercepting strings
- Differential: definition of total differential, partial derivative, gradient
- Provincial selection + noi Part II string
- OJ input and output exercise
- 7-26 word length (input and output in the loop)
- getInputStream() has already been called for this request
- Contenttype comparison of all types
- Set up file server Minio for quick use
- Scala语言学习-07-构造器
- 力扣每日一题-第32天-1822.数组元素积的符号
猜你喜欢

web254

手工挖XSS漏洞
![[staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)](/img/ff/ebd936eaa6e57b1eabb691b0642957.jpg)
[staff] high and low octave mark (the notes in the high octave mark | mark range are increased by one octave as a whole | low octave mark | mark range are decreased by one octave as a whole)

seaborn clustermap矩阵添加颜色块

Use threejs simple Web3D effect

Aardio - Shadow Gradient Text
![[getting started] input n integers and output the smallest K of them](/img/b8/20852484f10bc968d529e9c1ff5480.png)
[getting started] input n integers and output the smallest K of them

软键盘高度报错
![[getting started] intercepting strings](/img/16/363baa4982408f55493057200bcba5.png)
[getting started] intercepting strings

Instead of houses, another kind of capital in China is rising
随机推荐
web254
AArdio - 【问题】bass库回调时内存增长的问题
Aardio - [problem] the problem of memory growth during the callback of bass Library
CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging
Provincial election + noi part I dynamic planning DP
seaborn clustermap矩阵添加颜色块
Utiliser Beef pour détourner le navigateur utilisateur
Erreur de hauteur du clavier souple
【入门】截取字符串
Gdip - hatchbrush pattern table
使用threejs简单Web3D效果
软键盘高度报错
Uni hot update
Learn reptiles for a month and earn 6000 a month? Tell you the truth about the reptile, netizen: I wish I had known it earlier
栈实现计算器
sqlalchemy创建MySQL_Table
LM08丨网格系列之网格反转(精)
Teach you how to apply for domestic trademark online step by step
leetcode T31:下一排列
Source code analysis of open source API gateway APIs IX