团体天梯练习 L2-028 秀恩爱分得快
L2-028 秀恩爱分得快
古人云:秀恩爱,分得快。
互联网上每天都有大量人发布大量照片,我们通过分析这些照片,可以分析人与人之间的亲密度。如果一张照片上出现了
输入格式:
输入在第一行给出 2 个正整数:
其中
输出格式:
首先输出
输入样例 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,在之前计算过程中已经避免计算同性的亲密度了,然后顺序枚举的编号本身就是按照绝对值升序的。但是我们要求得编号对应的人的原始字符串,即判断这个人是男生还是女生的字符串,所以一开始在录入每个人信息时,需要开一个哈希映射
还有一个比较重要的要求,就是如果最亲密的人正好是对方,所有并列的人都不作数。这一点只需要用一下
/* 一切都是命运石之门的选择 El Psy Kongroo */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<double, double> pdd;
typedef pair<string, int> psi;
typedef __int128 int128;
#define PI acos(-1.0)
#define x first
#define y second
//int dx[4] = {1, -1, 0, 0};
//int dy[4] = {0, 0, 1, -1};
const int inf = 0x3f3f3f3f, mod = 1e9 + 7;
const int N = 1010;
struct node{
int id;
char gen;
};
map<int, string> mp; //编号 对应 人
double g[N][N]; //g[i][j]表示i对j的亲密度
int n, m, max_id; //max_id记录最大编号
//获取编号
int get_id(string s){
int res = 0;
if(s[0] == '-') s = s.substr(1);
for(auto &ch : s) res = res * 10 + ch - '0';
return res;
}
//获取当前照片中的人
void work(vector<node> &v){
string s; cin >> s;
int id = get_id(s); //获取编号
max_id = max(max_id, id); //记录最大编号
if(!mp.count(id)) mp[id] = s; //记录每个编号对应的人
node p = {id, (s[0] == '-' ? 'M' : 'F')};
v.push_back(p);
}
//获取最亲密的人
vector<string> get_max(string s){
vector<string> res;
int x = get_id(s);
double max_t = 0.0;
for(int y = 0; y <= max_id; y ++ ) max_t = max(max_t, g[x][y]);
for(int y = 0; y <= max_id; y ++ )
if(g[x][y] == max_t && g[x][y] != 0.0) res.push_back(mp[y]); //为0.0时 无需放入
return res;
}
bool check(string a, string b, vector<string> &v1, vector<string> &v2){
if(v1.empty() && v2.empty()) return true; //当没有照片时 两者最亲密对象为空
return find(v1.begin(), v1.end(), b) != v1.end() && find(v2.begin(), v2.end(), a) != v2.end(); //最亲密的人包含对方
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
while(m -- ){
int k; cin >> k;
double t = 1.0 / k;
vector<node> v;
for(int i = 0; i < k; i ++ ) work(v); //获取照片中所有的人
//计算照片中两两之间的亲密度
for(int i = 0; i < k - 1; i ++ )
for(int j = i + 1; j < k; j ++ )
if(v[i].gen != v[j].gen) //保证性别不同
g[v[i].id][v[j].id] += t, g[v[j].id][v[i].id] += t;
}
string a, b; cin >> a >> b;
auto v1 = get_max(a);
auto v2 = get_max(b);
if(check(a, b, v1, v2)) cout << a << ' ' << b << endl;
else{
for(auto &x : v1) cout << a << ' ' << x << endl;
for(auto &x : v2) cout << b << ' ' << x << endl;
}
return 0;
}
一切都是命运石之门的选择,本文章来源于博客园,作者:MarisaMagic,出处:https://www.cnblogs.com/MarisaMagic/p/17332958.html,未经允许严禁转载
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具