-
Topic Links:
Https://arc100.contest.atcoder.jp/tasks/arc100_a
-
Analysis:
In the game to do this problem to think of a blind way is to move the average up and down to take the minimum value, and then the big guy YJW Seniors said this is a strict monotone single-peak function of the direct three-way it's better, though I've heard it before, it's the first time.
-
The three-way
has a maximum function f (x) that defines the field as \ ([l,r]\) , we find two points within the defined field \ ( Lmid,rmid (Lmid<rmid) \)
-
If \ (f (lmid) <f (rmid) \) , or span class= "Math inline" >\ (lmid\) and \ (rmid\) are all on the left of a single peak, or \ (lmid\) on the left, \ (rmid\) on the right, but no matter what \ (lmid\) is on the left of a single peak, so \ (l=lmid\)
-
If \ (f (lmid) <f (rmid) \) , the analysis is similar, \ (r=rmid\)
-
If \ (f (lmid) ==f (rmid) \) emmm This actually I do not know how to deal with, casually according to the above one situation but always feel not very stable
-
Code:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype > #include <map> #include <queue> #include <algorithm> #define RI Register int #define LL long longusing namespace Std;const int maxn=200005;const int inf=0x7fffffff;template <class t>inline void Read (T &x) {X=0;int Ne=0;char C; while (!isdigit (C=getchar ())) ne=c== '-'; x=c-48; while (IsDigit (C=getchar ())) x= (x<<3) + (x<<1) +c-48; x=ne?-x:x; return;} int N;ll a[maxn];ll sum=0;inline ll solve (int k) {ll cnt=0; For (RI i=1;i<=n;i++) cnt+=abs (a[i]-k); return CNT;} int main () {read (n); For (Ri i=1;i<=n;i++) {read (a[i]); A[i]=a[i]-i; } ll Ans,lmid,rmid; ll L=-1e9,r=1e9; while (l<r-1) {lmid= (l+r) >>1,rmid= (lmid+r) >>1; if (Solve (LMID) >solve (rmid)) L=lmid; else R=rmid; } if (Solve (L) <solve (R)) Ans=l; else ans=r; printf ("%lld\n", Solve (ans)); return 0;}
ARC C-linear Approximation---Three-part method