L2-028 秀恩爱分得快 (25分)

首先找好一个数据结构

#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
vector<set<int>> lst;
vector<int> gender;
int N, M;
//记录和转换 性别
//要用string的 因为有-0
int setGender(const string &x)
{
    int ret;
    if (x[0] == '-')
    {
        //去掉- 把后面的变成数字
        ret = stoi(x.substr(1));
        gender[ret] = 0;
    }
    else
    {
        ret = stoi(x);
        gender[ret] = 1;
    }
    return ret;
}
//找到最大的编号
auto mostInti(int x)
{
    set<int> ret;
    //存亲密度
    vector<double> f(N, 0.0);
    for (auto &i : lst)
    {
        //看有没有这个人
        if (i.count(x))
        {
            int k = i.size();
            //遍历这个照片
            for (auto &y : i)
            {
                //性别不同的话
                if (gender[x] != gender[y])
                {
                    f[y] += 1.0 / k;
                }
            }
        }
    }
    //寻找最大那个的数 注意不加星是迭代器
    double maxI = *max_element(f.begin(), f.end());
    for (int i = 0; i < N; i++)
    {
        //就是相等的话
        if (abs(maxI - f[i]) < 1e-7)
            ret.insert(i);
    }
    return ret;
}
//因为最后输出的时候有-0的情况 所以单独搞一下
string prt(int x)
{
    string ret = "";
    if (gender[x] == 0)
    {
        ret += "-";
    }
    ret += to_string(x);
    return ret;
}
int main()
{
    cin >> N >> M;
    lst.resize(N);
    gender.resize(N);
    for (int i = 0; i < M; i++)
    {
        int K;
        cin >> K;
        while (K--)
        {
            string x;
            cin >> x;
            lst[i].insert(setGender(x));
        }
    }
    string A, B;
    cin >> A >> B;
    int va = setGender(A);
    int vb = setGender(B);
    auto sa = mostInti(va);
    auto sb = mostInti(vb);
    //如果是a和b都有彼此
    if (sa.count(vb) && sb.count(va))
    {
        cout << A << " " << B << endl;
    }
    else
    {
        for (auto i : sa)
            cout << prt(va) << " " << prt(i) << endl;
        for (auto i : sb)
            cout << prt(vb) << " " << prt(i) << endl;
    }
    return 0;
}

发布了117 篇原创文章 · 获赞 27 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_45653525/article/details/104733951
今日推荐