D. Moving Dots

Problem - 1788D - Codeforces

D. Moving Dots_第1张图片

思路:对于某个碰撞的位置来说,一定是因为两个的碰撞引起的,而我们可以枚举每一对,i,j然后假如说她两个产生了一个碰撞的位置,那么就会对答案产生1的贡献,那么我们只需要求一下在i,j碰撞的情况下,又多少组是满足的,那么这个碰撞就会产生多少的贡献,那么我们就是让i向右走,让j向左走,然后求一下即可

// Problem: D. Moving Dots
// Contest: Codeforces - Codeforces Round 851 (Div. 2)
// URL: https://codeforces.com/problemset/problem/1788/D
// Memory Limit: 1024 MB
// Time Limit: 1500 ms

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include 
#include
#include
#define fi first
#define se second
#define i128 __int128
using namespace std;
typedef long long ll;
typedef double db;
typedef pair PII;
typedef pair > PIII;
const double eps=1e-7;
const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
inline void write(ll x,char ch) {write(x);putchar(ch);}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}

int T,hackT;
int n,m,k;
int w[N];

int qmi(int a,int b) {
	int res=1%mod;
	while(b) {
		if(b&1) res=(ll)res*a%mod;
		a=(ll)a*a%mod;
		b=b>>1;
	}
	return res;
}

void solve() {
	n=read();
	
	for(int i=1;i<=n;i++) w[i]=read();
	
	int res=0;
	for(int i=1;i<=n;i++) {
		for(int j=i+1;j<=n;j++) {
			int dist=abs(w[i]-w[j]);
			int l1=0,r1=i-1;
			while(l1>1;
				if(abs(w[i]-w[mid])>dist) l1=mid;
				else r1=mid-1;
			}
			int l2=j+1,r2=n+1;
			while(l2>1;
				if(abs(w[mid]-w[j])>=dist) r2=mid;
				else l2=mid+1;
			}
			
			int x=l1+n-l2+1;
			res=(res+qmi(2,x))%mod;
		}
	}
	
	printf("%d\n",res);
}   

int main() {
    // init();
    // stin();

    // scanf("%d",&T);
    T=1; 
    while(T--) hackT++,solve();
    
    return 0;       
}          

你可能感兴趣的:(codeforces,算法)