zoukankan      html  css  js  c++  java
  • POJ 1042 Gone Fishing

    题意:一个人要在n个湖中钓鱼,湖之间的路径是单向的,只能走1->2->3->...->n这一条线路,告诉你每个湖中一开始能钓到鱼的初始值,和每钓5分钟就减少的数量,以及湖之间的距离,问用h小时最多钓多少鱼。鱼的数量不会增加,而且如果不钓鱼的话鱼的数量不会减少,如果有多个答案,输出在小号的湖上花费时间最多的答案。

    解法:贪心。枚举在前i个湖里钓鱼,那么走的路程就是一定的,用总时间减去走过的时间,剩下的时间每5分钟为一个单位,选鱼最多的湖钓,然后更新湖里鱼的数量。据说dp也可以做,大概想到了但是觉得好麻烦没有写……以下是我的想法……欢迎指正:可以用一个结构体dp数组,结构体里存鱼数和时间,dp[i][j]表示在前i个湖里钓鱼用了j时间时钓到的鱼数和在第i个湖用的时间,应该就可以了吧……

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<limits.h>
    #include<time.h>
    #include<stdlib.h>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #define LL long long
    using namespace std;
    struct node
    {
        int f, d, id;
        bool operator < (const node &tmp) const
        {
            if(f == tmp.f) return id > tmp.id;
            else return f < tmp.f;
        }
    }l[30];
    int sum[30];
    int ans[30][30];
    int main()
    {
        int n, h;
        while(~scanf("%d", &n) && n)
        {
            scanf("%d", &h);
            memset(sum, 0, sizeof sum);
            memset(ans, 0, sizeof ans);
            for(int i = 1; i <= n; i++)
            {
                scanf("%d", &l[i].f);
                l[i].id = i;
            }
            for(int i = 1; i <= n; i++)
                scanf("%d", &l[i].d);
            for(int i = 2; i <= n; i++)
            {
                int x;
                scanf("%d", &x);
                sum[i] = sum[i - 1] + x;
            }
            int st = h * 12;
            int maxn = -1, pos = 0;
            for(int i = 1; i <= n; i++)
            {
                priority_queue <node> q;
                for(int j = 1; j <= i; j++)
                    q.push(l[j]);
                int res = 0;
                for(int j = 0; j < st - sum[i]; j++)
                {
                    node tmp = q.top();
                    q.pop();
                    res += tmp.f;
                    ans[i][tmp.id]++;
                    q.push((node){max(0, tmp.f - tmp.d), tmp.d, tmp.id});
                }
                if(res > maxn)
                {
                    maxn = res;
                    pos = i;
                }
            }
            for(int i = 1; i <= n; i++)
            {
                if(i != 1) printf(", ");
                printf("%d", ans[pos][i] * 5);
            }
            puts("");
            printf("Number of fish expected: %d
    
    ", maxn);
        }
        return 0;
    }
    

      

  • 相关阅读:
    LeetCode OJ 107. Binary Tree Level Order Traversal II
    LeetCode OJ 116. Populating Next Right Pointers in Each Node
    LeetCode OJ 108. Convert Sorted Array to Binary Search Tree
    LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode OJ 98. Validate Binary Search Tree
    老程序员解Bug的通用套路
    转载 四年努力,梦归阿里,和大家聊聊成长感悟
    转载面试感悟----一名3年工作经验的程序员应该具备的技能
    Web Service和Servlet的区别
    关于spring xml文件中的xmlns,xsi:schemaLocation
  • 原文地址:https://www.cnblogs.com/Apro/p/4846145.html
Copyright © 2011-2022 走看看