时间限制
500 ms
内存限制
64 MB
题目描述:
古人云:秀恩爱,分得快。
互联网上每天都有大量人发布大量照片,我们通过分析这些照片,可以分析人与人之间的亲密度。如果一张照片上出现了 K K K 个人,这些人两两间的亲密度就被定义为 1 / K 1/K 1/K。任意两个人如果同时出现在若干张照片里,他们之间的亲密度就是所有这些同框照片对应的亲密度之和。下面给定一批照片,请你分析一对给定的情侣,看看他们分别有没有亲密度更高的异性朋友?
输入格式:
输入在第一行给出 2 个正整数:N(不超过1000,为总人数——简单起见,我们把所有人从 0 到 N-1 编号。为了区分性别,我们用编号前的负号表示女性)和 M(不超过1000,为照片总数)。随后 M 行,每行给出一张照片的信息,格式如下:
K P [ 1 ] … P [ K ] K P[1] \dots P[K] KP[1]…P[K]
其中 K( ≤ 500 \le 500 ≤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
给定 m m m张照片,找出 A A A与 B B B亲密度最高的编号
emmmmmmm
首先照片中有
k
k
k个人,这些人两两之间的亲密度为
1
k
\frac{1}{k}
k1
统计每个人之间的亲密度的值
然后再找出每个人的最高亲密值
最后判断两人是否为彼此亲密值最高的一对
- 是则输出这两个人的编号
- 不是则需要分别遍历异性的编号,找到亲密值最高的编号
注:本题我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;
}
如果有说错的 或者 不懂的 尽管提 嘻嘻
一起进步!!!