古人云:秀恩爱,分得快。
互联网上每天都有大量人发布大量照片,我们通过分析这些照片,可以分析人与人之间的亲密度。如果一张照片上出现了 K 个人,这些人两两间的亲密度就被定义为 1/K。任意两个人如果同时出现在若干张照片里,他们之间的亲密度就是所有这些同框照片对应的亲密度之和。下面给定一批照片,请你分析一对给定的情侣,看看他们分别有没有亲密度更高的异性朋友?
输入格式:
输入在第一行给出 2 个正整数:N(不超过1000,为总人数——简单起见,我们把所有人从 0 到 N-1 编号。为了区分性别,我们用编号前的负号表示女性)和 M(不超过1000,为照片总数)。随后 M 行,每行给出一张照片的信息,格式如下:
K P[1] … P[K]
其中 K(<= 500)是该照片中出现的人数,P[1] ~ P[K] 就是这些人的编号。最后一行给出一对异性情侣的编号 A 和 B。同行数字以空格分隔。题目保证每个人只有一个性别,并且不会在同一张照片里出现多次。
输出格式:
首先输出“A PA”,其中 PA 是与 A 最亲密的异性。如果 PA 不唯一,则按他们编号的绝对值递增输出;然后类似地输出“B PB”。但如果 A 和 B 正是彼此亲密度最高的一对,则只输出他们的编号,无论是否还有其他人并列。
输入样例 1:
10 4
4 -1 2 -3 4
4 2 -3 -5 -6
3 2 4 -5
3 -6 0 2
-3 2
输出样例 1:
-3 2
2 -5
2 -6
输入样例 2:
4 4
4 -1 2 -3 0
2 0 -3
2 2 -3
2 -1 2
-3 2
输出样例 2:
-3 2
对于该题目,如果你没有做对,先考虑一下,对0是否进行特殊处理,题目中分别用-0和0表示编号为0的女生和男生。
对于负数,用数组是非常不友好(其实就是不能用),但是编号的范围是-999到+999。那么我们可以在处理的时候将数据都加1000,就消除来负数对编码的影响。至于-0和0我们可以用2000和2001来特殊表示(只要不和别的数据冲突即可)。
我们邻接表来保存某位的异性朋友,inti[i][j] 来表示i和j之间的亲密度。
第一步:首先将照片信息保存下来。
第二步:获取要查询的情侣的编号a和b
第三步:遍历照片,将与a或b同框的异性分别加入a和b的邻接表中,并且更新相应的inti[a][i] 和 inti[b][j]
第四步:将a的异性按按照亲密度进行排序,b同理。
第五步:按照题目要求进行输出即可。需特殊判断两者互为最亲密情况。
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
#include <iostream>
using namespace std;
int strToint(string s) {
if (s == "-0") return 2000;
if (s == "0") return 2001;
return stoi(s)+1000;
}
void outPut(int x) {
if (x == 2000) printf("-0");
else if (x == 2001) printf("0");
else printf("%d", x - 1000);
}
bool isSameSex(int x, int y) {
if (x < 1000 && (y < 1000 || y == 2000)) return true;
if (x > 1000 && (y > 1000 && y != 2000)) return true;
return false;
}
double inti[2005][2005];
vector<int> photo[1005];
int n, m, k, id, a, b;
string str1, str2;
bool cmp1(int x, int y) {
if (inti[a][x] != inti[a][y]) {
return inti[a][x] > inti[a][y];
} else {
if (x == 2000 || x == 2001) x = 0; else x -= 1000;
if (y == 2000 || y == 2001) y = 0; else y -= 1000;
return abs(x) < abs(y);
}
}
bool cmp2(int x, int y) {
if (inti[b][x] != inti[b][y]) {
return inti[b][x] > inti[b][y];
} else {
if (x == 2000 || x == 2001) x = 0; else x -= 1000;
if (y == 2000 || y == 2001) y = 0; else y -= 1000;
return abs(x) < abs(y);
}
}
int main() {
scanf("%d %d", &n, &m);
memset(inti, 0, sizeof(inti));
for (int i = 0; i < m; i++) {
scanf("%d", &k);
for (int j = 0; j < k; j++) {
cin>>str1;
id = strToint(str1);
photo[i].push_back(id);
}
}
cin>>str1>>str2;
a = strToint(str1);
b = strToint(str2);
// printf("%d %d\n", a, b);
vector<int> fa;
vector<int> fb;
double maxa = 0.0, maxb = 0.0;
for (int i = 0; i < m; i++) {
if (find(photo[i].begin(), photo[i].end(), a) != photo[i].end()) {
for (int j = 0; j < photo[i].size(); j++) {
if (photo[i][j] == a) continue;
if (isSameSex(a, photo[i][j]) == true) continue;
inti[a][photo[i][j]] += (1.0/photo[i].size());
maxa = max(inti[a][photo[i][j]], maxa);
if (find(fa.begin(), fa.end(), photo[i][j]) != fa.end()) continue;
fa.push_back(photo[i][j]);
}
}
if (find(photo[i].begin(), photo[i].end(), b) != photo[i].end()) {
for (int j = 0; j < photo[i].size(); j++) {
if (photo[i][j] == b) continue;
if (isSameSex(b, photo[i][j]) == true) continue;
inti[b][photo[i][j]] += (1.0/photo[i].size());
maxb = max(inti[b][photo[i][j]], maxb);
if (find(fb.begin(), fb.end(), photo[i][j]) != fb.end()) continue;
fb.push_back(photo[i][j]);
}
}
}
sort(fa.begin(), fa.end(), cmp1);
sort(fb.begin(), fb.end(), cmp2);
vector<int> ans1, ans2;
if (inti[a][b] == maxa && inti[b][a] == maxb) {
outPut(a); printf(" ");
outPut(b); printf("\n");
return 0;
}
for (int i = 0; i < fa.size(); i++) {
if (inti[a][fa[i]] < maxa) break;
outPut(a); printf(" ");
outPut(fa[i]); printf("\n");
}
for (int i = 0; i < fb.size(); i++) {
if (inti[b][fb[i]] < maxb) break;
outPut(b); printf(" ");
outPut(fb[i]); printf("\n");
}
return 0;
}