队列之前学的。忘得差不多了。
cf这题重新写
http://codeforces.com/contest/450/problem/A
There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let's number all the children from 1 to n. The i-th child wants to get at least ai candies.
Jzzhu asks children to line up. Initially, the i-th child stands at the i-th place of the line. Then Jzzhu start distribution of the candies. He follows the algorithm:
- Give m candies to the first child of the line.
- If this child still haven't got enough candies, then the child goes to the end of the line, else the child go home.
- Repeat the first two steps while the line is not empty.
Consider all the children in the order they go home. Jzzhu wants to know, which child will be the last in this order?
The first line contains two integers n, m (1 ≤ n ≤ 100; 1 ≤ m ≤ 100). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100).
Output a single integer, representing the number of the last child.
5 2 1 3 1 4 2
4
6 4 1 1 2 2 3 3
6
Let's consider the first sample.
Firstly child 1 gets 2 candies and go home. Then child 2 gets 2 candies and go to the end of the line. Currently the line looks like [3, 4, 5, 2] (indices of the children in order of the line). Then child 3 gets 2 candies and go home, and then child 4 gets 2 candies and goes to the end of the line. Currently the line looks like [5, 2, 4]. Then child 5 gets 2 candies and goes home. Then child 2 gets two candies and goes home, and finally child 4 gets 2 candies and goes home.
Child 4 is the last one who goes home.
意思是有n个小孩,头个小孩给m个糖果,如果不满足,则到队列最后。
最后一个是编号多少?
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<deque>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<numeric>
#include<iomanip>
#include<bitset>
#include<sstream>
#include<fstream>
#define debug puts("-----")
#define pi (acos(-1.0))
#define eps (1e-8)
#define inf (1<<30)
using namespace std;
struct node
{
int i,x;
};
int main()
{
int n,m;
queue<node> q;
cin>>n>>m;
for (int i=1; i<=n; i++)
{
int x;
cin>>x;
node a;
a.i=i;
a.x=x;
q.push(a);//每个成员压入队中
}
node a;
while(!q.empty())//队列非空
{
a=q.front();//提取每个队首
q.pop();//抛弃队首
a.x-=m;//分糖果
if (a.x>0)
q.push(a);//如果不满足,放到队列末尾
}
cout<<a.i<<endl;
return 0;
}
自己写的队列模拟,写的很乱
#include<iostream>
#include<queue>
using namespace std;
struct stu
{
int num,key;
}a[1100];
int main()
{
int n,f,kk=0;
cin>>n>>f;
for(int i=1;i<=n;i++)
{
cin>>a[i].key;
a[i].num=i;
}
if(n>1)
for(;;)
{
a[1].key-=f;
int k,nn;
k=a[1].key;
nn=a[1].num;
if(a[1].key>0)
{
for(int i=1;i<n;i++)
{
a[i].key=a[i+1].key;
a[i].num=a[i+1].num;
}
a[n].key=k;
a[n].num=nn;
}
if(a[1].key<=0)
{
for(int i=1;i<n;i++)
{
a[i].key=a[i+1].key;
a[i].num=a[i+1].num;
}
n=n-1;
}
if(n==1)
break;
}
cout<<a[1].num<<endl;
}