iCow
Description 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 (1 ≤ N ≤ 1,000) indexed 1 through N that plays songs in a "shuffled" order, as determined by Farmer John's own algorithm:
Determine the first T songs (1 ≤ T ≤ 1000) that are played by the iCow. Input * Line 1: Two space-separated integers: N and T Output * Lines 1..T: Line i contains a single integer that is the i-th song that the iCow plays. Sample Input 3 4 10 8 11 Sample Output 3 1 2 3 Source |
题目题意:
有一款MP3,叫iCow,里面存储了N(1<=N<=1000)首歌曲,随机播放顺序由以下决定。
• 每首歌i有一个初始的Ri(1<=Ri<=10000)• 下首歌总是最高的Ri(如果Ri==Rj && i<j,则选i)
• 一首歌i播放之后,这首歌的Ri=0,并且将这首歌原来的Ri分给其他N-1首歌
• 若Ri不能被N-1整除,则多出的部分Ri%(N-1)将从第1首歌开始一首歌1点(播放的这首歌除外),直到没有多余的点数
每首歌播放完毕都执行以上算法更新Ri
请决定先被播放的T(1<=T<=1000)首歌曲。
输出T行,代表每次播放哪首歌。
解题思路:纯模拟
#include<iostream>
#include<cstdio>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<climits>
#include<cmath>
#include<vector>
#include <bitset>
#include<algorithm>
#include <queue>
#include<map>
using namespace std;
int n, T, i, j, ans, k;
struct song
{
int i;
int ans;
}s[1005];
bool cmp(song x, song y)
{
if (x.ans == y.ans)
{
return x.i < y.i;
}
else
{
return x.ans > y.ans;
}
}
bool cmp1(song x, song y)
{
return x.i < y.i;
}
int main()
{
cin >> n >> T;
for (i = 1; i <= n; i++)
{
cin >> s[i].ans;
s[i].i=i;
}
while (T--)
{
sort(s + 1, s + 1 + n, cmp);
ans = s[1].ans;
k = s[1].i;
cout << k << endl;
s[1].ans = 0;
sort(s + 1, s + 1 + n, cmp1);
j = ans / (n - 1);
ans %= n - 1;
for (i = 1; i <= n; i++)
{
if (i != k)
{
s[i].ans += j;
}
if (ans != 0&&i!=k)
{
s[i].ans++;
ans--;
}
}
}
}