当前位置:网站首页>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
}边栏推荐
- [question brushing] character statistics [0]
- STM32 uses esp01s to go to the cloud, mqtt FX debugging
- slice扩容机制分析
- 初学者如何正确理解google官方建议架构原则(疑问?)
- How to get a SharePoint online site created using the office365 group template
- Aardio - Method of self constructed geticonhandle
- Find the nearest n-th power of 2
- The Windows C disk is full
- AArdio - 【问题】bass库回调时内存增长的问题
- EDA open source simulation tool verilator beginner 6: debugging examples
猜你喜欢

Teach you how to apply for domestic trademark online step by step

Adding color blocks to Seaborn clustermap matrix

Access report realizes subtotal function

Utiliser Beef pour détourner le navigateur utilisateur

7-26 word length (input and output in the loop)

Soft keyboard height error

Aardio - 阴影渐变文字

LM08丨网格系列之网格反转(精)

P4 安装bmv2 详细教程

Using settoolkit to forge sites to steal user information
随机推荐
使用beef劫持用户浏览器
Deep learning systematic learning
Data analysis notes 11
手工挖XSS漏洞
web254
Gdip - hatchBrush图案表
window c盘满了
【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序
【力扣10天SQL入门】Day9 控制流
P4 installation bmv2 detailed tutorial
[untitled]
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion
[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)
getInputStream() has already been called for this request
Utiliser Beef pour détourner le navigateur utilisateur
【无标题】
[staff] key number (key number identification position | key number marking list | a major key identification principle | F, C, G position marking ascending | F major key identification principle | B
Connect timed out of database connection
一套十万级TPS的IM综合消息系统的架构实践与思考
Two expressions of string