zju 1161 Gone Fishing

这个题是个经典的贪心题。
枚举走过的湖的个数x,最终将在湖x停下来,那么在走路上消耗的时间可以算出来t[1]+t[2]+...+t[n-1],在这个前提下,我们可以从一个湖瞬移到另外一个湖(这个是关键!!!)。为了掉的更多的鱼,我们每次都选择瞬移到与最多的湖去钓5分钟(可以通过优先队列来实现),这样得到的鱼肯定是最多的。

我做这个题一开始WA了N次,有下面2点必须要注意:
1)对于进入优先队列的比较操作,除了要比较湖中鱼的多少外,当鱼的数量相同时,要优先选择靠前的湖。
2)要考虑到有剩余时间不能用的情况,即鱼都钓完了,但是还有时间,这是要将剩余的时间都加到1号湖中。
3)最优时间的初始值不要设为0,因为最优值可能是0,这样的话最优钓鱼的方案得不到更新。

my code:
#include <iostream>
#include 
<queue>
using namespace std;

struct node {
    
int id;
    
int fn;
    node() 
{}
    node(
int I,int F):id(I),fn(F) {}
}
;
bool operator<(const node& n1,const node& n2) {
    
if(n1.fn==n2.fn) return n1.id>n2.id;
    
return n1.fn<n2.fn;
}


priority_queue
<node> q;
int n,h,maxfish,small;
int f[26],d[26],t[26],cnt[26],cntt[26];

int main() {
    
int tt,ca=0;
    cin
>>tt;
    
while(tt--{
        
while(cin>>n) {
            
if(n==0break;
            cin
>>h;
            h
=h*60;
            
for(int i=1;i<=n;i++) cin>>f[i];
            
for(int i=1;i<=n;i++) cin>>d[i];
            
for(int i=1;i<n;i++) cin>>t[i];
    
            maxfish
=-1;
            
for(int stop=1;stop<=n;stop++{
                memset(cntt,
0,sizeof cntt);
                
while(!q.empty()) q.pop();
                
int spendonway=0;
                
for(int i=1;i<stop;i++) spendonway+=5*t[i];
                
for(int i=1;i<=stop;i++{
                    
if(f[i]>0)
                        q.push(node(i,f[i]));
                }

                
int timetofish=h-spendonway;
                
int totalfish=0;
                
if(timetofish<5continue;
                
while(timetofish>=5&&!q.empty()) {
                    node nt
=q.top();
                    q.pop();
                    timetofish
-=5;
                    totalfish
+=nt.fn;
                    cntt[nt.id]
+=5;
                    
if(nt.fn>d[nt.id])
                        q.push(node(nt.id,nt.fn
-d[nt.id]));
                }
    
                
if(timetofish>0) cntt[1]+=timetofish;
                
if(maxfish<totalfish) {
                    maxfish
=totalfish;
                    memcpy(cnt,cntt,
sizeof cntt);
                }

                
if(maxfish==totalfish) {
                    
for(int i=1;i<=n;i++{
                        
if(cntt[i]==cnt[i]) continue;
                        
if(cntt[i]>cnt[i]) {small=0;break;}
                        
if(cntt[i]<cnt[i]) {small=1;break;}
                    }

                    
if(small==0{
                        memcpy(cnt,cntt,
sizeof cntt);
                    }

                }
    
            }

            
if(ca++>0) cout<<endl;
            cout
<<cnt[1];
            
for(int i=2;i<=n;i++) cout<<""<<cnt[i];
            cout
<<endl;
            cout
<<"Number of fish expected: "<<maxfish<<endl;
        }

    }

    
return 0;
}



            

posted on 2007-07-26 21:26  woodfish  阅读(534)  评论(1编辑  收藏  举报

导航