catalogue
1, Foreword
Hahaha, it's really exciting today. It's from a small konjac who has just finished the final examination of Si Xiu. I really didn't expect that the Si Xiu examination can run out in 2 hours! Then, because there will be an English exam tomorrow, an English boss named Gouzi said that there is nothing to review in English, but I still panic about English. So, in order to think of a way to practice English and blog, let's give you or Amway, a new algorithm website! Yes, he is CF(codeforces) , it's a gun fight game (ah, no), and then, because I talked about the template of deep search last time when I talked about subsets, and today I just brush a DFS topic every day, so, let's start!
2, Title Description
① Students who want to practice English can read this question carefully to see if they can understand the question. What do you want us to do!
② The following is the input and output of the sample (read the question carefully! There is an implication)
3, Topic interpretation
In order to help you quickly understand the topic, take one of the examples as an example. When n is 5, there are 5 numbers of 1-5 generation operations. Then, according to the meaning of the topic, it is proved that the set composed of these 5 numbers can be empty with n operations! So what is the operation? One person selects an interval [l,r], the other person selects a number d from this interval, and then simply delete D, and then put the remaining numbers into the set again, so don't think too complicated! Finally, if you pass 5 operations, and the D of each operation is within the interval selected by one person! The overall process is as follows
4, Train of thought analysis
Why is this topic a deep search topic? What are we searching for? In fact, when I first thought about this topic, I didn't think of DFS so quickly. At the beginning, I actually thought of greed. Careful students should find that if the given interval L and r are equal, there is no doubt that the selected d must be l or r. after judging these special situations? If it is the upper 15 interval, how to determine that 3 is selected? In fact, we will think of search here. We pay attention to that D is a different number in 1-n, and the final answer will only have one solution. Then, we use DFS to search for its unique solution, then record the solution in an array, and finally output it. So when we write the code, we do a sort. Why do we do it? The reason is that we want to search in front of the smaller interval. One is that the smaller the interval, the fewer answers we can try in that search. Then we push it to the larger interval, Because the number of elements accessed by the array vis [] is increasing, it will also reduce the time complexity of large range search, which is also a very good greedy sorting idea to reduce the time complexity of this problem.
5, Complete code
#include<bits/stdc++.h> using namespace std; int t,n; //t represents the number of rounds of the game, and n represents n per round of the game //ans1 records the results of the x-th search. ans1 is finally assigned to ans in order, and vis accesses the array. int ans1[1005],ans[1005],vis[1005]; struct game //Record the structure of interval l and r of each round of game { int l,r; }ss[1005]; bool com(game s1,game s2) //Comparison function to be used by sort { return (s1.r-s1.l)<(s2.r-s2.l); } void dfs_game(int x,int times) //Deep search template, routine { if(x>times) //The only set of solutions has been found { for(int i=1;i<=times;i++) ans[i]=ans1[i]; return; } for(int i=ss[x].l;i<=ss[x].r;i++) { if(ss[x].l==ss[x].r) //Special judgment, reduce subsequent search (pruning) { ans1[x]=ss[x].l; vis[ss[x].l]=1; dfs_game(x+1,times); } if(!vis[i]) //If this number has not been accessed { ans1[x]=i; //d representing x accesses is i vis[i]=1; //Representative visited dfs_game(x+1,times); vis[i]=0; //Backtracking operation } } } int main() { cin>>t; for(int i=1;i<=t;i++) { cin>>n; for(int j=1;j<=n;j++) { cin>>ss[j].l>>ss[j].r; } sort(ss+1,ss+1+n,com); dfs_game(1,n); for(int j=1;j<=n;j++) { cout<<ss[j].l<<" "<<ss[j].r<<" "<<ans[j]; cout<<endl; } memset(vis,0,sizeof(vis)); //Be sure to reset the access array to avoid interval collision and the search will be affected } }
6, AC voucher
7, Shui Hua
What do you say today? In fact, I feel that no one may read the water talk every time, but this water talk is like I give up writing a diary, but it's another form of diary. Yesterday and this morning, I read the meditation book for a day, but I still don't feel ready to answer some multiple-choice questions today. It seems that I still don't have a good liberal arts thinking! Today, I suddenly came up with an idea that I should take the mature route. I feel that sometimes I am no different from children, and I speak childishly. My personality and temper are a little childish. It doesn't feel like an adult at all. Yesterday I saw "Xiaomin's house". The cp in it was really knocked! Also, do many girls like handsome boys? Remember when she broke up with that friend, the last thing she said was that you should be more natural and unrestrained! It seems that I have to understand the natural and unrestrained words!