hihoCoder-1631

题意:

描述

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只猫,每只猫吃鱼的速度为c[i]。问x秒后有多少条鱼没被吃,多少条鱼没吃完?(同一秒吃的快的猫先拿鱼吃)

思路:

​ 用两个数m代表剩余鱼数,mm代表n-完全吃光的鱼数,两数都初始化为n。先把猫吃鱼的速度排个序,开一个book数组记录一下每个猫的状态(0时代表需要拿一个鱼,1时代表正在吃鱼),我们初始化为0。然后遍历x秒,每秒从快->慢的猫进行遍历。若状态为0则m–,并把状态更改为1。若时间正好时该鱼速度的倍数,那么mm–,然后把状态更新为0。最后注意当m为0的时候跳出,再注意一下特判就可以啦。

代码:

#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <algorithm>
#define d int32_t
#define ll int64_t
#define r return
#define N 100000
#define For(i, star, endd) for(d i = star; i <= endd; i++)
#define mem(a) memset(a, 0, sizeof(a))
using namespace std;

d a[105];
d book[105];

d main() {
    d m, n, x;
    while(scanf("%d%d%d", &m, &n, &x) == 3) {
        d mm = m;
        mem(book);
        For(i, 1, n) {
            scanf("%d", &a[i]);
        }
        sort(a + 1, a + n + 1);
        For(i, 1, x) {
            d flag = 0;
            For(j, 1, n) {
                if (!book[j]) {
                    book[j] = 1;
                    m--;
                    if(m == 0) {
                        if(i % a[j] == 0) {		//需要特判m为0的时候是否i%a[j] == 0
                            mm --;
                            book[j] = 0;
                        }
                        flag = 1;
                        break;
                    }
                }
                if(i % a[j] == 0) {
                    mm --;
                    book[j] = 0;
                }
            }
            if(flag) {
                break;
            }
        }
        printf("%d %d\n", m, mm - m);
    }
    r 0;
}

转载请注明出处!!!

如果有写的不对或者不全面的地方 可通过主页的联系方式进行指正,谢谢

猜你喜欢

转载自blog.csdn.net/Ivan_zcy/article/details/83119090
今日推荐