1012. The Best Rank (25)

本文介绍了一种用于解决PAT竞赛中学生排名问题的算法。该算法通过对学生在不同科目及平均成绩上的表现进行综合评估,确定每位学生的最优排名。文章详细展示了算法实现过程,包括如何处理重复排名情况,并提供了完整的C++代码示例。

1012. The Best Rank (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algebra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks -- that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of C, M, E and A - Average of 4 students are given as the following:

StudentID  C  M  E  A
310101     98 85 88 90
310102     70 95 88 84
310103     82 87 94 88
310104     91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (<=2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.

Output

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output "N/A".

Sample Input
5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999
Sample Output
1 C
1 M
1 E
1 A
3 A
N/A
主要思路:
1、使用map简历学生id与输入顺序号的联系
2、排名为实际排名,若有2个第一名,就没有第2名了
3、用一个二维数组存放每个学生的每科和平均成绩排名(不需要这样做,查哪个学生就去search这个学生的排名就好)
4、此代码通过PAT所有测试点,但是牛客网上一个测试点都通不过,why?
#include <iostream>
#include <string>
#include <map>

using namespace std;
#define Max_N 2001

void Rank_Per_Project(int* Array,int flag,int n,int (*Id)[6]) //将每一位学生某单个科目的排名存入Id二维数组的flag位
{
    int temp=0,count=1;
    for (int i=0; i<n; ++i)
    {
        temp=Array[i];
        count=1;
        for (int k=0; k<n; ++k)
        {
            if (Array[k]>temp)
            {
                count++;
            }
        }
        Id[i][flag]=count;
    }
}

void Rank(int *C,int *M,int* E,int*A,int (*Id)[6],int n,string str)
{
    Rank_Per_Project(C,1,n,Id); //处理每个学生C科目成绩的排名,并存入该学生Id数组
    Rank_Per_Project(M,2,n,Id);  //处理每个学生M科目成绩的排名,并存入该学生Id数组
    Rank_Per_Project(E,3,n,Id); //处理每个学生E科目成绩的排名,并存入该学生Id数组
    Rank_Per_Project(A,0,n,Id); //处理每个学生平均成绩的排名,并存入该学生Id数组
    
    int rank=n+1;
    int flag=-1;
    for (int i=0; i<n; ++i) //处理每一个学生的最佳排名,并存入Id[][4],Id[][5]放入标志,Id二维数组的每行按课程优先级放置,低优先级的科目必须排名更好,才能记录到该学生的最佳排名
    {
        rank=n+1;
        for (int k=0; k<4; ++k)
        {
            if (Id[i][k]<rank)
            {
                rank=Id[i][k];
                flag=k;
            }
        }
        Id[i][4]=rank;
        Id[i][5]=flag;
    }
}

int main(int argc, const char * argv[])
{
    
    int n,m;
    cin>>n>>m;
    
    int C[Max_N]={0};
    int M[Max_N]={0};
    int E[Max_N]={0};
    int A[Max_N]={0};
    int Id[Max_N][6]={0};
    
    map<string,int> relation;//创建每一个学生id与输入顺序号的关联,方便后续处理
    
    string id;
    for (int i=0; i<n; ++i)
    {
        cin>>id>>C[i]>>M[i]>>E[i];
        A[i]=C[i]+M[i]+E[i];
        relation[id]=i;
    }
    
    string str="ACME";
    Rank(C, M, E, A,Id,n,str);
    map<string,int>::iterator it;
    
    
    
    string search[Max_N];
    int sequence=0;
    for (int i=0; i<m; ++i)
    {
        cin>>search[i];
    }
    
    for (int i=0; i<m; ++i)
    {
        it=relation.find(search[i]);
        if (it!=relation.end())//成功找到该学生
        {
            sequence=it->second;
            cout<<Id[sequence][4]<<' '<<str[Id[sequence][5]]<<endl;;
        }else//未找到该学生信息
        {
            cout<<"N/A"<<endl;
        }
    }
   
    
    
    return 0;
}


To determine the number of distinct sets of 13 cards for which Player A can guarantee a win in a poker game where each player selects 5 cards and Player B wins on ties, the problem revolves around combinatorics and game theory. ### Problem Interpretation: - A standard deck has 52 cards. - Player A selects 13 cards as their hand. - From these 13 cards, Player A must be able to choose a 5-card poker hand that beats any 5-card poker hand that Player B could form from the remaining 39 cards. - If both players have the same rank of hand (e.g., both have a flush), Player B wins by default. - The objective is to count how many such 13-card combinations exist where Player A can always select a 5-card hand strictly better than any 5-card hand Player B could choose. ### Key Observations: - There are $\binom{52}{13}$ total possible 13-card hands. - Not all of these hands will allow Player A to guarantee a win. - The solution involves evaluating the strength of all possible 5-card combinations from the 13-card set and ensuring that none of the 5-card combinations from the remaining 39 cards can tie or beat them. ### Strategy to Solve: 1. **Understand Poker Hand Rankings**: The ranking of poker hands from highest to lowest is: Royal Flush, Straight Flush, Four of a Kind, Full House, Flush, Straight, Three of a Kind, Two Pair, One Pair, High Card. 2. **Generate All 13-Card Hands**: The number of ways to choose 13 cards from 52 is $\binom{52}{13}$, which is approximately $6.35 \times 10^{11}$. 3. **Evaluate Each 13-Card Hand**: - For each 13-card hand, generate all $\binom{13}{5} = 1287$ possible 5-card combinations. - Determine the best possible 5-card hand from these combinations. - From the remaining 39 cards, generate all $\binom{39}{5} = 575757$ possible 5-card combinations for Player B. - Check if any of Player B’s hands can tie or beat Player A’s best hand. 4. **Count Winning Hands for Player A**: - If Player A’s best 5-card hand beats all possible 5-card hands from the remaining 39 cards, count that 13-card set as a winning hand. - This requires a poker hand evaluator to compare the strength of hands. 5. **Implement Efficient Comparison**: - Use a precomputed lookup table for poker hand strengths. - Implement a fast comparison algorithm to avoid redundant computations. - Parallelize the computation to handle the large search space. 6. **Optimize with Pruning**: - Discard 13-card hands early if their best 5-card hand cannot beat the minimum possible strength of Player B’s hands. - For example, if Player A’s best hand is a high card, and Player B can form a pair, discard that 13-card set. 7. **Final Count**: - After evaluating all possible 13-card combinations and filtering out those where Player A cannot guarantee a win, the remaining count is the desired answer. ### Computational Complexity: This problem is computationally intensive due to the large number of combinations. It is not feasible to compute manually and would require: - Efficient poker hand evaluation code. - Distributed computing or GPU acceleration. - Optimization techniques such as memoization and pruning. Here is a simplified version of the code in Python: ```python from itertools import combinations import random # Simplified poker hand evaluator (placeholder) def evaluate_hand(hand): # This would be replaced with a full poker hand evaluator return random.randint(1, 1000000) # Generate a deck of 52 cards deck = list(range(52)) # Placeholder for counting winning hands winning_hands_count = 0 # Iterate over all possible 13-card hands (this is computationally infeasible to complete in full) for hand_A in combinations(deck, 13): remaining_cards = list(set(deck) - set(hand_A)) best_hand_A = max(combinations(hand_A, 5), key=evaluate_hand) # Check if best_hand_A beats all possible 5-card hands from the remaining 39 cards player_B_hands = combinations(remaining_cards, 5) if all(evaluate_hand(best_hand_A) > evaluate_hand(hand_B) for hand_B in player_B_hands): winning_hands_count += 1 print(f"Number of winning 13-card hands: {winning_hands_count}") ``` ### Conclusion: The exact number of such 13-card hands is not trivial to compute and would require a dedicated program with optimized algorithms. However, the framework for solving the problem involves evaluating all possible combinations and comparing poker hand strengths.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值