L2-028 秀恩爱分得快
90行,调了俩小时,大约有以下坑点。
1.每个数字都可能正负出现,比如-0 0,-1 1,一开始以为一个数的正负只会出现一个。
2.当俩人都不出现在照片中,那么输出俩人就行
3.当其中一个人不在照片里的时候,那个不在照片里的人不用输出,一开始以为既然是情侣,所以不在照片里的那个那么肯定和自己的对象最亲密,是我想多了。
对,大概就这些了
#include <bits/stdc++.h>
using namespace std;
constexpr double eps = 1e-8;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<string>> p;
for (int i = 0; i < m; i++) {
int k;
cin >> k;
vector<string> v(k);
for (int j = 0; j < k; j++) {
string s; cin >> s;
if (s[0] != '-') s = "+" + s;
v[j] = s;
}
p.push_back(v);
}
string a, b;
cin >> a >> b;
if (a[0] != '-') a = "+" + a;
if (b[0] != '-') b = "+" + b;
map<pair<string, string>, double> intimacy;
set<string> S[2];
for (int i = 0; i < m; i++) {
bool havea = false, haveb = false;
if (find(p[i].begin(), p[i].end(), a) != p[i].end()) havea = true;
if (find(p[i].begin(), p[i].end(), b) != p[i].end()) haveb = true;
int K = (int)p[i].size();
for (int j = 0; j < p[i].size(); j++) {
if (havea && p[i][j] != a && p[i][j][0] != a[0]) intimacy[{a, p[i][j]}] += 1.0 / K, S[0].insert(p[i][j]);
if (haveb && p[i][j] != b && p[i][j][0] != b[0]) intimacy[{b, p[i][j]}] += 1.0 / K, S[1].insert(p[i][j]);
}
}
vector<string> v[2];
for (int i = 0; i < 2; i++) {
for (auto x : S[i]) {
v[i].push_back(x);
}
}
sort(v[0].begin(), v[0].end(), [&](string x, string y) {
if (fabs(intimacy[{a, x}] - intimacy[{a, y}]) < eps) return abs(stoi(x)) < abs(stoi(y));
return intimacy[{a, x}] > intimacy[{a, y}];
});
sort(v[1].begin(), v[1].end(), [&](string x, string y) {
if (fabs(intimacy[{b, x}] - intimacy[{b, y}]) < eps) return abs(stoi(x)) < abs(stoi(y));
return intimacy[{b, x}] > intimacy[{b, y}];
});
double mxv0 = 0.0;
if (!v[0].empty()) mxv0 = intimacy[{a, v[0][0]}];
double mxv1 = 0.0;
if (!v[1].empty()) mxv1 = intimacy[{b, v[1][0]}];
//double mx = max(mxv0, mxv1);
double mx = max(mxv1, mxv0);
function<void(string, string)> print = [&] (string a, string b) {
if (a[0] == '+') cout << stoi(a) << " ";
else cout << a << " ";
if (b[0] == '+') cout << stoi(b) << "\n";
else cout << b << "\n";
};
if (fabs(intimacy[{a, b}] - mxv0) < eps && fabs(intimacy[{b, a}] - mxv1) < eps) print(a, b);
else {
for (int i = 0; i < v[0].size(); i++) {
if (fabs(intimacy[{a, v[0][i]}] - mxv0) < eps) {
print(a, v[0][i]);
//cout << a << " " << v[0][i] << "\n";
}
}
for (int i = 0; i < v[1].size(); i++) {
if (fabs(intimacy[{b, v[1][i]}] - mxv1) < eps) {
print(b, v[1][i]);
//cout << b << " " << v[1][i] << "\n";
}
}
}
return 0;
}