二分与贪心-Gone Fishing(算法基础 第9周)

问题描述:
二分与贪心-Gone Fishing(算法基础 第9周)_第1张图片
分析
共n个湖,h个小时的时间;每个湖首次可钓到fi条鱼,往后一次递减di条,从i湖走到i+1湖需ti×5分钟。问:指定时间内最多可钓多少条鱼。
参考代码;http://blog.csdn.net/lyhvoyage/article/details/23289531 。按他的方式,第一种方法测试超时,第二种使用优先队列方法可以通过。但自己写了两个,即使使用优先队列也会超时,呃,,郁闷,,代码先放这儿,以后再调。
源码
//代码1,不使用优先队列,超时

#include   
#include 
#include 
using namespace std;  

int n, h;
vector<int> fi(25), di(25), ti(25);
int ft[25];
//在0~k之间钓鱼
int index_maxfi(const int& k,const vector<int>& kfi) {
    int ind=0;
    int maxfi=kfi[0];
    for(int i=1; i<=k; i++) {
        if (maxfireturn ind;
}


//在0~k之间钓鱼
int fish(const int& k, vector<int> kfi) {
    int nfishes=0;
    int rest_h=h;
    int ind;
    while(rest_h) {
        ind = index_maxfi(k, kfi);
        if (kfi[ind]==0) {
            ft[0] += rest_h;
            break;
        }
        nfishes += kfi[ind];
        ft[ind]++;
        if (kfi[ind]>di[ind])
            kfi[ind] -= di[ind];
        else
            kfi[ind] = 0;
        rest_h--;
    }
    return nfishes;
}

int main() {
    while(cin>>n && n>0) {
        cin >> h;
        h *= 12;
        for(int i=0; icin >> fi[i];
        } 
        for(int i=0; icin >> di[i];
        } 
        for(int i=0; i1; i++) {
            cin >> ti[i];
        } 

        int maxfishes=0;
        vector<int> fishtime(n, 0);
        for (int i=0; iif (i>0){
                h -= ti[i-1];
            }
            memset(ft, 0, sizeof(ft));

            int tempf=fish(i, fi);
            if (maxfishes < tempf) {
                maxfishes = tempf;
                for (int j=0; j<=i; j++){
                    fishtime[j] = ft[j]*5;
                }
            }           
        }
        cout << fishtime[0];
        for(int i=1; icout << ", " << fishtime[i];
        cout << endl;
        cout << "Number of fish expected: " << maxfishes << endl;
        cout << endl;
    }
}

//代码2,使用优先队列,还是超时,日后再看

#include   
#include 
#include 
#include 
#include 
#include 
using namespace std;  

int n, h;
int ft[25];
struct Fish 
{
    int i; //ith个鱼塘
    int f;
    int d;
    int t;
    Fish():i(99),f(0),d(0),t(0) {}
};
bool operator < (const Fish& a, const Fish& b){
    if (a.f==b.f){
        return a.i>b.i;  //如果多个计划存在时,选择靠前的
    }
    else {
        return a.fvector gofish(25); 
//在0~k之间钓鱼
int fish(int& k) {
    int nfishes=0;
    int rest_h=h;
    priority_queuevector, less > q;
    for(int i=0; i<=k; i++) {
        q.push(gofish[i]);
    }
    Fish topfish;
    while(rest_h) {
        topfish=q.top();
        if (topfish.f==0){
            ft[0] += rest_h;
            break;
        }
        nfishes += topfish.f;
        ft[topfish.i]++;
        if (topfish.f>topfish.d)
            topfish.f -= topfish.d;
        else
            topfish.f = 0;
        q.pop();
        q.push(topfish);
        rest_h--;
    }
    return nfishes;
}

int main(){  
    while(~scanf("%d",&n) && n>0) {
        scanf("%d",&h);
        h *= 12;
        for(int i=0; iscanf("%d",&gofish[i].f);
            gofish[i].i=i;  //为每个鱼塘编上号
        } 
        for(int i=0; iscanf("%d",&gofish[i].d);
        } 
        for(int i=0; i1; i++) {
            scanf("%d",&gofish[i].t);
        } 
        int maxfishes=0;        
        vector<int> fishtime(n, 0);
        for (int i=0; iif (i>0){
                h -= gofish[i-1].t;
            }
            memset(ft, 0, sizeof(ft));;
            int tempf=fish(i);
            if (maxfishes < tempf) {
                maxfishes = tempf;
                for (int j=0; j<=i; j++){
                    fishtime[j] = ft[j]*5;
                }
            }           
        }
        cout << fishtime[0];
        for(int i=1; icout << ", " << fishtime[i];
        cout << endl;
        cout << "Number of fish expected: " << maxfishes << endl;
        cout << endl;
    }
    return 0;  
}  

你可能感兴趣的:(程序设计与算法)