[***] HZOJ histogram

Immortal title.

Author of Positive Solutions:

Algorithms II: for 60% of data: consider the location of the direct enumeration of the roof , the relationship between the total cost and the roof height is a unimodal function ,, we can use the rule of thirds-third of the roof height. Time complexity of O (n 2 * logN). 

 

Algorithms three: for 100% of the data:   We enumerate the roof position and then one-third the height of the practice, the bottleneck lies in the complexity of the calculation of the cost. Suppose roof i, the height H i , if J <i, there H J -j = H i -i; if J>i, there H J + J = H i + i.

Sort according to weights to build Fenwick tree to solve the weight and the relationship between the weight of i, maintaining two Fenwick tree. Time complexity of O (n (logn) (logv ), V is the height of the roof .

 

For about 60% of the data, there are two practices:

1. As the author solution to a problem, that's quite clear.

2. Consider for a determined position as a roof, then the roof height is determined to prove later. The height of each spot plus the distance from the roof to the height of the median after sequence, so that the annealing can be simulated.

To 100% of the data:

1. The simulated annealing (people called).

Enumerate the roof, one-third height, and then the main optimization is computationally spending. You can maintain two Fenwick tree.

Because time is tight before the rest of the goo out ......

 

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cmath>
 5 #define int LL
 6 #define LL long long
 7 #define MAXN 100010
 8 #define reg register
 9 #define con const
10 using namespace std;
11 struct node
12 {
13     int val,id;
14     friend bool operator < (node a,node b)
15     {return !(a.val^b.val)?a.id<b.id:a.val<b.val;}
16 }al[MAXN],ar[MAXN];
17 int n,a[MAXN],maxh,rkl[MAXN],rkr[MAXN];
18 #define lowbit(x) ((x)&(-(x)))
19 struct bit
20 {
21     int C[MAXN],num[MAXN];
22     void add(reg int x,reg con int y,reg con int z)
23     {while(x<=n){C[x]+=y;num[x]+=z;x+=lowbit(x);}}
24     pair<int,int> ask(reg int x)
25     {
26         int sum=0,nnum=0;
27         while(x)
28         {
29             sum+=C[x];
30             nnum+=num[x];
31             x-=lowbit(x);
32         }
33         return make_pair(sum,nnum);
34     }
35 }le,re;
36 __attribute((always_inline))int get(reg con int x,reg con int maxh)
37 {
38     int ans=0;
39     ans+=abs(maxh-a[x]);
40     int pos=upper_bound(al+1,al+n+1,(node){maxh-x,x})-al-1;
41     pair<int,int> tem=le.ask(pos);
42     ans+=tem.second*(maxh-x)-tem.first;
43     pair<int,int> tem2=le.ask(n);
44     ans+=tem2.first-tem.first-(tem2.second-tem.second)*(maxh-x);
45     pos=upper_bound(ar+1,ar+n+1,(node){maxh+x,x})-ar-1;
46     tem=re.ask(pos);
47     ans+=tem.second*(maxh+x)-tem.first;
48     tem2=re.ask(n);
49     ans+=tem2.first-tem.first-(tem2.second-tem.second)*(maxh+x);
50     return ans;
51 }
52 __attribute((always_inline))void read(int &s)
53 {
54     s=0;
55     reg int f=1;reg char a=getchar();
56     while(a<'0'||a>'9'){if(a=='-')f=-1;a=getchar();}
57     while(a>='0'&&a<='9'){s=s*10+a-'0';a=getchar();}
58     s*=f;
59 }
60 signed main()
61 {
62     read(n);
63     int ans=0x7ffffffffffff;
64     for(reg int i=1;i<=n;++i)
65     {
66         read(a[i]);
67         al[i].val=a[i]-i,al[i].id=i;
68         ar[i].val=a[i]+i,ar[i].id=i;
69     }
70     sort(al+1,al+n+1);
71     sort(ar+1,ar+n+1);
72     for(reg int i=1;i<=n;++i)rkl[al[i].id]=i,rkr[ar[i].id]=i;
73     for(reg int i=1;i<=n;++i)re.add(rkr[i],a[i]+i,1);
74     for(reg int i=1;i<=n;++i)
75     {
76         re.add(rkr[i],-a[i]-i,-1);
77         int l=max(i,n-i+1),r=1e9,ml,mr;
78         ans=min(ans,get(i,l));
79         while(r>l+1)
80         {
81             int mid=(l+r)>>1;
82             ml=mid-1,mr=mid;
83             int ans1=get(i,ml),
84                 ans2=get(i,mr);
85             if(ans1<ans2)r=mid;
86             else l=mid;
87             ans=min(ans,ans1);
88             ans=min(ans,ans2);
89             if(!(ml^l)&&!(mr^r))break;
90         }
91         le.add(rkl[i],a[i]-i,1);
92     }
93     printf("%lld\n",ans);
94 }
View Code

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/Al-Ca/p/11318851.html