我的题解
2026-01-11 21:33:44
发布于:广东
3阅读
0回复
0点赞
这是我的正确代码:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> number(n), color(n);
for (int i = 0; i < n; i++) cin >> number[i];
for (int i = 0; i < n; i++) cin >> color[i];
unordered_map<int, vector<int>> groups;
for (int i = 0; i < n; i++) {
groups[color[i]].push_back(i);
}
long long ans = 0;
const int MOD = 10007;
for (auto &p : groups) {
auto &indices = p.second;
for (int i = 0; i < indices.size(); i++) {
for (int j = i + 1; j < indices.size(); j++) {
int x = indices[i], z = indices[j];
if ((x + z) % 2 == 0) {
long long id_sum = (x + 1) + (z + 1);
long long num_sum = number[x] + number[z];
ans = (ans + id_sum * num_sum) % MOD;
}
}
}
}
cout << ans % MOD << endl;
return 0;
}
反正想的是分组算,这样再到更小的组里枚举时间复杂度就不会炸了。
标准的错误答案(我之前写的):
#include<bits/stdc++.h>
int main() {
int x, y, z;
int n, m;
std::cin >> n >> m;
int number[n], color[n];
for (int i = 0; i < n; i++) {
std::cin >> number[i];
}
for (int i = 0; i < n; i++) {
std::cin >> color[i];
}
std::vector<std::pair<int, int>> pairsForColors;
for (int x = 0; x < n; x++) {
for (int z = x + 1; z < n; z++) {
if (color[x] == color[z]) {
pairsForColors.push_back({x + 1, z + 1});
}
}
}
std::set<std::pair<int, int>> founded;
for (int x = 0; x < pairsForColors.size(); x++) {
for (int y = 2; y <= 2 * n; y += 2) {
if ((pairsForColors[x].first + pairsForColors[x].second) == y) {
founded.insert({pairsForColors[x].first, pairsForColors[x].second});
}
}
}
int itTurnsOut = 0;
for (std::pair<int, int> x : founded) {
itTurnsOut += ((x.first + x.second) * (number[x.first - 1] + number[x.second - 1]));
}
std::cout << itTurnsOut % 10007;
}
之前写的还会MLE、WA。反正看得懂的都看懂了
全部评论 1
虽然不会有很顶尖的性能但是我感觉是我有史以来写的最稳妥的一个了。
2026-01-11 来自 广东
0


有帮助,赞一个