"BZOJ4240" Interesting family garden tree Array + greedy

Source: Internet
Author: User

"BZOJ4240" Interesting family garden description Joi, who is interested in the family garden, grows a plant called ioi grass every year in his own field. Joi's field is divided into N regions along the east and west, and is labeled as 1~n from Western to east. There are n strains of ioi grass, each planted in a single area. The ioi grass, planted in the area I, will grow to hi in the spring, and then no longer grow. In order to observe the appearance of spring and travel Joi June noticed that the configuration of ioi grass is not the same as scheduled. Ioi grass is a very dependent on the sun's plants, if a region of Ioi grass east and west have higher than its ioi grass exists, then this plant ioi grass will wither in the summer. In other words, in order not to wither any ioi grass, the following conditions need to be met: for any 2<=i<=n-1, the following two conditions satisfy at least one: 1.for any 1<=j<=i-1,hj<=hi2.for any I+1<=j<=n,hk<=hiIoi grass is very expensive, in order not to let ioi grass wither, joi need to change the order of ioi grass. Ioi grass is very very tall and slender plant, so joi can only exchange two adjacent strains of ioi grass at a time. That is, joi every time you need to select an integer i (1<=i<=n-1), and then exchange the first strains of ioi grass and i+1 strains ioi grass. As summer approaches, the likelihood of ioi wilt is growing, so Joi wants to know the minimum number of operations that will make all ioi grass wither. Now given the number of fields and the height of each plant ioi, please ask for the minimum number of operations that will not wither the ioi grass. Input first line a positive integer n, representing the field is divided into N regions. Next N rows, line I (1<=i<=n) An integer hi, indicating the height of the plant in the spring when the output is an integer, indicating the minimum number of operations required by the sample Input6
2
8
4
5
3
6Sample Output3The final height sequence of the hint is 2 4 5 8 6 3, which requires operation three times. 3<=n<=3*10^51<=hi<=10^9

A frequently used conclusion: we give the original array index table 1...N, after moving a number of times we will get a new subscript sequence, the number of times required is the reverse logarithm of this new sequence.

So we thought about how to make the new sequence less logarithmic. The final sequence is required to meet the H value of the first increment and then decrement, so the largest must be placed in the middle, the second largest can be placed on the largest two sides, again placed on both sides of the front ... So, when we put the K-big, we greedy choice can form a few reverse pairs of the other side of the put, and this does not affect the back node.

And if multiple grass heights are the same, you have to give a special sentence.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;const int maxn=300010;typedef Long long ll;ll ans;int n,l,r;int s[maxn],v[maxn],p[maxn];void updata (int x,int val) {fo R (int i=x;i<=n;i+=i&-i) S[i]+=val;} int query (int x) {int i,ret=0;for (i=x;i;i-=i&-i) ret+=s[i];return ret;} BOOL CMP (int a,int b) {return v[a]>v[b];} Int Rd () {int Ret=0,f=1;char gc=getchar (); while (gc< ' 0 ' | | Gc> ' 9 ') {if (gc== '-') F=-f;gc=getchar ();} while (gc>= ' 0 ' &&gc<= ' 9 ') ret=ret*10+gc-' 0 ', Gc=getchar (); return ret*f;} int main () {scanf ("%d", &n), int i,j,a;for (i=1;i<=n;i++) v[i]=rd (), P[i]=i;sort (P+1,P+N+1,CMP); for (i=1;i<=n ;) {for (j=i;j<=n;j++) {a=query (p[j]), Ans+=min (a,i-1-a), if (V[p[j+1]]!=v[p[j]]) break;} for (; i<=j;i++) Updata (p[i],1);} printf ("%lld", ans); return 0;}

"BZOJ4240" Interesting family garden tree Array + greedy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.