T1 (Light)
Description
? There are n bulbs in a row. Each light bulb may be lit or extinguished. There is a machine that controls the light bulb, each time you can choose a period, so that all the lights off in this interval, light bulbs are all off. But because the machine is aging, it can only be used again.
? You can think of the light bulb alternating with the extinguished bulb (hereinafter referred to as the alternating column). Now, you want to cherish the last chance to manipulate the bulb, making the longest alternating column in the bulb as long as possible.
? For example, this light bulb should look like this (0 for a lighted bulb, an off bulb):
? XX 0
? If you choose a 4th to 7th bulb, it will become the following form:
? XX 0 0
? At this point, the longest alternating is listed as the 2nd to the 8th bulb, with a length of 7.
? If you select only the 88th light bulb, it will become the following form:
? XX 0 0 0
? At this point, the longest alternating is listed as the 4th to the 10th bulb, and the length is 7.
? It can be found that there is no method in this example to make the longest alternating column length greater than 77, then 77 is the answer.
? Input
? The first line of the input file is a positive integer n, indicating the number of bulbs. The second row contains n digits, each of which is 0 or 1, which in turn represents the initial state of each bulb in the sequence. 1 is lit and 0 is off.
? Output
? Outputs an integer that represents the length of the longest alternating column in all the available bulb columns.
The first question means that there is an initial 01-string, the 01-phase interval is beautiful, and we only have one chance to flip all the values in a certain interval, that is, 0 becomes 1, 1 becomes 0. The problem is finding out the length of the longest beautiful interval after flipping.
Eolv in the title when the big guy said: For a similar to give you a 01 string, and then let you about the 01 phase of the request for something, and then do not read the question should think of all the even digits (or odd digits) of the 01 number of flip, so that, then go to ask about 01 to meet the Thing, Is that it can be transformed into the same range.
Think carefully about this, remember a question: the chessboard production, but also through the above method can transform the problem into a better solution to the problem.
The idea of this problem: in the input process, all the numbers on the even digits are flipped, in the conversion sequence to operate.
? First find out how many consecutive same cell in this sequence, only need to count the length of these cells, and then to enumerate each interval.
? Like this: 110001100011 when enumerating to the middle 1 of the small interval, we will flip the whole cell, then we can get the answer is t[now-1]+t[now]+t[now+1], that is, reverse the middle interval, The interval before and after the interval and the current interval after inversion are combined to form a new large interval, and then through the preprocessing interval length, O (1) can be used to query the current answer and update.
? In this way, the overall complexity is O (n).
Then follow this idea to achieve a time, suddenly found in the examination room feel very difficult problem, immediately become water.
In the examination room or should try to sink the heart to read the question, sank the heart to think.
Code
#include<iostream>#include<cstdio>using namespace std;inline int read(){ int sum=0,f=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘){ if(ch==‘-‘)f=-1; ch=getchar(); } while(ch>=‘0‘&&ch<=‘9‘){ sum=(sum<<1)+(sum<<3)+ch-‘0‘; ch=getchar(); } return sum*f;}int n,ans,tot,flag;int a[999999],t[999999];int main(){ n=read(); for(int i=1;i<=n;i++){ if(i&1)a[i]=read(); else { (a[i]=read())^=1; } if(a[i]!=a[i-1])flag=1; } if(!flag)ans=n; for(int i=1;i<=n;i++){ if(a[i]==a[i-1])t[tot]++; else t[++tot]=1; } for(int i=1;i<=tot;i++){ ans=max(ans,t[i-1]+t[i]+t[i+1]); } printf("%d\n",ans); return 0;}
T2: Paste (copy)
? Description
? Once there was a ${1, 2, \dots, n}$ arrangement, you want to use cut/paste operations, this arrangement into $1,2, \dots, n$.
? A cut/paste operation refers to the whole range of successive sub-intervals in a sequence that are shifted forward or backward in length. Because you think pressing ctrl+x at the same time is very tiring, you want to know the minimum number of operations.
? Input
? The input file contains multiple sets of data.
? Each set of data has two rows, where the first row contains a positive integer n, and the second row gives a set of ${1, 2, ..., n}$.
? Ends with a single-ended input.
? Output
? For each set of data, the output is an integer that represents the minimum number of operations.
The second question, or to first%%%zager big guy, search Wang deserved (fog), the whole court he a person a dropped this problem, (as if he only a person scored?) Forget about it. And he still used the very cowhide of the ida*, he said at the time of lectures, he told the problem, and really in the PPT at that time found the problem, may be because it was not serious, alas, regret ...
The test instructions of the problem is this: gives you a sequence of unordered sequences, consisting of 1 to n numbers that are not duplicated. The minimum number of steps is now required by panning a small interval to turn this sequence into a strictly ascending, that is, the 1,2,3......N so arranged.
Data range is very friendly, eolv conscience Ah, originally thought can open mind disorderly, can be because this problem to oneself whole is decadent, the whole exam almost collapsed.
First of all, Ida, Zager, when he was giving us a title, mentioned his understanding of Ida.
? Zager:ida*= Iterative deepening + valuation function h ();
The personal feeling is very incisive, after all, the search vegetable chicken does not understand anything.
1, iterative deepening: is each given a depth, as the upper bound of the number of search steps, the depth from small to large, so if the answer is the number of operations, iterative deepening can ensure that the first search for the answer is the smallest, because the equivalent of small to large enumeration answer.
? Why do you do this?
? Because for a part of the search problem, if you start to search the wrong state, is very deadly, that is, if the wizard in the process of searching for a wrong state and very deep, then basically GG. So in this case, we use a similar wide-search idea to think. is in the process of searching, the equivalent of We are in the root of a search tree from the continuous outward expansion of the new state, one layer, more and more. So in order to prevent us from drilling into a subtrees tree can not come to the situation, may be a depth, once the current depth of the search to this depth and has not been searched, then we choose to abandon the subtrees tree, to another subtree is to search other states. This way, even if the first few layers of the state we will be repeated search, but also can effectively solve the problem of the search.
2, Valuation function: Personal feeling this thing is too cowhide, but zager the big guy teaches me when said this thing is actually we usually do the idea of a problem, just call it the valuation function I was confused forced.
? Think about it.
? The valuation function plays a role in pruning.
? In relation to iterative deepening, we can know that the total number of steps searched is given.
? Then imagine that when we search for a state, the number of steps already used is known, the number of steps that can be used is also known (that is, the maximum depth of the iteration), and more importantly, we know what the target state is, what the current state is. Then you can estimate the cost from the current state to the target State is the number of steps to use, if the current step plus the estimated number of expected steps is greater than the total number of steps, then this situation is definitely not good or can not find the answer, directly return on the line, equivalent to a pruning.
? Why is it an estimate?
? The important point is that the valuation function is our analysis of the topic, that is, in the optimal decision, from the current state to reach the target state of the desired step, so not accurate, but must be guaranteed in the optimal state. Because if the optimal state is not from the current state to the target State, we can not hesitate to do pruning.
? Analysis of this problem, because the data is very small, so do not worry about complexity (with ida* do not worry about the only thing that will make us time out of the situation is to search the end). After analysis, we can conclude that as long as in (N+1)/2 steps, and because of n<=9, so the answer is up to 5. will be any one sequence of the wrong sequence into the target sequence, Elov said can be divided to cure proof, but he did not testify,,, just to cite a worst case example
? The initial sequence given is: 5 4 3 2 1
? You can change this:
3 2 5 4 1 *
? 3 4 1 2 5
? 1 2 3 4 5 It means that in the worst case of n=5 the problem can be solved in 3 steps.
Then we can optimize, the iterative deepening can be to the maximum depth of 5, here is another magical place, is that we can set the maximum depth of 4, this time if you have not found the answer, then do not search down also can know the answer is 5, (analysis of the problem is really important ...) , shivering).
After each transformation, you can enumerate a small interval (left and right endpoints), and then enumerate where the interval is inserted. (really is with ida* casually how disorderly do not fear ... )
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;inline int read () {int sum= 0,f=1; Char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') f=-1; Ch=getchar (); } while (ch>= ' 0 ' &&ch<= ' 9 ') {sum= (sum<<1) + (sum<<3) +ch-' 0 '; Ch=getchar (); } return sum*f;} const INT Wx=199;int n,m;int A[wx],t[wx];bool goal () {for (int i=1;i<n;i++) {if (a[i]!=a[i+1]-1) return false; } return true; int h () {int tot=0; for (int i=1;i<n;i++) {if (a[i+1]!=a[i]+1) tot++; } if (a[n]!=n) tot++; return tot;} BOOL Dfs (int d,int maxd) {if (H () +d*3>maxd*3) return false;//valuation function Big Pruning is important: changing a number will eliminate the disharmony value by up to 3. if (goal ()) return true;//determines whether to reach the target State int b[wx],olda[wx]; memcpy (olda,a,sizeof a); for (int i=1;i<=n;i++) {for (int j=i;j<=n;j++) {//i,j) is an enumeration of intervals around the endpoint int cnt=0; for (int k=1;k<=n;k++) {if (k<i| | K>J) b[++cnt]=a[k];//will not be in the number of i,j in transit array B save it} for (int k=1;k<=cnt;k++) {///enum i,j in which position, violence change a array can be int cnt 2=0; for (int l=1;l<=k;l++) a[++cnt2]=b[l];//k is an enumeration i,j this interval is inserted after the number of K. for (int l=i;l<=j;l++) a[++cnt2]=olda[l]; for (int l=k+1;l<=cnt;l++) a[++cnt2]=b[l]; if (Dfs (D+1,MAXD)) return true; memcpy (a,olda,sizeof Olda);//Recover A array}}} return false;} int Slove () {if (goal ()) return 0; int max_ans=5; for (int i=1;i<max_ans;i++) {if (Dfs (0,i)) return i;//iteration count} return max_ans;//analyzing the problem is really important ... }int Main () {freopen ("copy.in", "R", stdin); Freopen ("Copy.out", "w", stdout); while (1) {n=read (); if (!n) break; for (int i=1;i<=n;i++) {a[i]=read (); } int ans=slove (); printf ("%d\n", ans); } return 0;}
For the second question [UVa 11212 Editor's manuscript, consult the idea of a big guy, express thanks and hang up the link.
? 51785439
10-2 National Day Fourth field simulation problem