L2-028 秀恩爱分得快 (25 分)
谢谢阿康的帮助此题历经坎坷终于补上啦
阿康的博客:
题意:给出m张图片,每张图片里面有k个人,然后他们在同一张图片里面会增加1/k的亲密度,最后问x,y两人是不是互相亲密度最高的最佳情侣,是的话直接输出两人,不是的话,先输出x的亲密度最高的组合,在输出y亲密度最高的组合。里面男女会有“-”的差异,-0的存在会影响代码的正确性,所以需要用字符串输入,判端男女。然后题目保证男女的绝对值不会重复。
思路:题目大致是个模拟,一定要记住不要直接把照片转成二位矩阵,因为三层循环会超时。(之前我就是折磨做的。)还有亲密度可能为零也要输出。所以不要用零来当有没有在同一张照片里面出现过的判端依据。
剩下的代码直接看代码应该不难理解。
#include <bits/stdc++.h>
#include <limits.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<double, int> PII;
typedef pair<int, int> pii;
typedef unsigned long long ull;
const ll inf = 0x3f3f3f3f3f3f3f;
double g[1400][1400]; // 用来直接存储两人之间的亲密度
bool sex[1400]; //存储性别
vector<int> a[1400]; //存储每一张图片中的成员
vector<pii> v; //存储最后需要输出的答案
void print(int a, int b) {
if (sex[a]) cout << "-";
cout << a << " ";
if (sex[b]) cout << "-";
cout << b << endl;
}
bool cmp(
PII a,
PII b) { //原本用来排序的东西,现在不需要啦,因为直接下标从小到大找,就是他要的绝对值升序序列
if (a.first == b.first)
return abs(a.second) < abs(b.second); //绝对值递增排序
return a.first > b.first;
}
string s;
int getx() {
int x;
cin >> s;
x = abs(atoi(s.c_str()));
if (s[0] == '-') sex[x] = 1;
return x;
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int k;
cin >> k;
for (int j = 0; j < k; j++) {
int x = getx();
a[i].push_back(x);
}
}
int x, y;
x = getx();
y = getx();
double maxx = 0;
double mbxx = 0;
for (int i = 0; i < m; i++) {
if (find(a[i].begin(), a[i].end(), x) != a[i].end())
for (int j = 0; j < a[i].size(); j++) {
if (a[i][j] == x || sex[a[i][j]] == sex[x]) continue;
int v = a[i][j];
g[x][v] += (double)1.0 / (int)a[i].size();
maxx = max(maxx, g[x][v]);
}
if (find(a[i].begin(), a[i].end(), y) != a[i].end())
for (int j = 0; j < a[i].size(); j++) {
if (a[i][j] == y || sex[a[i][j]] == sex[y]) continue;
int v = a[i][j];
g[y][v] += (double)1.0 / (int)a[i].size();
mbxx = max(mbxx, g[y][v]);
}
}
if (maxx == g[x][y] && mbxx == g[y][x]) {
print(x, y);
return 0;
}
for (int i = 0; i < n; i++) {
if (g[x][i] == maxx&&sex[i] != sex[x]) v.push_back({x, i});
}
for (int i = 0; i < n; i++) {
if (g[y][i] == mbxx&&sex[i] != sex[y]) v.push_back({y, i});
}
for (int i = 0; i < v.size(); i++) {
print(v[i].first, v[i].second);
}
return 0;
}