1025 PAT Ranking (25 分)

本文介绍了一种用于Programming Ability Test(PAT)竞赛中合并多个地点的排名列表并生成最终排名的算法。通过输入各测试地点的注册号和总分,算法能够正确地将所有排名列表合并,并按最终排名和注册号顺序输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

#include<bits/stdc++.h>
using namespace std;
struct node
{
    string id;
    int score;
    int final_rank;
    int location_num;
    int local_rank;
};
bool cmp(const node &a,const node &b)
{
    if(a.score!=b.score)
        return a.score>b.score;
    else
        return a.id<b.id;
}

int  main()
{
    int n,k;
    cin>>n;
    vector<node>v;
    for(int i=1; i<=n; i++)
    {
        cin>>k;
        vector<node>stu(k);
        for(int j=0; j<k; j++)
        {
            cin>>stu[j].id>>stu[j].score;
            stu[j].location_num=i;
        }
        sort(stu.begin(),stu.end(),cmp);
        stu[0].local_rank=1;
        v.push_back(stu[0]);
        for(int j=1; j<k; j++)
        {
            //stu[j].local_rank = (stu[j].score == stu[j - 1].score) ? (stu[j - 1].local_rank) : (j + 1);
            if(stu[j].score==stu[j-1].score)
                stu[j].local_rank = stu[j-1].local_rank;
            else
                stu[j].local_rank = j+1;
            v.push_back(stu[j]);
        }
    }
    sort(v.begin(),v.end(),cmp);
    v[0].final_rank=1;
    for(int i=1;i<v.size();i++)
    {
        //v[i].final_rank = (v[i].score == v[i - 1].score) ? (v[i - 1].final_rank) : (i + 1);
        if(v[i].score==v[i-1].score)
            v[i].final_rank = v[i-1].final_rank;
        else
            v[i].final_rank = i+1;
    }
    cout<<v.size()<<endl;
    for(int i=0;i<v.size();i++)
        cout<<v[i].id<<" "<<v[i].final_rank<<" "<<v[i].location_num<<" "<<v[i].local_rank<<endl;
    return 0;
}
 

 

### Ranking Loss 的概念 Ranking loss 是一种用于衡量模型预测排名顺序与真实标签之间差异的损失函数。这种类型的损失函数广泛应用于信息检索、推荐系统以及自然语言处理等领域,特别是在涉及排序的任务中。 ### Ranking Loss 的计算方法 常见的 ranking loss 形式之一是成对排序损失 (Pairwise Ranking Loss),其核心思想是比较不同样本之间的相对位置关系。具体来说,对于任意一对正负样例 \(i\) 和 \(j\),如果希望正样例子 \(i\) 排名高于负样例子 \(j\),则定义如下形式的损失: \[L_{rank}(f(x_i), f(x_j)) = \max(0, 1 - (f(x_i) - f(x_j)))\] 其中 \(f(\cdot)\) 表示评函数[^1]。该公式表明当正样例得低于或等于负样例时会产生惩罚;反之,则不会增加额外成本。 另一种常用的 ranking loss 是列表级别的 RankNet 损失函数,在此框架下,通过比较每一对文档的概率布来优化整个查询结果列表的质量: \[L(y_1,y_2)=E[\log(1+\exp(-t(s_1-s_2)))]\] 这里 \(s_1,s_2\) 别代表两个文档的相关度数,而\( t=\text{sign}(y_1-y_2)\)决定了这对文档的真实偏好方向。 ### 应用场景 - **搜索引擎**:提高搜索结果页面上高质量网页的位置。 - **广告投放平台**:确保高点击率的商品展示给更多潜在客户。 - **社交网络中的好友推荐**:优先显示那些更有可能成为新朋友的人选。 - **学术论文引用析**:帮助学者找到最相关的研究资料。 ```python def pairwise_ranking_loss(pos_score, neg_score): """Calculate Pairwise Ranking Loss.""" margin = 1.0 losses = torch.relu(margin - pos_score + neg_score) return losses.mean() ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值