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 1≤i<j≤N 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 xk≤xj−xi; 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 k−xj<xj−xi; 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 2xi−−xjk≤2xj−−xj; 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;
}