1056 Mice and Rice

题意:略

思路:利用queue来模拟一轮一轮的比赛。自己第一遍做的时候完全没有用queue做的意识,代码写的贼烦最后还只得了17分,非常郁闷。通过本题反映出对queue的应用场景季度不熟悉,STL里面用的最少的就是队列了。另外还有一点,在当前这一轮被淘汰的老鼠排名均为当前组数+1,这一点我也没看出来,自己做的时候拐了18个弯去实现这一点,真是惭愧!

代码:

#include <cstdio>
#include <queue>
using namespace std;

struct Mouse{
    int w;
    int r;
}mouse[1005];

int main()
{
    //freopen("pat.txt","r",stdin);
    int n,step;
    scanf("%d%d",&n,&step);
    int order;
    queue<int> q;
    for(int i=0;i<n;i++)
        scanf("%d",&mouse[i].w);
    for(int i=0;i<n;i++){
        scanf("%d",&order);
        q.push(order);
    }
    int temp=n,group;//temp为当前这一轮参加的老鼠个数,初始化为n;group为组数
    while(q.size()!=1){
        //计算这一轮的分组
        group=(temp%step==0 ? temp/step : temp/step+1);
        //遍历每一组,找出该组的老鼠质量的最大值
        for(int i=1;i<=group;i++){
            int maxIdx=q.front();//记录该组质量最大的下标
            for(int j=1;j<=step;j++){
          //用于判断最后一组不满step个的情况
if((i-1)*step+j>temp) break; if(mouse[q.front()].w > mouse[maxIdx].w) maxIdx=q.front(); mouse[q.front()].r=group+1;//关键,规律 q.pop(); } //maxIdx即这一组的胜利者,push进队列,进入下一轮的比赛 q.push(maxIdx); } temp=group;//下一轮参加比赛的老鼠个数就是这一轮的组数 } mouse[q.front()].r=1; printf("%d",mouse[0].r); for(int i=1;i<n;i++) printf(" %d",mouse[i].r); return 0; }

猜你喜欢

转载自www.cnblogs.com/kkmjy/p/9562478.html