The three-c-linear method of solving the---ARC approximation

Source: Internet
Author: User

    • 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

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.