CSU 1859 Gone Fishing(贪心)

Gone Fishing

【题目链接】Gone Fishing

【题目类型】贪心

&题解:

这题要先想到枚举走过的湖,之后才可以贪心,我就没想到这,就不知道怎么贪心 = =
之后在枚举每个湖的鱼的个数,之后总是选最大的就好了,这里我是直接变的f数组,所以最后一定不要忘了在赋值回来

【时间复杂度】(O(n^2k))

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxn= 30 +9;
int n,h,f[maxn],K,ff[maxn],d[maxn],t[maxn];
vector<int> v1,v2;
int main()
{
//	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	freopen("E:1.txt","r",stdin);
	while(cin>>n){
		if(K!=0) cout<<endl; K++;
		if(n==0)break;
		cin>>h; h*=12;
		for(int i=0;i<n;i++) cin>>f[i],ff[i]=f[i];
		for(int i=0;i<n;i++) cin>>d[i];
		for(int i=1;i<n;i++) {cin>>t[i];t[i]+=t[i-1];}
		int ans=0;
		for(int i=0;i<n;i++) {
			v2.clear(); v2.resize(n);
			int tm=h-t[i];
//			cout<<"tm="<<tm<<endl;
			if(tm<0) break;
			int a1=0;
			for(int i=0;i<n;i++) f[i]=ff[i];
			for(int o=0;o<tm;o++){
				int ma=0,ps=0;
				for(int j=0;j<=i;j++){
					if(ma<f[j]){
						ma=f[j];
						ps=j;
					}
				}
//				if(tm==10){
//					cout<<"ps="<<ps<<" f[ps]="<<f[ps]<<endl;
//				}
				a1+=f[ps];
				v2[ps]++;
				f[ps]-=d[ps];
				if(f[ps]<0) f[ps]=0;
			}
			if(ans<a1){
				v1=v2;
				ans=a1;
			}
		}
		for(int i=0;i<n;i++){
			cout<<v1[i]*5;
			if(i!=n-1)cout<<", ";
		}cout<<endl;
		cout<<"Number of fish expected: "<<ans<<endl;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/s1124yy/p/6664209.html