CodeForces - 76E

博客讲述了如何使用数学知识优化暴力算法,将复杂度从O(N^2)降低到O(N),从而解决CodeForces上的76E问题。通过合并X和Y的平方项,实现了在时限内求解的答案。代码中展示了实现这一优化的过程。
摘要由CSDN通过智能技术生成

CodeForces - 76E

题目: https://codeforces.com/problemset/problem/76/E
在这里插入图片描述
在这里插入图片描述
暴力会超时,需要用一点数学知识,把对应的(xi - xj)^2 + (yi - yj)^2 拆分一下,具体如上,然后分别合并X和Y,不合并是N^2复杂度,合并后O(n),不会超时,推理如上

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <stack>
 
using namespace std;

typedef pair<int,int> PII;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double PI = acos(-1);

const int N = 100010;
ll x, y, s, ans, res, t, tt;
int n;
int main()
{
	scanf("%d", &n);
	for(int i = 0;i < n;i++)
	{
		scanf("%lld%lld", &x, &y);
		ans += x * x;
		res += y * y;
		t += x;
		tt += y;
	}	
	printf("%lld\n", n * (ans + res) - t * t - tt * tt);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

噶米困了

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

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

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

打赏作者

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

抵扣说明:

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

余额充值