HihoCoder - 1631 Cats and Fish

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37129433/article/details/83065616

Cats and Fish
题 意:有m个鱼干,n只猫,和时间x,每只猫都有一个吃鱼干的速度,吃的快的猫,在鱼干不足的情况下,可以先吃,问x分钟后,有多少完整的鱼剩下,有多少不完整的鱼剩下。
题面是说,鱼吃完这个鱼干之后立马能吃下一个,样例1说并不能立马吃下一个。
数据范围:
0 < m < = 5 e 3 0<m<=5e3
1 < = n < = 1 e 2 1<=n<=1e2
0 < = x < = 1 e 3 0<=x<=1e3
输入样例:

2 1 1
1
8 3 5
1 3 4
4 5 1
5 4 3 2 1
1 0
0 1
0 3

思 路: 这题很简单啊,看这数据范围,题意,直接模拟就好了。用优先队列模拟,用优先队列维护node{可以开始吃鱼的时间,和这只猫吃一个鱼干所需要的时间}。优先取出可以开始吃鱼时间少的猫,如果一样,那么选吃的快的。然后就是细节方面的维护了,具体看代码。

模拟题:(lll¬ω¬)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<cmath>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
#define IN freopen("input.txt","r",stdin)
#define mst(x,y) memset(x,y,sizeof(x));
#define debug(x) cout<< #x <<" = "<< (x) <<endl;
#define min(x,y) x>y?y:x
#define max(x,y) x>y?x:y
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef unsigned long long ull;
const int mod = 1e6+3;
const int INF = 0x3f3f3f3f;
const int LINF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1e3+5;
struct node {
    int first,second;
    node(int First=0,int Second=0):first(First),second(Second) {}
    bool operator<(const node & _p)const {
        if(first == _p.first) return second>_p.second;  //吃的快的
        return first > _p.first; //取出时间少的
    }
};
priority_queue<node> que;
int m,n,x;
int a[maxn];
int main() {
    //IN;
    while(~scanf("%d %d %d",&m,&n,&x)) {
        while(!que.empty())que.pop();
        for(int i=1; i<=n; i++) {
            scanf("%d",&a[i]);
            que.push(node(0,a[i]));
        }
        int ans1 = m,ans2 = 0;
        if(x == 0){
            printf("%d 0\n",m);
            continue;
        }
        while(!que.empty()) {
            node p =que.top();  //不能直接pop掉,可能当前这个已经不能吃了,那么我们就要放到外面取判断,而不是直接pop掉
            if(ans1>0) {
                if(p.first<=x-1) {   //不能立马吃下一个
                    que.pop();
                    que.push(node(p.first+p.second,p.second));
                    --ans1;
                }else{  //已经全部都不能吃了。
                    break;
                }
            } else {
                break;
            }
        }
        while(!que.empty()) {
            node p = que.top();
            que.pop();
            if(p.first>x)ans2++;
        }
        printf("%d %d\n",ans1,ans2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37129433/article/details/83065616
今日推荐