[Title]
digital array composed of a crater, a value of Seco
12,453
rules, flames pass:
two mountains will be able to be seen between the neighboring flames,
high mountains between non-adjacent side of the mountains <= the two non-adjacent mountain high mountain, you can see the flames of these two mountains each other.
For example, 1 and 4 can not be seen, because the clockwise side 2 is larger than 1, blocking the counterclockwise edge, 3 and 5, than a large, blocking.
3 and 4 can be seen, while blocking the counterclockwise side 5, 1 and 2 but not clockwise side block 3.
Q. Which two mountains could see the flames from each other; required time complexity is O (1)
answer to this question is (1, 2) (1, 3) (2, 4) (4, 5) (5, 3) (2 3 (4, 3))
[explanations]
[digital] array different from each other
any mountain between the highest mountain N, M and sub-alpine in, clockwise, counterclockwise looking for,
there you can see the mountain N, M two mountains, namely (n - 2) * 2 pair, and then coupled with N, M can be seen that each two hill
so a total of (n - 2) * 2 + 1 = 2 * n - 3 for
Conclusion: n is the number of non-identical, a total of (2 * n - 3) of
Array [] containing the same number
monotone stack: descending order
, in one direction of rotation from a maximum value of any beginning with the incoming operation
when a number is popped off the stack, he found a mountain can be seen that each two that, and he will push the following
when the stack number and stack are equal, the two numbers together information recorded in the same stack location,
when the eject a number of records as of n, that is, at the same a record position of a number n times, for example, there are n consecutive stack 4, the same position in the n-th recording 4
at this time it pops up, as can be seen Hill: Cn2 + n * 2 times wherein Cn2 [ combined with each other between the n 4] + n * 2 [n either side of a mountain 4 with his combination],
: when the array after been traversed, the number of remaining stack pop
for penultimate i> 2 remaining number of pop he could see the mountain: Cn2 + n * 2
for the penultimate time remaining pop-up,
if the last number is> a record he could see the mountain: Cn2 + n * 2
If the last digit == is a record he could see the mountain: Cn2 + n * 1, because it is a ring.
For when the penultimate one remaining pop-up, he could see the mountain: Cn2
[Code]
. 1 #pragma Once 2 #include <the iostream> . 3 #include <Vector> . 4 #include <the deque> . 5 . 6 . 7 the using namespace STD; . 8 . 9 int fireTranfer (Vector < int > m) 10 { . 11 // first find the maximum value of 12 int RES = 0 ; 13 is int maxIndex = 0 ; 14 the deque <pair < int , int >> D; // monotone stack 15 for (int i = 0; i < m.size(); ++i)//寻找最大值 16 maxIndex = m[maxIndex] > m[i] ? maxIndex : i; 17 d.push_back(make_pair(m[maxIndex], 1)); 18 for (int i = maxIndex + 1; i != maxIndex; ++i) 19 { 20 if (i == m.size()) 21 { 22 i = -1; 23 continue; 24 } 25 26 while (!d.empty() && m[i] >. d.back () First) 27 { 28 int value, NUM; 29 value = d.back () First;. 30 NUM = d.back () SECOND;. 31 is RES = + (NUM * (NUM - . 1 ) / 2 ) + NUM * 2 ; // calculated composition data for the pop-up 32 d.pop_back (); 33 is } 34 is IF (m [I] == d.back (). First) 35 ++ d.back () .second; 36 the else 37 [ d.push_back (the make_pair (m [I], . 1 ));// press-fitting the new number 38 is 39 40 } 41 // the data remaining in the queue pop 42 is the while (! D.empty ()) 43 is { 44 is int value, NUM; 45 value = d.back () First.; 46 is NUM = . d.back () SECOND; 47 IF (d.size ()> 2 ) // not the last two data 48 RES + = (* NUM (NUM - . 1 ) / 2 ) + NUM * 2 ; 49 the else IF (d.size () == 2 ) //Penultimate number 50 IF (d.front () SECOND>. . 1 ) 51 is RES = + (NUM * (NUM - . 1 ) / 2 ) + NUM * 2 ; 52 is the else 53 is RES = + (NUM * (NUM - . 1 ) / 2 ) + NUM; 54 is the else // is the reciprocal of the first 55 RES + = (* NUM (NUM - . 1 ) / 2 ); 56 is d.pop_back (); 57 is } 58 return RES; 59 } 60 61 void Test() 62 { 63 vector<int>v; 64 v = { 1,2,4,5,3 }; 65 cout << fireTranfer(v) << endl; 66 v = { 2,5,3,4,3,4,5 }; 67 cout << fireTranfer(v) << endl; 68 v = { 5,4,4,4 }; 69 cout << fireTranfer(v) << endl; 70 71 }
Reproduced in: https: //www.cnblogs.com/zzw1024/p/11066599.html