天梯赛_L2-028 秀恩爱分得快(stl, set)

团体程序设计天梯赛

stl的使用。

使用数组记录性别,处理时注意0-0的区别,所以用字符串的方式读取。

先读取所有照片的数据,使用set数组记录。

因为只需要输出两个人与他人的亲密度,不需要计算这两个人之外的人,所以使用两个数组分别记录。在处理照片时,如果一个集合(对应一张照片)中有这两个人中的某一个,就在ta对应的数组中加上其他人的亲密度。

使用集合maxPerson1maxPerson2,搭配最大亲密度值maxCls1maxCls2,记录对某人亲密度最高的集合。

最后判断,格式化输出,由于set自带排序,所以不需要再考虑排序的问题。

L2-028 秀恩爱分得快

#include <bits/stdc++.h>
using namespace std;
vector<int> sex;    // female:0, male:1
vector<double> closeDeg[2];
vector<set<int> > photo;
int main() {
    int n, m;
    cin >> n >> m;
    sex.resize(n);
    photo.resize(m);
    closeDeg[0].resize(n);
    closeDeg[1].resize(n);
    for (int i = 0; i <= m - 1; i++) {
        int k;
        cin >> k;
        for (int j = 0; j <= k - 1; j++) {
            string temp;
            cin >> temp;
            if (temp[0] != '-') sex[stoi(temp)] = 1;
            photo[i].insert(abs(stoi(temp)));
        }
    }
    int a, b;
    cin >> a >> b;
    a = abs(a);
    b = abs(b);
    for (int i = 0; i <= m - 1; i++) {
        if (photo[i].find(a) != photo[i].end()) {
            for (auto j : photo[i]) closeDeg[0][j] += (double)1 / (int)photo[i].size();
            closeDeg[0][a] = 0;
        }
        if (photo[i].find(b) != photo[i].end()) {
            for (auto j : photo[i]) closeDeg[1][j] += (double)1 / (int)photo[i].size();
            closeDeg[1][b] = 0;
        }
    }
    double maxCls1 = 0, maxCls2 = 0;
    set<int> maxPerson1, maxPerson2;
    for (int i = 0; i <= n - 1; i++) {
        if (sex[a] == sex[i]) continue;
        if (closeDeg[0][i] - maxCls1 > 1e-6) {
            maxCls1 = closeDeg[0][i];
            maxPerson1.clear();
            maxPerson1.insert(i);
        }
        else if (abs(closeDeg[0][i] - maxCls1) < 1e-6)
            maxPerson1.insert(i);
    }
    for (int i = 0; i <= n - 1; i++) {
        if (sex[b] == sex[i]) continue;
        if (closeDeg[1][i] - maxCls2 > 1e-6) {
            maxCls2 = closeDeg[1][i];
            maxPerson2.clear();
            maxPerson2.insert(i);
        }
        else if (abs(closeDeg[1][i] - maxCls2) < 1e-6)
            maxPerson2.insert(i);
    }
    if (maxPerson1.find(b) != maxPerson1.end() && maxPerson2.find(a) != maxPerson2.end()) {
        string stra, strb;
        if (sex[a] == 0) stra = "-" + to_string(a);
        else stra = to_string(a);
        if (sex[b] == 0) strb = "-" + to_string(b);
        else strb = to_string(b);
        cout << stra << ' ' << strb << endl;
    }
    else {
        string stra, strb;
        if (sex[a] == 0) stra = "-" + to_string(a);
        else stra = to_string(a);
        if (sex[b] == 0) strb = "-" + to_string(b);
        else strb = to_string(b);
        for (auto it = maxPerson1.begin(); it != maxPerson1.end(); it++){
            string strit;
            if (sex[*it] == 0) strit = "-" + to_string(*it);
            else strit = to_string(*it);
            cout << stra << ' ' << strit << endl;
        }
        for (auto it = maxPerson2.begin(); it != maxPerson2.end(); it++) {
            string strit;
            if (sex[*it] == 0) strit = "-" + to_string(*it);
            else strit = to_string(*it);
            cout << strb << ' ' << strit << endl;
        }
    }
    return 0;
}