poj 1042Gone Fishing

贪心算法+枚举

枚举最远到达那个湖i,先把总时间减去走到湖i的时间,剩下的可以看做在i个湖里每次拿一个最大的,用了一个自定义的堆结构

#include<iostream>

#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct node{
int num;
int loca;
node(int nn = 0,int ll = 0){
num = nn;
loca = ll;
}
};
bool operator<(node a,node b){
if(a.num==b.num)return a.loca>b.loca;
return a.num<b.num;
}
const int MAXN = 30;
int h;
int n;
int d[MAXN];
int f[MAXN];
int ans_n[MAXN];
int ans_sum;
int T[MAXN];
int main(){
T[0] = 0;

while(scanf("%d",&n),n){

scanf("%d",&h);
h = h*60;
for(int i = 0;i<n;i++)scanf("%d",&f[i]);
for(int i = 0;i<n;i++)scanf("%d",&d[i]);
for(int i = 1;i<n;i++)scanf("%d",&T[i]),T[i] = T[i]+T[i-1];
int hh = h;
int left = f[0];
ans_sum = 0;
memset(ans_n,0,sizeof(ans_n));
while(left>0 && hh>0){
ans_sum+=left;
left-=d[0];
hh-=5;
ans_n[0]+=5;
}
if(hh>0)ans_n[0]+=hh;
int cop_n[MAXN];
int sum = 0;
for(int i = 1;i<n;i++){
hh = h;
sum = 0;
memset(cop_n,0,sizeof(cop_n));
priority_queue<node> qe;
for(int j = 0;j<=i;j++){
if(f[j]>0)qe.push(node(f[j],j));
}
hh-=5*T[i];
node a;
while(hh>0 && !qe.empty()){
a = qe.top();
qe.pop();
sum+=a.num;
hh-=5;
cop_n[a.loca]+=5;
a.num = a.num-d[a.loca];
if(a.num>0)qe.push(a);
}
if(hh>0)cop_n[0]+=hh;
if(sum>ans_sum){
ans_sum = sum;
memcpy(ans_n,cop_n,MAXN*sizeof(int));
}else if(sum==ans_sum){
bool ok = false;
for(int j = 0;j<=i;j++)if(cop_n[j]>ans_n[j]){
ok = true;
break;
}else if(cop_n[j]<ans_n[j]){
break;
}else;
if(ok)memcpy(ans_n,cop_n,MAXN*sizeof(int));
}else;
}
printf("%d",ans_n[0]);
for(int i = 1;i<n;i++)printf(", %d",ans_n[i]);
printf("\nNumber of fish expected: %d\n\n",ans_sum);
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/sysucph/article/details/8079776
今日推荐