Codeforces Round #851 (Div. 2) D. Moving Dots

Codeforces Round #851 (Div. 2) D. Moving Dots

Let’s think about the original problem where we do not think about subsets.

We can easily observe that each dot does not change direction during moving.

Assume that dots gather at coordinate x x x. Rightmost dot of dots that have smaller coordinate than x x x should move right, and leftmost dot which has bigger coordinate than x x x should move left. We can observe that the number of adjacent dot where each move toward each other will be the answer.

Now let’s solve the problem for subsets. Instead of counting number of adjacent dot that moves toward each other for each subset of dots, we will count the number of subset for each possible 1 ≤ i < j ≤ N 1≤i<j≤N 1i<jN where dot i i i moves right and dot j j j moves left and there are no dots between i i i and j j j.

Let the coordinate of a dot in a subset be k k k. We will try to find out which k k k can be in a subset where dot i i i and dot j j j move toward each other.

Since there are no dot between i i i and j j j, dots satistying x i < k < x j x_i<k<x_j xi<k<xj; should not be in the subset.Since dot i i i should move right, dotsthat satisfy k < x i k < x_i k<xi and x k ≤ x j − x i x_k ≤x_j-x_i xkxjxi; should not be in the subset.As the same way for dot j j j, dots that satisfy k > x j k > x_j k>xj; and k − x j < x j − x i k - x_j < x_j-x_i kxj<xjxi; should not be in the subset. Summing these up, dots satisfying 2 x i − − x j k ≤ 2 x j − − x j 2x_i - - x_j k ≤ 2x_j - - x_j 2xixjk2xjxj; should not bein the subset.By using binary search, we can find the number of dots that cannot be in the subset in O ( l o g N ) O(logN) O(logN).

lf there are p dots that can be in the subset, the number of subset where i i i and j j j moves toward each other will be 2 P 2^P 2P .summing all 2 P 2^P 2P will give us the answer. Since there are O ( N 2 ) O(N^2) O(N2) pairs of ( i , j ) (i, j) (i,j), we can solve the problem in O ( N 2 l o g N ) O(N^2logN) O(N2logN).

There were some dynamic programming solutions from some testers.

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int MOD = 1000000000 + 7;

int main()
{
	int n;cin>>n;
	vector<int> a(n+1);
    for(int i=1;i<=n;i++) cin>>a[i];
    vector<LL> fac(n+1);
    fac[0]=1;
    for(int i=1;i<=n;i++)
        fac[i]=fac[i-1]*2%MOD;
    LL ans=0;
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
        {
            int d=a[j]-a[i];
            int pos1=lower_bound(a.begin()+1,a.begin()+i,a[i]-d)-a.begin()-1;
            int pos2=a.begin()+n-lower_bound(a.begin()+j,a.end(),a[j]+d)+1;
            ans+=fac[pos1]*fac[pos2];
            ans%=MOD;
        }
    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wa_Automata

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值