1. 程式人生 > 實用技巧 >AW_魚塘釣魚(大根堆)

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)\)