当前位置: 代码迷 >> 综合 >> #1631 : Cats and Fish
  详细解决方案

#1631 : Cats and Fish

热度:79   发布时间:2024-01-06 15:54:11.0

#1631 : Cats and Fish

时间限制: 1000ms
单点时限: 1000ms
内存限制: 256MB

描述

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

题目大意:多个case,每个case两行。第一行给m条鱼和n只猫还有时间x,第二行给出1到n只猫吃完一条鱼需要的时间。输出x分钟后剩余完整的鱼的数量和吃了一部分的鱼的数量。注意不能简单地统计x时总共能吃多少鱼,然后比较,因为有可能出现x时刻吃的最慢的猫还在吃,吃的快的猫早吃完很久了的情况

收到比赛的邮件就来做了一下,确实比pat甲级难很多。没想到还ac了一道。现在再回去做之前的题感觉好简单....


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;vector<int>nods[3001];//下标为时间点,x最大1000,吃的最慢的需要2000,所以取2000出头足够int m,n,x,cat[101],ans1,ans2;void ct(){int i,j,pro,comp=0,left=m;//pro用来实时统计正在吃的鱼,不包括刚吃完和要开始吃的、comp表示吃完的鱼、left表示剩下的 if(m>=n){//一开始所有的猫都有鱼吃 left=m-n;pro=n;for(i=1;i<=n;i++){nods[cat[i]].push_back(i);//在吃完的时间点那里标记一下是哪只猫吃完了 }}else{//鱼不够吃的情况 left=0;pro=m;for(i=1;i<=m;i++){nods[cat[i]].push_back(i);//因为调用之前排了序,所以从头开始分就好 }}for(i=0;i<=x;i++){//遍历时间,从0到x if(left>0){//还有鱼没吃 if(left>=nods[i].size()){//现在吃完鱼的猫都能吃到下一条 ans1=left;left-=nods[i].size();ans2=pro-nods[i].size();for(j=0;j<nods[i].size();j++){nods[i+cat[nods[i][j]]].push_back(nods[i][j]);}}else{//剩下的鱼不够吃了 ans1=left;pro-=nods[i].size()-left;ans2=pro-nods[i].size();sort(nods[i].begin(),nods[i].end());//不是所有的猫都能分到下一条鱼,先排序,按吃的速度分 for(j=0;j<left;j++){nods[i+cat[nods[i][j]]].push_back(nods[i][j]);}left=0;}}else{//没有鱼了 ans1=0;pro-=nods[i].size();if(pro<0) pro=0;ans2=pro;}}printf("%d %d\n",ans1,ans2);
}int main(){int i,j;while(scanf("%d %d %d",&m,&n,&x)!=EOF){for(i=0;i<=x;i++){//初始化 nods[i].clear();}for(i=1;i<=n;i++){scanf("%d",&cat[i]);}sort(cat+1,cat+n+1);//因为吃得快的猫会先抢到鱼,所以排一下序 ct();}return 0;
}