二分与贪心-Gone Fishing(算法基础 第9周)
来源:互联网 发布:微信小说分销源码下载 编辑:程序博客网 时间:2024/05/16 05:45
问题描述:
分析
共n个湖,h个小时的时间;每个湖首次可钓到fi条鱼,往后一次递减di条,从i湖走到i+1湖需ti×5分钟。问:指定时间内最多可钓多少条鱼。
参考代码;http://blog.csdn.net/lyhvoyage/article/details/23289531 。按他的方式,第一种方法测试超时,第二种使用优先队列方法可以通过。但自己写了两个,即使使用优先队列也会超时,呃,,郁闷,,代码先放这儿,以后再调。
源码
//代码1,不使用优先队列,超时
#include <iostream> #include <vector>#include <cstring>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 (maxfi<kfi[i]){ maxfi=kfi[i]; ind=i; } } return 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; i<n; i++) { cin >> fi[i]; } for(int i=0; i<n; i++) { cin >> di[i]; } for(int i=0; i<n-1; i++) { cin >> ti[i]; } int maxfishes=0; vector<int> fishtime(n, 0); for (int i=0; i<n; i++) { if (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; i<n; i++) cout << ", " << fishtime[i]; cout << endl; cout << "Number of fish expected: " << maxfishes << endl; cout << endl; }}
//代码2,使用优先队列,还是超时,日后再看
#include <iostream> #include <vector>#include <queue>#include <algorithm>#include <cstring>#include <stdio.h>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.f<b.f; }}vector<Fish> gofish(25); //在0~k之间钓鱼int fish(int& k) { int nfishes=0; int rest_h=h; priority_queue<Fish, vector<Fish>, less<Fish> > 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; i<n; i++) { scanf("%d",&gofish[i].f); gofish[i].i=i; //为每个鱼塘编上号 } for(int i=0; i<n; i++) { scanf("%d",&gofish[i].d); } for(int i=0; i<n-1; i++) { scanf("%d",&gofish[i].t); } int maxfishes=0; vector<int> fishtime(n, 0); for (int i=0; i<n; i++) { if (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; i<n; i++) cout << ", " << fishtime[i]; cout << endl; cout << "Number of fish expected: " << maxfishes << endl; cout << endl; } return 0; }
0 0
- 二分与贪心-Gone Fishing(算法基础 第9周)
- Gone Fishing (经典贪心)
- uva757 - Gone Fishing(贪心)
- 二分与贪心-圣诞老人的礼物(算法基础 第9周)
- 二分与贪心-烘晾衣服(算法基础 第9周)
- 二分与贪心-雷达安装问题(算法基础 第9周)
- 二分与贪心-誊写书籍(算法基础 第9周)
- 二分与贪心-Aggressive cows(算法基础 第9周)
- 二分与贪心-Yogurt factory(算法基础 第9周)
- Gone Fishing(Poj1042)(贪心+枚举)
- poj 1042 Gone Fishing(贪心 钓鱼)
- NYOJ 30 Gone Fishing(枚举+贪心)
- NYOJ 30Gone Fishing(贪心)
- POJ 1042 Gone Fishing(贪心+枚举)
- UVA 757 Gone Fishing(贪心 + 暴力)
- uva 757 Gone Fishing(贪心)
- POJ 1042 Gone fishing(枚举+贪心)
- NYOJ 30 Gone Fishing(贪心)
- C# DataTable常用方法总结
- Android动态加载技术之三个基础性问题
- XCODE的相关配置
- Android studio中设置adb超时时间
- Android DownloadManager 下载完成并安装
- 二分与贪心-Gone Fishing(算法基础 第9周)
- 简介android Binder
- mongodb的聚合函数aggregate的$match和$group方法。
- 树:Trie树
- linux下 目录(扩容)挂载磁盘
- docker swarm 使用
- Invalid content was found starting with element 'async-supported
- 如何调整eclipse左边包的显示方式(package presentation)
- QT 文件拖拽问题