7-17 奥运排行榜 (25 分)

本文详细解析了一道涉及多种排序算法的竞赛题目,通过构建结构体实现四种不同排序方法,并结合实际竞赛样例进行演示。文章深入介绍了如何利用C++进行高效编码,包括自定义比较函数、快速排序等关键步骤。

题目:

思路:针对四种排序方法构建四个结构体,按四种排序排完之后,把结果汇总到代表国家的一个结构体中。然后就是查询就是了。排序规则可通过下面的例子了解一下:

序列:g[0] = 1,g[1] = 2,g[2] = 2, g[3] = 3;

排名:1          ,2        ,2          ,3(并不是1,2,3,4)

代码: 

#include <bits/stdc++.h>
#define FRE() freopen("in.txt","r",stdin)
using namespace std;
const int maxn = 500;
typedef unsigned long long ll;
int n,q;

struct Country
{
    int g,s;
    int g_p,s_p;
} ct[maxn];

struct node
{
    double gold_p, silver_p;
    int g, s;
    int id;
} g[maxn],s[maxn],g_p[maxn],s_p[maxn];

void init()
{
    cin>>n>>q;
    for(int i = 0; i<n; i++)
    {
        int num;
        cin>>g[i].g>>s[i].s>>num;
        g_p[i].gold_p = 1.0*g[i].g/num;
        s_p[i].silver_p = 1.0*s[i].s/num;
        g[i].id = s[i].id = g_p[i].id = s_p[i].id = i;
    }
    return;
}

bool cmdg(node& a, node& b)
{
    if(a.g == b.g)
        return a.id<b.id;
    return a.g > b.g;
}
bool cmds(node& a,node& b)
{
    if(a.s==b.s)
        return a.id<b.id;
    return a.s > b.s;
}
bool cmdg_p(node& a,node& b)
{
    return a.gold_p > b.gold_p;
}
bool cmds_p(node& a,node& b)
{
    return a.silver_p > b.silver_p;
}

int main()
{
    init();
    sort(g,g+n,cmdg);
    int lastid = 0;
    ct[g[0].id].g = 0;
    for(int i = 1; i<n; i++)
    {
        if(g[i].g==g[i-1].g)
            ct[g[i].id].g = lastid;
        else
        {
            ct[g[i].id].g = i;
            lastid = i;
        }
    }
    sort(s,s+n,cmds);
    lastid = 0;
    ct[s[0].s].s = 0;
    for(int i = 1; i<n; i++)
    {
        if(s[i].s==s[i-1].s)
            ct[s[i].id].s = lastid;
        else
        {
            ct[s[i].id].s = i;
            lastid = i;
        }
    }
    sort(g_p,g_p+n,cmdg_p);
    lastid = 0;
    ct[g_p[0].id].g_p = 0;
    for(int i = 1; i<n; i++)
    {
        if(g_p[i].gold_p == g_p[i-1].gold_p)
            ct[g_p[i].id].g_p = lastid;
        else
        {
            ct[g_p[i].id].g_p = i;
            lastid = i;
        }
    }
    sort(s_p,s_p+n,cmds_p);
    lastid = 0;
    ct[s_p[0].id].s_p = 0;
    for(int i = 1; i<n; i++)
    {
        if(s_p[i].silver_p == s_p[i-1].silver_p)
            ct[s_p[i].id].s_p = lastid;
        else
        {
            ct[s_p[i].id].s_p = i;
            lastid = i;
        }
    }
    for(int i = 0; i<q; i++)
    {
        int tmp,f,ans;
        cin>>tmp;
        f = 1,ans = ct[tmp].g;
        if(ct[tmp].s<ans)
            f = 2,ans = ct[tmp].s;
        if(ct[tmp].g_p<ans)
            f = 3,ans = ct[tmp].g_p;
        if(ct[tmp].s_p<ans)
            f = 4,ans = ct[tmp].s_p;
        if(i)
            cout<<" ";
        cout<<ans+1<<":"<<f;
    }
    return 0;
}
/*
样例输入:
4 4
51 100 1000
36 110 300
6 14 32
5 18 40
0 1 2 3
样例输出:
1:1 1:2 1:3 1:4
*/

 

### 快速排序算法实现奥运排行榜的排序 快速排序是一种高效的排序算法,采用治策略来对数组进行排序。对于奥运排行榜的排序问题,可以按照金牌数、奖牌总数和国民总数等指标进行排序[^1]。以下是一个完整的解决方案,包括数据结构设计和快速排序算法的实现。 #### 数据结构设计 使用 `vector` 容器存储国家的相关信息,每个国家的信息可以用一个结构体表示,包含金牌数、奖牌总数和国民总数等字段。为了记录原始数据和排序后的数据,需要将数据存储两次:一次用于排序,另一次用于记录原始数据以便后续查询[^1]。 ```cpp struct Country { int goldMedals; // 金牌数 int totalMedals; // 奖牌总数 long population; // 国民总数 string name; // 国家名称 }; ``` #### 快速排序算法实现 快速排序的核心思想是选择一个基准值(pivot),将数组为两部:一部小于基准值,另一部大于基准值,然后递归地对这两部进行排序。在本例中,可以根据金牌数、奖牌总数和国民总数等指标定义排序规则。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; // 定义排序规则 bool compare(const Country& a, const Country& b) { if (a.goldMedals != b.goldMedals) return a.goldMedals > b.goldMedals; // 按金牌数降序排列 if (a.totalMedals != b.totalMedals) return a.totalMedals > b.totalMedals; // 按奖牌总数降序排列 return a.population < b.population; // 按国民总数升序排列 } // 快速排序函数 void quickSort(vector<Country>& countries, int left, int right) { if (left >= right) return; int i = left, j = right; Country pivot = countries[left]; while (i < j) { while (i < j && compare(countries[j], pivot)) j--; countries[i] = countries[j]; while (i < j && compare(pivot, countries[i])) i++; countries[j] = countries[i]; } countries[i] = pivot; quickSort(countries, left, i - 1); quickSort(countries, i + 1, right); } int main() { vector<Country> countries = {{"USA", 39, 113, 331000000}, {"China", 38, 88, 1400000000}, {"UK", 22, 65, 67000000}}; vector<Country> originalCountries = countries; // 记录原始数据 quickSort(countries, 0, countries.size() - 1); cout << "Sorted Countries:" << endl; for (const auto& country : countries) { cout << country.name << " Gold: " << country.goldMedals << " Total: " << country.totalMedals << " Population: " << country.population << endl; } return 0; } ``` 上述代码实现了基于金牌数、奖牌总数和国民总数的排序逻辑,并使用了快速排序算法[^1]。排序后输出各国的排名信息。 #### 使用 `map` 存储排名 为了方便查询每个国家在不同排名方式下的成绩,可以使用 `map` 容器存储排名信息。例如,可以将排名方式作为键,排名作为值存储在 `map` 中[^1]。 ```cpp #include <map> map<string, int> rankings; for (int i = 0; i < countries.size(); ++i) { rankings[countries[i].name] = i + 1; // 排名从1开始 } ``` 通过这种方式,可以快速查询任意国家在当前排名方式下的排名。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值