题目链接
https://www.patest.cn/contests/gplt/L2-028
思路
0.只处理被询问的情侣的亲密度,否则会超时
1.要注意输入数字要用字符串,还要标记性别 因为 输出-0 得到的数字是0
也就是说用int 型输入 是没有办法 辨别编号0的性别的
2.要注意被询问的情侣可能没有出现在照片当中。
输出的时候也要注意负号
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const double PI = acos(-1);
const double E = exp(1);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int MOD = 1e9 + 7;
double ans[2][maxn];
map <int, int> M;
int tran(char s[])
{
int len = strlen(s);
int i;
if (s[0] == '-')
i = 1;
else
i = 0;
int ans = 0;
for ( ; i < len; i++)
{
ans = ans * 10 + s[i] - '0';
}
if (s[0] == '-')
M[ans] = -1;
else
M[ans] = 1;
return ans;
}
void print(int x, int y)
{
int a = abs(x);
int b = abs(y);
if (M[a] == -1)
printf("-%d ", a);
else
printf("%d ", a);
if (M[b] == -1)
printf("-%d\n", b);
else
printf("%d\n", b);
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
vector <int> G[maxn];
int num, tot;
char s[10];
for (int i = 0; i < m; i++)
{
scanf("%d", &tot);
for (int j = 0; j < tot; j++)
{
scanf("%s", s);
num = tran(s);
G[i].pb(num);
}
}
int A, B;
CLR(ans);
scanf("%s", s);
A = tran(s);
scanf("%s", s);
B = tran(s);
vector <int>::iterator it;
double Max[2] = { 0.0, 0.0};
for (int i = 0; i < m; i++)
{
int flag[2] = { 0, 0 };
for (it = G[i].begin(); it != G[i].end(); it++)
{
if (*it == abs(A))
flag[0] = 1;
if (*it == abs(B))
flag[1] = 1;
if (flag[0] && flag[1])
break;
}
if (flag[0])
{
double k = 1.0 / G[i].size();
for (it = G[i].begin(); it != G[i].end(); it++)
{
if (*it != abs(A) && M[*it] * M[abs(A)] == -1)
{
ans[0][*it] += k;
}
Max[0] = max(Max[0], ans[0][*it]);
}
}
if (flag[1])
{
double k = 1.0 / G[i].size();
for (it = G[i].begin(); it != G[i].end(); it++)
{
if (*it != abs(B) && M[*it] * M[abs(B)] == -1)
{
ans[1][*it] += k;
}
Max[1] = max(Max[1], ans[1][*it]);
}
}
}
if (ans[0][abs(B)] == Max[0] && ans[1][abs(A)] == Max[1])
{
print(A, B);
}
else
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < n; j++)
{
if (ans[i][j] == Max[i] && M[abs(i? B:A)] * M[j] == -1)
{
print(i? B:A, j);
}
}
}
}
}