1. 程式人生 > >ACM-ICPC北京賽區2017網路同步賽 題目5 : Cats and Fish【模擬】

ACM-ICPC北京賽區2017網路同步賽 題目5 : 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
題意: 給你m個魚,n只貓,然後x分鐘,再給出每隻貓吃一條魚所用的時間,當有一條魚時,吃魚速度快的搶到魚的概率大於吃魚慢的(簡單來說就是誰吃的快是誰的),讓你求x分鐘後還剩多少條完整的魚,和不完整的魚

分析: 開始看到這個題還以為是貪心,然後就wa了,後來想了想還是得模擬呀,直接根據時間來模擬即可,先是根據速度排個升序,然後模擬就行,新增一個vis陣列來記錄狀態,當某條魚空閒時,且還有魚的時候,魚的總數就少一條,當吃魚的速度為1的時候要特判下,仔細分析好就行

參考程式碼

#include<bits/stdc++.h>

using namespace std;
const int N = 1e5 + 10;
int a[N];

int main(){
    ios_base::sync_with_stdio(0);
    int p,n,x;
    while (cin>>p>>n>>x) {
        for(int i = 0;i < n;i++)
            cin>>a[i];
        sort(a,a+n);
        int s = 1;
        int q = 0;
        bool vis[N];
        memset(vis,false,sizeof(vis));
        for(int i = 1;i <= x;i++) {
            for(int j = 0;j < n;j++) {
                if(p == 0) break;
                if(i%a[j] == 0) {
                    a[j] == 1?p--:p;
                    if(vis[j]) vis[j] = false,q--;
                } else {
                    if(!vis[j]) {
                        p--;
                        q++;
                        vis[j] = true;
                    }
                }
                if(p == 0 && q == 0) break;
            }
            if(p == 0 && q == 0) break;
        }
        cout<<p<<' '<<q<<endl;
    }
    return 0;
}
  • 如有錯誤或遺漏,請私聊下UP,thx