POJ 1042 go fishing(贪心)

Decsription:

有几个钓鱼池分布在一条直线上,编号从1到n,一个人从1出发,可以选择走到不同的位置钓鱼,但是只能往右边走而不能回头而且人走到下一个鱼池需要消耗一定的时间,对于第i个鱼池,在该鱼池在初始时间掉一次5分钟的鱼可以得到fi条鱼,每过5分钟在该鱼池掉得的鱼数减少di,问当结束时(无鱼可掉,或者在限制的h时间结束时),最多可以调到多少鱼。

Input:

h, n , fi , di.

Output:

具体的钓鱼方案

Analysis:

这面时间分为两部分,走路的时间和钓鱼的时间,由于只能往右走不能回头,可以枚举最后停止的位置,这样走路所占用的总时间同时得到,那么只要找最多的钓鱼方案就行了,直接贪心就好,这里面的值得注意的地方在于枚举停止位置,同时得到走路总时间,这种枚举一部分信息在算法里很常见,靠部分枚举得到更多的信息扩充已知条件。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<sstream>
#include<cmath>
#include<iterator>
#include<bitset>
#include<stdio.h>
#include<time.h>
using namespace std;
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define _rep(i,a,b) for(int i=(a);i<=(b);++i)
typedef long long LL;
const int INF = 0xffffff0;
const int MOD = 1e9 + 7;
const int maxn = 30;

int n,h;
int f[maxn], d[maxn], t[maxn];
int tot[maxn];
struct Node {
	int f;
	int id;
	Node(int f,int id):f(f),id(id){}
	bool operator < (const Node & rhs)const {
		return f < rhs.f||(f==rhs.f&&id>rhs.id);
	}
};
priority_queue<Node> Q;

bool cmp(int a[], int b[]) {
	for (int i = 0; i < n; ++i) {
		if (a[i] != b[i])return a[i] > b[i];
	}
	return false;
}

int main() {
	freopen("C:\\Users\\admin\\Desktop\\in.txt", "r", stdin);
	freopen("C:\\Users\\admin\\Desktop\\out.txt", "w", stdout);
	int blank = 0;
	while (cin >> n && n) {
	
		if (blank)cout << endl;
		blank = 1;

		cin >> h; h *= 60 / 5;
		_for(i, 0, n)cin >> f[i];
		_for(i, 0, n)cin >> d[i];
		_for(i, 0, n-1)cin >> t[i];

		int ans = 0;
		memset(tot, 0, sizeof(tot));

		int hh = 0;
		_for(i, 0, n) {
			if (i > 0) hh+= t[i - 1];
			int tot1[maxn];
			memset(tot1, 0, sizeof(tot1));
			while (Q.size())Q.pop();
			for (int j = 0; j <=i; ++j)
				Q.push(Node(f[j], j));
			int ans1 = 0,h1=0;
			while (Q.size()) {
				Node now = Q.top();
				Q.pop();
				if (h1+hh>=h)break;
				h1++;tot1[now.id]++;
				ans1 += now.f;
				now.f =max(now.f-d[now.id],0);
				Q.push(now);
			}
			if (ans1>ans||(ans1==ans&&cmp(tot1,tot))) {
				ans = ans1;
				memcpy(tot, tot1, sizeof(tot));
			}
		}
		_for(i, 0, n) {
			if (i)cout << ", ";
			cout << tot[i]*5;
		}
		cout << endl << "Number of fish expected: " << ans << endl;
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/tomandjake_/article/details/81384629
今日推荐