我正在创建一个程序,您输入n个蘑菇采摘者,他们参加了蘑菇采摘比赛,他们可以找到shroomA(价值5分),shroomB(价值3分)和shroomC(价值15分) 。我需要找到比赛的获胜者并打印他/她的名字,但是如果两个或多个参赛者的得分相同,那么他们将被取消比赛资格,这意味着我需要找到最高的非重复结果。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class ShroomPicker {
private:
string name;
long long int shroomA, shroomB, shroomC;
public:
void Input() {
char Name[100];
long long int shrooma, shroomb, shroomc;
cin >> Name >> shrooma >> shroomb >> shroomc;
name = Name;
shroomA = shrooma; shroomB = shroomb; shroomC = shroomc;
}
long long int calcPoints() {
return shroomA * 5 + shroomB * 3 + shroomC * 15;
}
string winnersName() {
return name;
}
};
int main() {
int n;
cin >> n;
vector<ShroomPicker> shr;
for (int i = 0; i < n; i++) {
ShroomPicker s;
s.Input();
shr.push_back(s);
}
long long int hiscore = 0;
int num = 0;
for (int i = 0; i < n; i++) {
long long int temp = 0;
temp = shr[i].calcPoints();
if (temp > hiscore) {
hiscore = temp;
num = i;
}
}
cout << shr[num].winnersName();
}
我制作了这个程序,即使重复多次也能找到最高分,有人可以建议我如何找到最高的非重复分值吗?
编辑:
for (int i = 0; i < n; i++) {
long long int temp = 0;
temp = shr[i].calcPoints();
if (scoreMap.find(temp) == scoreMap.end()) {
scoreMap[temp] = Info{ i, false };
}
else {
scoreMap[temp] = Info{ i, true };
}
}
答案 0 :(得分:0)
我建议按照减少的蘑菇数量(O [nlogn])对参与者列表进行排序,然后从头到尾浏览列表(O [n] max)。采摘蘑菇数量与相邻参与者(在排序列表中)不同的第一个参与者是获胜者。
答案 1 :(得分:0)
我能想到的最快(O(N))方法是:
struct Info
{
int picker_index;
bool disqualified;
}
// map from score to the Info object above
std::unordered_map<int, Info> scoreMap;
遍历选择器并更新地图,如下所示:
-如果地图中没有项目,只需添加scoreMap [score] = Info {picker_index,false};
-否则,在现有项目上设置disqualified = true;
一旦构建了映射,就在映射中找到不合格=假的最大密钥;类似于您现在正在做的事情。