打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Gone Fishing 贪心算法
POJ 1042 Gone Fishing 贪心算法
2006-05-27 14:30:05
  
看上去比较难,看了报告分析就会觉得很简单了。贪心算法就能搞定,由于数据规模不大,因此每次都用遍历求最大值就能过了,但是我还是决定用堆去坐。普通遍历一次最大值复杂度O(N),用堆的话是O(logN)。以前不怎么用堆,这次也算用了呵呵。
 
 
代码如下:
/***************************************
 
   Author: 赵永辉
   Date:  2006.05.27
*****************************************/
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
#define min(a, b)  ((a) < (b) ? (a):(b))
#define max(a, b)  ((a) > (b) ? (a):(b))
#define INTMAX 1000000000
const double PI = 3.141592653589793238462643383279502884197169399375105820974944;
#define MAX 28
int n, h;
int d[MAX], t[MAX];
int temp[MAX], stay[MAX];
int i, j, r;
struct fish
{
  int f;
  int index;
  bool operator < (const fish &a) const
  {
   if(f < a.f)
    return true;
   else if(f == a.f)
    return index > a.index;
   else
    return false;
  }
};
fish f[MAX], now[MAX];
int ans, tans;
int  main()
{
 while(cin >> n && n)
 {
  ans = -1;
  cin >> h;
  for(i = 0;  i < n; i ++)
  {
   cin >> f[i].f;
   f[i].index = i;
  }
  for(i = 0;  i < n; i ++)
   cin >> d[i];
  t[0] = 0;
  for(i = 1;  i < n; i ++)
  {
   cin >> t[i];
   t[i] = t[i - 1] + t[i];
  }
  for(i = 1; i <= n; i ++)
  {
    memcpy(now, f, sizeof(f));
    memset(temp, 0, sizeof(temp));
    r = h*12 - t[i - 1];
    tans = 0;
    make_heap(now, now + i, less<fish>());
    for(j = 1; j <= r; j ++)
    {   
     if(now[0].f > 0)
        tans += now[0].f;
     now[0].f -= d[now[0].index];
     now[0].f = max(now[0].f, 0);
     temp[now[0].index] += 5;
     pop_heap(now, now + i, less<fish>());
     push_heap(now, now + i, less<fish>());    
    }
    if(tans > ans)
    {
     ans = tans;
     memcpy(stay, temp, sizeof(stay));
    }  
  }
        for(i = 0; i < n - 1; i ++)
   cout << stay[i] <<", ";
  cout << stay[n - 1] << endl;
  cout <<"Number of fish expected: "<< ans <<endl << endl;  
 }
 return 0;  
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
gouzaogongoo
几种C++排序算法的实现(冒泡,归并,快速,插入,选择)
C++函数模板
找出无序数组中第K小的数
【数值分析】插值法:拉格朗日插值、牛顿插值
几种排序算法的改进
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服