P2671 求和

题目描述:这里
思路:

  1. 数学推导: a n s = ∑ i = 1 n S c o r e i ​ ans = \sum_{i = 1}^n Score_i​ ans=i=1nScorei,由此,复杂度为 O ( n ) O(n) O(n)
  2. 暴力+排序:对数组进行排序,使得复杂度降低,再进行穷举,复杂度 O ( n 2 ) O(n^2) O(n2)。再进行 O 2 O2 O2优化。
    代码:
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

template < typename T > void read(T &x)
{
	int f = 1;x = 0;char c = getchar();
	for (;!isdigit(c);c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c);c = getchar()) x = x * 10 + c - '0';
	x *= f;
}

struct node
{
	long long num, colour, id;
}a[100005];

bool cmp(node a, node b)
{
	if(a.id % 2 == b.id % 2)
	{
		if(a.colour == b.colour) return a.id < b.id;
		return a.colour < b.colour;
	}
	return a.id % 2 < b.id % 2;
}

const long long mod = 10007;

int main()
{
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	long long n, m;
	read(n);
	read(m);
	for(int i = 1;i <= n;i++)
	{
		a[i].id = i;
		read(a[i].num);
	}
	for(int i = 1;i <= n;i++)
		read(a[i].colour);
	sort(a + 1, a + n + 1, cmp);
	long long ans = 0;
	for(int i = 1;i <= n;i++)
		for(int j = i + 1;j <= n;j++)
			if((a[i].id + a[j].id) % 2 == 0 && a[i].colour == a[j].colour)
			{
				ans += (a[i].id + a[j].id) * (a[i].num + a[j].num);
				ans %= mod;
			}
			else break;
	cout << ans << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值