CF1788D Moving Dots 题解

· · 题解

对于每种选择,拆成不同点集考虑贡献。

考虑一组点,如果它们最终聚集在一个点上,一定有中间连续(该集合中)两个点 x,y,满足如下条件:

直接枚举 x,y 并二分求解第二点要求。求出的那些点可选可不选,2^m 即可。

时间复杂度 O(n^2\log n )。

实现细节参见代码注释。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1000000007;
int n , x[3010] , ans;
int fast_mi(int x , int y)
{
    int ans = 1 , qwq = x;
    while(y)
    {
        if(y & 1) ans *= qwq , ans %= mod;
        y >>= 1 , qwq *= qwq , qwq %= mod;
    }
    return ans;
}
signed main()
{
    scanf("%lld" , &n);
    for(int i = 1 ; i <= n ; i ++) scanf("%lld" , &x[i]);
    for(int i = 1 ; i <= n ; i ++)
    {
        for(int j = i + 1 ; j <= n ; j ++)
        {
//          int d = x[j] - x[i];
            int id1 = lower_bound(x + 1 , x + i + 1 , 2 * x[i] - x[j]) - x;
            int id2 = lower_bound(x + j , x + n + 1 , 2 * x[j] - x[i]) - x;

            // 1->id-1
            // x[i]-x[k] > x[j]-x[i]
            // x[k] < 2*x[i]-x[j]

            // x[k]-x[j] >= x[j]-x[i]
            // x[k] >= 2*x[j]-x[i]
            // id2->n

            int m = id1 + n - id2;
//          ans += (1ll << m);
            ans += fast_mi(2 , m) , ans %= mod;
        }
    }
    printf("%lld" , ans);
    return 0;
}