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.
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).
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.
2 1 1
1
8 3 5
1 3 4
4 5 1
5 4 3 2 1
1 0
0 1
0 3
题意:
给定m条鱼 n只猫,第i 只猫 吃掉一只鱼需要花费c[i]分钟,所有的猫同时开始吃鱼,吃得快的猫,比吃得慢的猫 吃的鱼多。
求:最后剩下多少只完整的鱼 和多少只不完整的鱼。
思路: 用一个tim数组用来标记每个猫吃掉鱼剩余的时间,求出每个猫在规定时间能吃整个鱼的总数sum1, 再标记剩余时间tim[i]=x-((x/c[i])*c[i]); 如果sum1 >m p =0,q=0, 因为吃得快的猫,比吃得慢的猫 吃的鱼多。 所以c[]数组需要先排序,注意一个bug 当n = m 时 需考虑。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int tim[2100],c[110];
int main()
{
int m,n,x;
while(~scanf("%d%d%d",&m,&n,&x))
{
int p,q,sum1=0,sum2=0;
memset(tim,0,sizeof(tim));
for(int i=0; i<n; i++)
scanf("%d",&c[i]);
sort(c,c+n);
if(n==m)
{
int n2=0;
for(int i=0; i<n; i++)
{
if(c[i]>x)
n2++;
}
printf("0 %d\n",n2);
continue;
}
for(int i=0; i<n; i++)
{
sum1=sum1+(x/c[i]);
tim[c[i]]=x-((x/c[i])*c[i]);
}
if(sum1>=m)
{
p=0;
q=0;
}
else
{
int y=0;
sum2=m-sum1;
for(int i=0; i<n; i++)
{
if(tim[c[i]])
y++;
}
if(y<sum2)
{
q=y;
p=sum2-y;
}
else
{
p=0;
q=sum2;
}
}
printf("%d %d\n",p,q);
}
return