There are many homeless cats in PKU campus. They are all happy because the students in the cat club of PKU take good care of them. Li lei is one of the members of the cat club. He loves those cats very much. Last week, he won a scholarship and he wanted to share his pleasure with cats. So he bought some really tasty fish to feed them, and watched them eating with great pleasure. At the same time, he found an interesting question:
There are m fish and n cats, and it takes ci minutes for the ith cat to eat out one fish. A cat starts to eat another fish (if it can get one) immediately after it has finished one fish. A cat never shares its fish with other cats. When there are not enough fish left, the cat which eats quicker has higher priority to get a fish than the cat which eats slower. All cats start eating at the same time. Li Lei wanted to know, after x minutes, how many fish would be left.
Input
There are no more than 20 test cases.
For each test case:
The first line contains 3 integers: above mentioned m, n and x (0 < m <= 5000, 1 <= n <= 100, 0 <= x <= 1000).
The second line contains n integers c1,c2 … cn, ci means that it takes the ith cat ci minutes to eat out a fish ( 1<= ci <= 2000).
Output
For each test case, print 2 integers p and q, meaning that there are p complete fish(whole fish) and q incomplete fish left after x minutes.
Sample Input
2 1 1
1
8 3 5
1 3 4
4 5 1
5 4 3 2 1
Sample Output
1 0
0 1
0 3
【分析】: 利用一个数组存每个猫的进食速度,用sort排序,升序排列(因为题目中中说明了进食速度快点猫优先分配到鱼),用另外一个数组存当前猫的状态,1为在吃鱼,0为空闲,
对于每一分钟,遍历每一个猫,如果当前猫为空闲,就给他一条鱼,如果当前时间与猫的速度取余==0;则当前猫置为空闲,
最后遍历数组寻找为1 的数量,则为不完整鱼的数量,
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[105],b[105];
int main()
{
int n,m,x;
while(~scanf("%d%d%d",&m,&n,&x))
{
int cfish=m,infish=0;
int time;
int flag=0;
memset(a,0,sizeof(a));
memset(b,1,sizeof(b)); //0空闲 1正在吃
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
if(m<=n)
{
for(int i=0;i<m;i++){
if(x<a[i]){
infish++;
}
}
printf("0 %d\n",infish);
}
else //一开始,每条猫都有一条鱼.
{
m-=n;
for(int j=1;j<=x; j++)
{
for(int i=0;i<n;i++){
if(b[i]==0){
m--;
}
if(j%a[i]==0){
b[i]=0;
}
else b[i]=1;
if(m==0){
break;
}
}
if(m==0){
break;
}
}
int ans=0;
for(int i=0;i<n;i++){
if(b[i]){
ans++;
}
}
printf("%d %d\n",m,ans);
}
}
}