PAT (Advanced) 1056. Mice and Rice (25)

原题:1056. Mice and Rice (25)



解题思路:

题目对第三行数据的描述略坑。第三行的数实际上本身就是出场次序,不如第一个数为6,就代表6号老鼠第一个出场。

实际上就是循环找出每组最大的,直到只剩下一个老鼠。需要设计几个比较函数进行排序。


代码如下:

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 1000 + 5;

struct Mouse
{
    int id;
    int order;
    int weight;
    int round;
    int Rank;
} mouse[maxn];

bool cmp1(Mouse a, Mouse b)
{
    if(a.round != b.round) return a.round < b.round;
    return a.order < b.order;
}

bool cmp2(Mouse a, Mouse b)
{
    if(a.round != b.round) return a.round > b.round;
    return a.order < b.order;
}

bool cmp3(Mouse a, Mouse b)
{
    return a.id < b.id;
}
int main()
{
    int ng, np;
    while(scanf("%d%d", &np, &ng) == 2)
    {
        for(int i = 0; i < np; i++)
        {
            scanf("%d", &mouse[i].weight);
            mouse[i].id = i;
            mouse[i].round = 0;
        }
        for(int i = 0; i < np; i++)
        {
            int order;
            scanf("%d", &order);
            mouse[order].order = i;
        }
        int rnd = 1;
        for(;;)
        {
            sort(mouse, mouse+np, cmp1);
            int cnt = 0;
            for(int i = 0; i < np && mouse[i].round == 0;)
            {
                int maxi = -1, maxw = -1;
                for(int j = i; j < i+ng && j < np && mouse[j].round == 0; j++)
                    if(mouse[j].weight > maxw)
                    {
                        maxi = j;
                        maxw = mouse[j].weight;
                    }
                for(int j = i; j < i+ng && j < np && mouse[j].round == 0; j++)
                    if(j != maxi) {mouse[j].round = rnd;cnt++;}

                i += ng;
            }
            rnd++;
            if(cnt == 0) //只剩一只了
            {
                mouse[0].round = rnd;
                break;
            }
        }
        //决定名次
        sort(mouse, mouse+np, cmp2);
        mouse[0].Rank = 1;
        for(int i = 1; i < np; i++)
            if(mouse[i].round == mouse[i-1].round) mouse[i].Rank = mouse[i-1].Rank;
            else mouse[i].Rank = i + 1;
        //输出
        sort(mouse, mouse+np, cmp3);
        printf("%d", mouse[0].Rank);
        for(int i = 1; i < np; i++)
            printf(" %d", mouse[i].Rank);
        printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值