题解 | iCow-牛客假日团队赛8B题

B-iCow_牛客假日团队赛8

https://ac.nowcoder.com/acm/contest/1069/B

题目描述

Fatigued by the endless toils of farming, Farmer John has decided to try his hand in the MP3 player market with the new iCow. It is an MP3 player that stores N songs indexed 1 through N that plays songs in a "shuffled" order, as determined by Farmer John's own algorithm: * Each song i has an initial rating . * The next song to be played is always the one with the highest rating (or, if two or more are tied, the highest rated song with the lowest index is chosen). * After being played, a song's rating is set to zero, and its rating points are distributed evenly among the other N-1 songs. * If the rating points cannot be distributed evenly (i.e., they are not divisible by N-1), then the extra points are parceled out one at a time to the first songs on the list (i.e., R1, R2, etc. -- but not the played song) until no more extra points remain. This process is repeated with the new ratings after the next song is played. Determine the first T songs that are played by the iCow.

输入描述:

* Line 1: Two space-separated integers: N and T
* Lines 2..N+1: Line i+1 contains a single integer:

输出描述:

* Lines 1..T: Line i contains a single integer that is the i-th song that the iCow plays.

示例1

输入
3 4
10
8
11
输出
3
1
2
3
说明
The iCow contains 3 songs, with ratings 10, 8, and 11, respectively. You must determine the first 4 songs to be played.
The ratings before each song played are:
R1 R2 R3
10 8 11 -> play #3 11/2 = 5, leftover = 1
16 13 0 -> play #1 16/2 = 8
0 21 8 -> play #2 21/2 = 10, leftover = 1
11 0 18 -> play #3 ...

解答

算法标签纯模拟,其实有很多细节需要注意,主要是控制余数;
下面讲一下主要思路:
1.找最大;
2.输出序号,并将权值按算法分配给其他歌曲;
3.重复1,直到输出个数==t。
代码如下:
#include<iostream> 
#include<cstdio> 
#include<cmath> 
#include<algorithm> 
#include<cstring>
using namespace std;
int main()
{
    int n,t,i,max=0,a[1001],ti=1,p,p1,p2,j;//ti控制输出个数,p1为余数,p2为商  
    scanf("%d%d",&n,&t);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(max<a[i]) max=a[i];//找最大,注意max初始化为0 
    }
    if(n==1)//题目的特殊情况 
    {
        for(i=1;i<=t;i++)
        printf("1\n");
        return 0;
    }
    while(ti<=t)
    {
        for(i=1;i<=n;i++)
        if(a[i]==max) break;//找最大 
        printf("%d\n",i);
        max=0;//注意清零 
        p1=a[i]%(n-1);
        p2=a[i]/(n-1);
        a[i]=0;//权值清零 
        if(p1==0)//可平均分配 
        {
            for(j=1;j<=n;j++)
            {
                if(j!=i)//播放的歌曲会被跳过 
                {
                    a[j]+=p2;
                    if(max<a[j]) max=a[j];
                }
            }
            ti++;
        }
        else//不可平均分配 
        {
            for(j=1;j<=n;j++)
            {
                if(j!=i)
                {
                    a[j]+=p2;
                }
            }
            for(j=1;j<=p1;j++)
            {
                if(j!=i)
                {
                    a[j]++;//以1为单位输送给每个歌曲 
                }
                else p1++;//这里是为了控制i==1,p1==1之类情况 
            }
            for(j=1;j<=n;j++)
            if(max<a[j]) max=a[j];
            ti++;
        }
    }
    return 0;
}

来源:dingxingdi
全部评论

相关推荐

点赞 评论 收藏
分享
AI牛可乐:哇,听起来你遇到了什么挑战呢!🐮牛可乐在这里,虽然小,但是勇敢又聪明,想听听你的具体情况哦!如果你愿意的话,可以点击我的头像给我私信,我们可以一起想办法应对挑战,好不好呀?🌟🎉
点赞 评论 收藏
分享
11-08 13:58
门头沟学院 Java
程序员小白条:竟然是蓝桥杯人才doge,还要花钱申领的offer,这么好的公司哪里去找
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务