zoukankan      html  css  js  c++  java
  • 题解-UVA757 Gone Fishing

    好像还没有人写题解鸭,那我就来写(shui)一篇吧。

    求最多能钓到多少鱼,容易想到贪心。

    先把时间的单位换成时间片。

    因为不知道终点在哪所以我们可以枚举终点。

    枚举完之后花在路上的时间就确定了,直接减去即可。

    然后我们在所有湖能钓到的鱼中贪心选最大值,选完之后再把这个湖能钓到的鱼的数量减去(d[i]),再重复上述操作,知道时间为(0)

    我们可以通过优先队列来实现这个操作。

    注意到要尽量在编号小的湖里待的时间长,所以排序时如果相同则编号小的优先级高。

    这样就做完了。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    using namespace std;
    const int N = 30;
    struct node{
    	int val, id;
    	friend bool operator < (node a, node b) {
    		if (a.val == b.val) return a.id > b.id;
    		return a.val < b.val;
    	}
    };
    int n;
    int h;
    int f[N], d[N], t[N];
    priority_queue<node> q;
    int tmp, temp, ans, cnt;
    int tim[N], tim_tmp[N], cc;
    int main() {
    	while (scanf("%d", &n) != EOF && n) {
    		cc++;
    		if (cc > 1) puts("");
    		memset(tim, 0, sizeof(tim));
    		ans = -10000;
    		scanf("%d", &h);
    		h *= 12;
    		for (int i = 1; i <= n; i++) scanf("%d", &f[i]);
    		for (int i = 1; i <= n; i++) scanf("%d", &d[i]);
    		for (int i = 1; i < n; i++) {
    			scanf("%d", &t[i]);
    			t[i] += t[i - 1];
    		}
    		for (int i = 1; i <= n; i++) {//枚举终点 
    			while (!q.empty()) q.pop();
    			memset(tim_tmp, 0, sizeof(tim_tmp));
    			cnt = 0;
    			tmp = h;
    			tmp -= t[i - 1];
    			for (int j = 1; j <= i; j++) {
    				q.push(node{f[j], j});
    			}
    			while (tmp > 0) {
    				node cur = q.top();
    				q.pop();
    				if (cur.val <= 0) break;
    				cnt += cur.val;
    				tim_tmp[cur.id]++;
    				q.push(node{max(0, cur.val - d[cur.id]), cur.id});
    				tmp--;
    			}
    			if (tmp) tim_tmp[1] += tmp;
    			if (cnt > ans) {
    				ans = cnt;
    				for (int j = 1; j <= n; j++) {
    					tim[j] = tim_tmp[j];
    				}
    			}
    		}
    		printf("%d", tim[1] * 5);
    		for (int i = 2; i <= n; i++) {
    			printf(", %d", tim[i] * 5);
    		} printf("
    ");
    		printf("Number of fish expected: %d
    ", ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    二叉树非递归遍历
    二叉树之统计二叉树的节点个数
    C语言32个关键字(2)
    C语言32个关键字(1)
    C语言常用字符串操作函数总结
    面向对象的四大特征
    C语言之生产者与消费者模型
    菜鸟随笔(4)---read函数与fread函数的区别
    菜鸟随笔(3)---三种进程学习.孤儿进程.僵尸进程.守护进程
    进程通信——管道、消息队列、共享内存、信号量
  • 原文地址:https://www.cnblogs.com/zcr-blog/p/14901194.html
Copyright © 2011-2022 走看看