CODEFORCES ROUND#624 DIV3

这次比赛从名字就可以看出非常水,然鹅因为第一次打codeforces不太熟悉操作只来的及做签到题(还错了一次)

A,B,C都是签到题考点思维就不写了

D题

https://codeforces.ml/problemset/problem/1311/D

题目大意是:有t组数据每组数据给你三个数,a,b,c每次一个数加一或

者减一都算一次操作(不能为变负数),问最小的操作次数构造出A,

B,C使b%a==0,c%b==0。

t<1000,a,b,c<1e4 时限2s

首先想到是找b的倍数与a,c最近的数再直接加减

再一看时限两秒。。。果断暴力

然后就枚举这样的数对A,B,C,答案就是其a,b,c的差的最小值

#代码后面有时间再补

E题

https://codeforces.ml/contest/1311/problem/E

奈何本人没文化,题目没怎么懂,日后有时间再补(flag)

接下来才是重头戏


F题

https://codeforces.ml/problemset/problem/1311/F

题目大意:给你n(<2e5)个点a1,a2,...,an在同一个数轴上,保证坐标不会重复, 每个点有一个固定的速度(-1e8到1e8),对某一个t时刻,某两个点距离最小,输出这些最小值之和(min d(i,j)i<j之和)

容易先想到将a按坐标排序,xi<xj,vi<vj那d(i,j)肯定不会减小则min d(i,j)=xj-xi,如果vi>vj 则min d(i,j)=0


暴力n^2代码很简单

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=2e5+5;
int n;
long long tot;
struct node
{
	long long x,v; 
}a[N]; 
bool cmp(node x,node y)
{
	return x.x<y.x;
}
int main()
{

	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i].x);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i].v);
	sort(a+1,a+n+1,cmp);
	for(int i=2;i<=n;i++)
	{
		for(int j=1;j<i;j++)
		{
			if(a[i].v>=a[j].v)
			{
				tot+=a[i].x-a[j].x;
			}
		}
	}
	printf("%lld",tot);
	return 0;
} 

  

因为ans+=xi⋅cnt−sum可以将每次枚举看成从当前新建的数组中添加一个元素,因为从小到大遍历的所以速度小于i而x大于i的一定还没有被加入新建的数组所以此想法可以算出满足条件的num和tot。

具体做法是新建两个数组q,p开始都为空,第一个数组q[i]代表a[1~i].v中速度为q的a[1~i].x(坐标)的和,第二个数组p[i]代表速度为p[i]的个数而这可以用树状数组优化。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=2e5+5;
int n,m,pos,v[N];
long long tot,s[N][2];
struct node
{
	long long x,v; 
}a[N]; 
bool cmp(node x,node y)
{
	return x.x<y.x;
}
int lowbit(int x)
{
	return x&(-x);
}
void update(int x,int val)//更新数组 
{
	while(x<=n)
	{
		s[x][0]++;
		s[x][1]+=val;
		x+=lowbit(x);//从x往上更新所有q,p数组有关a[x]的值 
	}
}
long long getsum(int x,int flag)//获取数组1~x的和 
{
	long long res=0;
	while(x)
	{
		res+=s[x][flag];
		x-=lowbit(x);	//从x往下加故是减号 
	}	
	return res;
}
int main()
{

	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i].x);
	for(int i=1;i<=n;i++)
	{	
		scanf("%lld",&a[i].v);
		v[i]=a[i].v;
	}
	sort(a+1,a+n+1,cmp);
	sort(v+1,v+n+1);
	m=unique(v+1,v+n+1)-v-1;
	for(int i=1;i<=n;i++)
	{
		pos=lower_bound(v+1,v+m+1,a[i].v)-v;
		tot+=getsum(pos,0)*a[i].x-getsum(pos,1);//s[][0]代表p[i],s[][1]代表q[i] 
		update(pos,a[i].x);
	}
	printf("%lld",tot);
	return 0;
} 

  

 

posted @ 2020-02-26 17:29  Sakura_Momoko  阅读(220)  评论(0编辑  收藏  举报