AGC037 C Numbers on a Circle【思维】

Topic Portal

The meaning of problems

This question is a Gangster adapted to bring out the exam questions is long like this:

 

 

 

 

 Well, in fact, this is the real meaning of the questions:

Given the initial and final sequence of the sequence, each time selecting a number into its own and the number 2 and adjacent. I asked whether the initial sequence can become final sequence, if you can, ask how many times the minimum required.

analysis

This question is found that there are a variety of actions, even if the violence is to write search is not very good writing.

Being the anti difficult, considering the state from the beginning to the end of the state, that is, minus the number has been left and right.

If the number is greater than the middle and both sides of the number of, the middle of the certain number to be operated (set $ a, b, c $ are $ i-1, i, i + $ B $ 1, $ value)
and in $ b> a + c $ when the condition is not destroyed, if the operation does $ b $, then $ A, c $ is not operated (after the operation is negative, not legal)
long $ b> a + c $ and $ Bi> Ai $ $ b $ should have been subtracted $ (a + c) $
because it is sure to reduce the minimum so it is necessary

$ b> a + c $ condition is in if (...) - 1 that place can also be written judgments if (! step) is to determine the number of operations of at least $ 1 $ times
if the number of times you can take the whole operation under operation must be done even if you can not operate (operation to become negative) it can not be solvable only one have to do
after finished if satisfied that $ Ai == Bi $ best friends on ok
but if not based on step further reduction algorithm is once turned negative this time $ b <a + c $ a
is not certainly we need to continue operating so stuffed inside a queue waiting one day to continue

with priority queues it, but also because "in the $ b> a + c $ when the conditions are not destroyed, if not $ b $ operation, then $ a, c $ also will not be operating (after the operation is negative) "
If you do not first So there is no way back operation that the greatest number of operations.

 

Basically that's it.

But also pay attention to open $ long $ $ long $ (specifically count the number of times it seems not very good, this situation is not good estimates are opened or safer)

Code

 1 #include<iostream>
 2 #include<string>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<queue>
 6 #include<algorithm>
 7 #include<vector>
 8 using namespace std;
 9 #define N 200005
10 #define ll long long 
11 #define fst first
12 #define snd second
13 //环:1:2,n n:n-1,1 
14 int n,a[N],b[N];
15 ll ans;//记得开ll 
16 inline int rd()
17 {
18     int f=1,x=0;char c=getchar();
19     while(c<'0'||'9'<c){if(c=='-')f=-f;c=getchar();}
20     while('0'<=c&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
21     return F * X;
 22 is  }
 23 is The priority_queue <pair < int , int >> Q;
 24  // pair defines the priority queue second press then automatically sorted first 
25  int main ()
 26 is  {
 27      // The freopen ( "hopeless.in "," R & lt ", stdin);
 28      // The freopen (" hopeless.out "," W ", stdout); 
29      n-= RD ();
 30      for ( int I = . 1 ; I <= n-; I ++ )
 31 is          A [I] = RD ();
 32      for ( int I = . 1;i<=n;i++)
33     {
34         b[i]=rd();
35         if(a[i]!=b[i]) Q.push(make_pair(b[i],i));
36     }
37     while(!Q.empty())
38     {
39         pair<int,int> now=Q.top();Q.pop();
40         int i=now.snd;
41         /*int pre=i-1,suf=i+1;
42         if(pre==0) pre=n;
43         if(suf==n+1) suf=1;
44         int step=(b[i]-a[i])/(b[pre]+b[suf]);
45         */
46         int pre=(i+n-2)%n+1,suf=i%n+1;
47         int step=(b[i]-a[i])/(b[pre]+b[suf]);
48         if(b[i]-b[pre]-b[suf]<a[i])
49         {
50             puts("-1");
51             return 0;
52         }
53         ans+=step;
54         b[i]-=step*(b[pre]+b[suf]);
55         if(a[i]!=b[i]) Q.push(make_pair(b[i],i));
56     }
57     printf("%lld\n",ans);
58     return 0;
59 }
Code

 

 

 

Guess you like

Origin www.cnblogs.com/lyttt/p/11774949.html