Bzoj 3613 Heoi2014 South Garden Ground pile Light flocculation Two-point answer/LINEAR procedure

Source: Internet
Author: User

The main idea: given a sequence A, a monotone non-decreasing sequence B, so that max{|ai-bi|} Minimum


The problem of teasing .....


Two-point answer procedure:

Scan from right to left for each verification

If the current number is less than or equal to the right number, adjust the number upward to the limit (the number reaching the right or the adjusted value reaches the upper bound)

If the current number is greater than the number on the right, adjust the number downward to equal to the number on the right. Cannot do so returns false

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 5005005using namespace Std;int n,a[m];long long sa,sb,sc,sd,mod;int F (int x) {long long re=sd,temp=x;re+=sc*temp%mod; Temp*=x)%=mod;re+=sb*temp%mod; (temp*=x)%=mod;re+=sa*temp%mod;return int (re%mod);} BOOL Judge (int x) {int i,min_num=2147483647;for (i=n;i;i--) {if (a[i]<=min_num) min_num=min (min_num,a[i]+x); else if ( A[I]-MIN_NUM>X) return false;} return true;} int bisection () {int L=0,r=mod-1;while (l+1<r) {int mid=l+r>>1;if (Judge (mid)) R=mid;elsel=mid;} Return Judge (L)? L:r;} int main () {int i;cin>>n>>sa>>sb>>sc>>sd>>a[1]>>mod;for (i=2;i<=n;i++ ) a[i]= (F (a[i-1]) +f (a[i-2])%mod;cout<<bisection () <<endl;return 0;}

But 500W obviously nlogn the pressure of Alexander (although I had only 1.5 seconds to go to the biggest point of my native slow death)

So I went to see the linear approach to the calibration process.

The moment I opened the CPP, I was shocked--


The answer is equal to the difference between the maximum difference of the inverse of the value +1>>1

Correctness obviously ... Obviously..... Obviously.........


#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 5005005using namespace Std;int n,ans,a[m];long long sa,sb,sc,sd,mod;int F (int x) {Long Long re=sd,temp=x;re+=sc*temp%mod ;(temp*=x)%=mod;re+=sb*temp%mod; (temp*=x)%=mod;re+=sa*temp%mod;return int (re%mod);} int main () {int i;cin>>n>>sa>>sb>>sc>>sd>>a[1]>>mod;for (i=2;i<=n;i++ ) a[i]= (F (a[i-1]) +f (a[i-2])%mod;int max_val=0;for (i=1;i<=n;i++) {Max_val=max (max_val,a[i]); Ans=max (ans,max_ VAL-A[I]+1>>1);} Cout<<ans<<endl;return 0;}


Bzoj 3613 Heoi2014 South Garden Ground pile Light flocculation Two-point answer/LINEAR procedure

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.