题目描述:这里
思路:
- 数学推导: 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)
- 暴力+排序:对数组进行排序,使得复杂度降低,再进行穷举,复杂度
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;
}