三分模板

int三分

int calc(int M)
{
    
}
int solve(int L, int R)
{
    int M, RM;
    while (L + 1 < R)
    {
        M = (L + R) / 2;
        RM = (M + R) / 2;
        if (calc(M) < calc(RM)) //計算最小值,若爲最大值則>
            R = RM;
        else
            L = M;
    }
    return L;
}

double的三分模板

注意一點,double的循環條件中最好使用以下的表示(L + EPS < R),如果使用dcmp(R - L)> 0判斷也可以,但是使用dcmp函數時候一定不能寫等號,因爲如果兩個double數在EPS範圍內相等的還繼續的話會導致死循環

const double EPS = 1e-10;  
  
double calc(double n)  
{  
    return;  
}  
  
double solve(double L, double R)  
{  
    double M, RM;  
    while (L + EPS < R)  
    {  
        M = (L + R) / 2;  
        RM = (M + R) / 2;  
        if (calc(M) < calc(RM)) //計算最小值  
            R = RM;  
        else  
            L = M;  
    }  
    return L;  
}  

還有一種是限定次數的三分

const double EPS = 1e-10;

double calc(double n)
{
    return;
}

double solve(double L, double R)
{
    double M, RM;
    for (int i = 1; i <= 300; i++)
    {
        M = (L + R) / 2;
        RM = (M + R) / 2;
        if (calc(M) < calc(RM)) R = RM;
        else L = M;
    }
    return L;
}


一個例題

cf244E. Police Patrol

using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int maxn = 100010;
const int MOD = 1000000007;

int n, m;
int a[1000100];
LL calc(int idx)
{
    LL ans = 0;
    int x = -1, y = -1;
    for (int i = 0; i < idx; i += m)
    {
        ans += (LL)abs(a[i] - a[idx]) * 2;
        x = i;
    }
    for (int i = n - 1; i > idx; i-= m)
    {
        ans += (LL)abs(a[i] - a[idx]) * 2;
        y = i;
    }
    return ans;
}

int solve(int L, int R)
{
    int M, RM;
    while (L + 1 < R)
    {
        M = (L + R) / 2;
        RM = (M + R) / 2;
        if (calc(M) < calc(RM)) //計算最小值
            R = RM;
        else
            L = M;
    }
    return L;
}

int main()
{
    RII(n, m);
    REP(i, n)
    {
        RI(a[i]);
    }
    int ansid = solve(0, n - 1);

    cout << calc(ansid) << endl;
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章