zoukankan      html  css  js  c++  java
  • POJ 1042 Gone Fishing(黑书1.22例一)

    枚举在每个湖结束钓鱼的情况,去掉移动的时间。这样在各个湖之间的移动就可以看成瞬移的了,贪心可以很容易的求出最大解。
    Gone Fishing

    Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) :    Accepted Submission(s) : 
    Problem 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
     

    Source
    PKU
     

    慢吞吞的写了很长时间,但一遍AC了,有这么多补丁的代码应该没什么人可以看懂吧

    #include <iostream>
    #include <cstring>

    #define ninf -99999999

    using namespace std;

    int n,h;
    int f[28][200];
    int nf[200];
    int tf[200];
    int d[28];
    int t[28];

    int main()
    {
        while(cin>>n&&n)
        {
            cin>>h;
            for(int i=1;i<=n;i++)
                cin>>f[1];
            for(int i=1;i<=n;i++)
                cin>>d;
            for(int i=1;i<=n-1;i++)
                cin>>t;

            for(int i=0;i<200;i++)
                nf=1;
            memset(tf,0,sizeof(tf));

            for(int i=1;i<=n;i++)
            {
                for(int j=2;j<=h*12;j++)
                   {
                       f[j]=f[j-1]-d;
                       if(f[j]<0)
                        f[j]=0;
                   }
            }
    /*
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=h*12;j++)
                   cout<<f[j]<<" ";
                cout<<endl;
            }
    */
            int nums;
            int times=h*12;
            for(nums=1;nums<=n-1&&times>0;nums++)
            {
                times-=t[nums];
            }
            if(times<=0)///有两种方式跳出,注意判断
                 nums--;///最多可以停在NUMS上
     //       cout<<nums<<endl;

            int fish=0;
            int Maxing=ninf;
            int times2=-1;
            for(int i=1;i<=nums;i++)
            {
                int waste=0;
                for(int j=1;j<=i-1;j++)
                    waste+=t[j];
                times=h*12-waste;
                fish=0;
                for(int j=0;j<200;j++)
                    nf[j]=1;

                ///greed

                int sum;
                for(int j=1;j<=times;j++)
                {
                    sum=ninf;
                    int nth=-1;
                    for(int k=1;k<=i;k++)
                    {
           //             cout<<sum<<":::"<<f[k][nf[k]]<<endl;
                        if(f[k][nf[k]]>sum&&f[k][nf[k]]>=0)
                        {
                            sum=f[k][nf[k]];
                            nth=k;
                        }
                    }
                    if(sum>0)
                    {
                        nf[nth]++;
                        fish+=sum;
                    }
                }
                if(Maxing<fish)
                {
                    Maxing=fish;///最多可以钓的鱼
                    times2=i;///应该在TIMES2号湖上结束钓鱼
                }

          //          cout<<times<<"   "<<times2<<"   "<<Maxing<<endl;
            }


    //        cout<<times<<"   "<<times2<<"   "<<Maxing<<endl;

            memset(tf,0,sizeof(tf));

            for(int i=0;i<200;i++)
                nf=1;

            fish=0;
            int waste=0;
            for(int o=1;o<=times2-1;o++)
                waste+=t[o];
            times=h*12-waste;
     //       cout<<times<<"   "<<times2<<"   "<<Maxing<<endl;
     //       cout<<"~~~"<<f[2][1]<<endl;
            int sum=ninf;
            memset(tf,0,sizeof(tf));
            for(int j=1;j<=times;j++)///TIMES次钓鱼
            {
                sum=ninf;
                int nth=-1;
                for(int k=1;k<=times2;k++)
                {
              //      cout<<k<<nf[k]<<endl;
        //            cout<<sum<<":::"<<f[k][nf[k]]<<endl;
                    if(f[k][nf[k]]>sum&&f[k][nf[k]]>=0)
                    {
                        sum=f[k][nf[k]];
                        nth=k;
                    }
                }
                if(sum>0)
                {
                    nf[nth]++;
                    tf[nth]++;
                    fish+=sum;
                }
                else if(sum==0)
                {
                    tf[1]++;
                }
            }

     //       cout<<times<<"   "<<times2<<"   "<<Maxing<<endl;
     /*       cout<<endl<<fish<<endl<<endl;
            for(int i=1;i<=times2;i++)
                cout<<tf<<" ";
            cout<<endl;
    */
            for(int i=1;i<=n;i++)
              {
                  cout<<tf*5;
                  if(i!=n)
                    cout<<", ";
              }
            cout<<endl;
            cout<<"Number of fish expected: "<<Maxing<<endl<<endl;


        }

        return 0;
    }

  • 相关阅读:
    数据结构和算法(Golang实现)(9)基础知识-算法复杂度及渐进符号
    基于深度学习方法的dota2游戏数据分析与胜率预测(python3.6+keras框架实现)
    基于CBOW网络手动实现面向中文语料的word2vec
    《Machine Learning Yearing》读书笔记
    使用神经网络预测航班起飞准点率
    使用LSTM-RNN建立股票预测模型
    基于selenium+phantomJS的动态网站全站爬取
    TensorFlow保存、加载模型参数 | 原理描述及踩坑经验总结
    学习笔记--python中使用多进程、多线程加速文本预处理
    通过外汇对冲手段稳定获利的可行性验证
  • 原文地址:https://www.cnblogs.com/CKboss/p/3351083.html
Copyright © 2011-2022 走看看