c_aw_鱼塘钓鱼(大根堆)

方法一:大根堆

每次都去能钓到尽量多的鱼塘取钓鱼

import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
    static class Solution {
        int f[], d[], t[];
        int get(int e, int time) {
            Queue<Node> q = new PriorityQueue<>((e1, e2) -> { return e2.f-e1.f; });
            for (int i=0; i<=e; i++) q.add(new Node(f[i], i, 0));
            int ans=0;
            for (int i=0; i<time; i++) {
                Node now = q.poll();
                if (now.f<=0) break;
                ans+=now.f;
                int nx_minute=now.mte+1, id=now.id;
                q.add(new Node(f[id]-nx_minute*d[id], id, nx_minute));
            }
            return ans;
        }
        void init() {
            Scanner sc = new Scanner(new BufferedInputStream(System.in));
            int n=sc.nextInt();
            f=new int[n]; d=new int[n]; t=new int[n];
            for (int i=0; i<n; i++) f[i]=sc.nextInt();
            for (int i=0; i<n; i++) d[i]=sc.nextInt();
            for (int i=1; i<n; i++) {t[i]=sc.nextInt(); t[i]+=t[i-1];}
            int limit=sc.nextInt(), ans=0;
            for (int e=0; e<n; e++) {
                ans=Math.max(ans, get(e, limit-t[e]));  //预先减去从1走到e鱼塘步行时间
            }
            System.out.println(ans);
        }
        class Node {
            int f, id, mte;
            Node(int fish, int pid, int minute) {f=fish; id=pid; mte=minute;}
        }
    }
    public static void main(String[] args) throws IOException {  
        Solution s = new Solution();
        s.init();
    }
}

复杂度分析

  • Time\(O(n^2logn)\)
  • Space\(O(n)\)
posted @ 2020-09-12 18:43  童年の波鞋  阅读(180)  评论(0编辑  收藏  举报