9 degree question 1377: slow change sequence, 9 degrees 1377
Reprinted please indicate this article link http://blog.csdn.net/yangnanhai93/article/details/40474355
Question link address: http://ac.jobdu.com/problem.php? Pid = 1, 1377
The difficulty of this question lies in how to analyze the characteristics of the slow variable sequence:
1: The sequence must be consecutive after the slow-changing sequence is sorted.
Proof: assume that the sorted sequence is a [1] a [2] a [3]... a [n], where a [n]-a [n-1]> 1, that is, an is not consecutive with the preceding number, because the Slow Variable Sequence requires that the changes before and after any number are 1, but for a [n], no number can satisfy the nature of the Slow Variable Sequence before and after it, therefore, the sorted array must be continuous.
2: The Slow Variable Sequence must meet the requirement of forming A new array as array A, which has the following properties:
A [1] = a1, A [n] = an-A [n-1]. when A [n] of 1-n must meet A [n] = 0 and 1-(n-1: A [n]> 0 (array A indicates the number of remaining positions to be filled)
Proof: When 1-(n-1) is counted as m, if A [m] <= 0, then we can obtain A [m] = 0, just get A slow-changing sequence. If A [m] <0, it means that the number of m is less than the number of m-bit, it certainly cannot meet the requirements.
For n bits, as described above, A [n] = 0 makes the number to be filled by the slow variable sequence exactly meet.
The following figure is used to help you understand the problem. By drawing a line, you obviously know that the vertices of the previous layer must be greater than the vertices of the next layer (except for the last layer ), must also meet A [n] = 0
#include <iostream>#include <memory.h>using namespace std;int main(){ //freopen("data.in","r",stdin); int num,A[10001],tmp,low,high; while(cin>>num) { memset(A,0,sizeof(A)); for(int i=0;i<num;i++) { cin>>tmp; A[tmp]++; } low=0; while(A[low]==0) low++; tmp=0; high=low; while(A[high]!=0) { tmp+=A[high]; high++; } if(num==tmp) { bool isTrue=true; tmp=0; for(int i=low;i