• Gone Fishing_优先队列&&贪心


    Time Limit: 2000MS   Memory Limit: 32768K
    Total Submissions: 33058   Accepted: 10114

    Description

    John is going on a fishing trip. He has h hours available (1 <= h <= 16), and there are n lakes in the area (2 <= n <= 25) all reachable along a single, one-way road. John starts at lake 1, but he can finish at any lake he wants. He can only travel from one lake to the next one, but he does not have to stop at any lake unless he wishes to. For each i = 1,...,n - 1, the number of 5-minute intervals it takes to travel from lake i to lake i + 1 is denoted ti (0 < ti <=192). For example, t3 = 4 means that it takes 20 minutes to travel from lake 3 to lake 4. To help plan his fishing trip, John has gathered some information about the lakes. For each lake i, the number of fish expected to be caught in the initial 5 minutes, denoted fi( fi >= 0 ), is known. Each 5 minutes of fishing decreases the number of fish expected to be caught in the next 5-minute interval by a constant rate of di (di >= 0). If the number of fish expected to be caught in an interval is less than or equal to di , there will be no more fish left in the lake in the next interval. To simplify the planning, John assumes that no one else will be fishing at the lakes to affect the number of fish he expects to catch. 
    Write a program to help John plan his fishing trip to maximize the number of fish expected to be caught. The number of minutes spent at each lake must be a multiple of 5.

    Input

    You will be given a number of cases in the input. Each case starts with a line containing n. This is followed by a line containing h. Next, there is a line of n integers specifying fi (1 <= i <=n), then a line of n integers di (1 <=i <=n), and finally, a line of n - 1 integers ti (1 <=i <=n - 1). Input is terminated by a case in which n = 0.

    Output

    For each test case, print the number of minutes spent at each lake, separated by commas, for the plan achieving the maximum number of fish expected to be caught (you should print the entire plan on one line even if it exceeds 80 characters). This is followed by a line containing the number of fish expected. 
    If multiple plans exist, choose the one that spends as long as possible at lake 1, even if no fish are expected to be caught in some intervals. If there is still a tie, choose the one that spends as long as possible at lake 2, and so on. Insert a blank line between cases.

    Sample Input

    2 
    1 
    10 1 
    2 5 
    2 
    4 
    4 
    10 15 20 17 
    0 3 4 3 
    1 2 3 
    4 
    4 
    10 15 50 30 
    0 3 4 3 
    1 2 3 
    0 

    Sample Output

    45, 5 
    Number of fish expected: 31 
    
    240, 0, 0, 0 
    Number of fish expected: 480 
    
    115, 10, 50, 35 
    Number of fish expected: 724 

    题意解释:

    John现有h个小时去钓鱼。钓鱼的地方共有n个湖,所有的湖沿着一条单向路顺序排列(John每在一个湖钓完鱼后,他只能走到下一个湖继续钓),John必须从1号湖开始钓起,但是他可以在任何一个湖结束他此次钓鱼的行程。

    此题以5分钟作为单位时间,John在每个湖中每5分钟钓的鱼数随时间的增长而线性递减。每个湖中头5分钟可以钓到的鱼数用fi表示,每个湖中相邻5分钟钓鱼数的减少量用di表示,John从任意一个湖走到它下一个湖的时间用ti表示。

    求一种方案,使得John在有限的h小时中可以钓到尽可能多的鱼。

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    struct node
    {
        int f,d,id;
    }a[35];
    priority_queue<node>qu;
    bool operator <(node a,node b)
    {
        if(a.f==b.f) return a.id>b.id;
        else return a.f<b.f;
    }
    int dis[35],num[35],tmp[35],tt,manx;
    int main()
    {
        int n,t,dd;
        while(cin>>n,n)
        {
            scanf("%d",&t);
            t*=12;
            memset(dis,0,sizeof(dis));
            memset(num,0,sizeof(num));
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i].f);
            }
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i].d);
                a[i].id=i;
            }
            dis[1]=0;
            for(int i=2;i<=n;i++)
            {
                scanf("%d",&dd);
                dis[i]=dis[i-1]+dd;
            }
            int ans=-1;
            for(int i=1;i<=n;i++)
            {
                manx=0;
                memset(tmp,0,sizeof(tmp));
                tt=t-dis[i];
                while(!qu.empty()) qu.pop();
                for(int j=1;j<=i;j++)
                {
                    qu.push(a[j]);
                }
                while(tt>0)
                {
                    node now=qu.top();
                    qu.pop();
                    manx+=now.f;
                    now.f-=now.d;
                    tt--;
                    tmp[now.id]++;
                    if(now.f<=0) now.f=0;
                    qu.push(now);
                }
                if(manx>ans)
                {
                    ans=manx;
                    for(int j=1;j<=i;j++)
                    {
                        num[j]=tmp[j];
                    }
                }
            }
            int i;
            for(i=1;i<n;i++)
            {
                printf("%d, ",num[i]*5);
            }
            printf("%d
    ",num[n]*5);
            printf("Number of fish expected: %d
    
    ",ans);
        }
    }
  • 相关阅读:
    关闭firefox的plugincheck
    C# 三个定时器区别
    数字图像处理学习 01 图像的几何变换
    C++ dll的创建和使用
    使用Log4Cplus+配置文件打印日志
    Bmp图像的数据格式及读取
    GCC的使用和Makefile的编写
    day03 QT学习 常用控件 QLabel QPushButton QLineEdit使用 QSS介绍以及QObject子对象的遍历
    day02 QT学习 字符集和中文乱码的问题
    day01 QT学习 信号槽和QWidget介绍
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/5795298.html
Copyright © 2020-2023  润新知