题目解析
给定张照片,找出与亲密度最高的编号
解题思路
首先照片中有个人,这些人两两之间的亲密度为
统计每个人之间的亲密度的值
然后再找出每个人的最高亲密值
最后判断两人是否为彼此亲密值最高的一对
- 是则输出这两个人的编号
- 不是则需要分别遍历异性的编号,找到亲密值最高的编号
注:本题我java版本的过不去,第4、5个测试数据 TLE 了
代码
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n = sc.nextInt();
int m = sc.nextInt();
double map[][] = new double[n + 10][n + 10]; // 两个人之间的亲密度
double max[] = new double[n + 10]; // 每个人的最大亲密值
TreeSet<Integer> nan = new TreeSet<Integer>(); // 男生
TreeSet<Integer> nv = new TreeSet<Integer>(); // 女生
while (m-- > 0)
{
int k = sc.nextInt();
ArrayList<Integer> a = new ArrayList<Integer>(); // 当前照片中的男生
ArrayList<Integer> b = new ArrayList<Integer>(); // 当前照片中的女生
for (int t = 1; t <= k; t++)
{
String s = sc.next();
int x = Math.abs(Integer.valueOf(s));
if (s.charAt(0) == '-') // 如果是女生
{
b.add(x);
nv.add(x);
} else // 否则是男生
{
a.add(x);
nan.add(x);
}
}
// 两两统计亲密值
for (int i : a)
{
for (int j : b)
{
// 两个人的亲密度增加1/k
map[i][j] += 1.0 / k;
map[j][i] += 1.0 / k;
// 找到当前这个人的最大亲密度
max[i] = Math.max(max[i], map[i][j]);
max[j] = Math.max(max[j], map[j][i]);
}
}
}
String s1 = sc.next();
String s2 = sc.next();
int a1 = Math.abs(Integer.valueOf(s1));
int a2 = Math.abs(Integer.valueOf(s2));
// 两个人亲密度彼此是最高的一对
if (max[a1] == map[a1][a2] && max[a2] == map[a2][a1])
out.println(s1 + " " + s2);
else
{
if (s1.charAt(0) == '-') // 如果是女生
{
for (int i : nan) // 遍历男生
{
if (max[a1] == map[i][a1]) // 与a1亲密度最高值相等
out.printf("-%d %d\n", a1, i);
}
} else // 否则是男生
{
for (int i : nv) // 遍历女生
{
if (max[a1] == map[a1][i]) // 与a1亲密度最高值相等
out.printf("%d -%d\n", a1, i);
}
}
if (s2.charAt(0) == '-') // 如果是女生
{
for (int i : nan) // 遍历男生
{
if (max[a2] == map[i][a2]) // 与a1亲密度最高值相等
out.printf("-%d %d\n", a2, i);
}
} else // 否则是男生
{
for (int i : nv) // 遍历女生
{
if (max[a2] == map[a2][i]) // 与a1亲密度最高值相等
out.printf("%d -%d\n", a2, i);
}
}
}
out.flush();
out.close();
}
static Scanner sc = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
}
c++
#include <iostream>
#include <vector>
#include <set>
using namespace std;
const int N = 1e3 + 10;
double mp[N][N], mx[N];
set<int> nan, nv;
int main()
{
int n, m; cin >> n >> m;
while(m -- > 0)
{
int k; cin >> k;
vector<int> a, b;
for(int i = 1; i <= k; i ++)
{
string s; cin >> s;
int x = abs(stoi(s));
if(s[0] == '-')
{
b.push_back(x);
nv.insert(x);
}
else
{
a.push_back(x);
nan.insert(x);
}
}
for(int i : a)
{
for(int j : b)
{
mp[i][j] += 1.0 / k;
mp[j][i] += 1.0 / k;
mx[i] = max(mx[i], mp[i][j]);
mx[j] = max(mx[j], mp[j][i]);
}
}
}
string s1, s2; cin >> s1 >> s2;
int a1 = abs(stoi(s1));
int a2 = abs(stoi(s2));
if(mx[a1] == mp[a1][a2] && mx[a2] == mp[a2][a1])
cout << s1 << " " << s2;
else
{
if(s1[0] == '-')
{
for(int i : nan)
{
if(mx[a1] == mp[i][a1])
cout << s1 << " " << i << endl;
}
}
else
{
for(int i : nv)
{
if(mx[a1] == mp[a1][i])
cout << s1 << " -" << i << endl;
}
}
if(s2[0] == '-')
{
for(int i : nan)
{
if(mx[a2] == mp[i][a2])
cout << s2 << " " << i << endl;
}
}
else
{
for(int i : nv)
{
if(mx[a2] == mp[a2][i])
cout << s2 << " -" << i << endl;
}
}
}
return 0;
}