题目传送门:U552380
本题与投票1类似,只是加强了难度,不做过多解释,题解:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
struct Node {
long long a; // 票数
string name; // 作品名称
};
bool cmp(const Node& A, const Node& B) {
return A.a > B.a; // 按票数从大到小排序
}
int main() {
int n;
cin >> n; // 读取作品数量
vector<Node> works(n); // 使用vector动态分配内存,存储所有作品的信息
for (int i = 0; i < n; i++) {
cin >> works[i].name >> works[i].a; // 读取作品名称和票数
}
sort(works.begin(), works.end(), cmp); // 对作品按票数排序
cout << works[0].name << endl; // 输出票数最多的作品名称
// 输出所有作品名称,按票数从大到小排列
for (const auto& work : works) {
cout << work.name;
if (&work != &works.back()) {
cout << " "; // 不是最后一个作品时,输出空格分隔
}
}
cout << endl; // 换行
return 0;
}
不做过多解释了,写的可能比较麻烦,欢迎大佬们写出更好题解!
紧凑版阅读:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
struct Node {
long long a; // 票数
string name; // 作品名称
};
bool cmp(const Node& A, const Node& B) {
return A.a > B.a; // 按票数从大到小排序
}
int main() {
int n;
cin >> n; // 读取作品数量
vector<Node> works(n); // 使用vector动态分配内存,存储所有作品的信息
for (int i = 0; i < n; i++) {
cin >> works[i].name >> works[i].a; // 读取作品名称和票数
}
sort(works.begin(), works.end(), cmp); // 对作品按票数排序
cout << works[0].name << endl; // 输出票数最多的作品名称
// 输出所有作品名称,按票数从大到小排列
for (const auto& work : works) {
cout << work.name;
if (&work != &works.back()) {
cout << " "; // 不是最后一个作品时,输出空格分隔
}
}cout << endl; // 换行
return 0;
}