Luo Gu P1717 Fishing solution to a problem

Daily questions day46 punch

Analysis

First, we can easily find by subject, in order to get the optimal solution, then there can not waste time on the road, that can not go back. Then it is easy to be found, the number of fish in each time in a different ponds catch is different, in order to ensure that catch the most fish, then we have to choose a time to catch up to the current number of fish you can catch fish pond, fishing done after updating the number of fishing on the pond, then the next round of fishing.

So now the question arises: if it is to follow the above greedy method, each time to the largest number of fish ponds to go fishing can be caught, then it may appear to come and go between several ponds , waste time on the road.

How to solve this problem?

We can first determine the ponds can walk the farthest i, then subtract the time i come from a fish pond fish pond a time, in the rest of the time has been fishing, fishing can be assumed that people can instantly move in With the above method of ponds greedy ponds between 1 to i, you can be in order to go to the farthest ponds i is the optimal solution.

Why can assume that fishermen can instantly move it?

Because the scope of fishermen and fishing ponds are from 1 to i ponds, so he spent the least time to move time is 1 fish ponds i, then the remaining time can be used for all fishing from the ponds, then at this time the order is not fishing we have to consider, but what caught a few fish, that is, as long as we know that every time to catch fish where fishing is on the line, and do not know from the ponds after 1 fishing process, and the greedy algorithm mentioned above, is just seeking to catch every fish is where the fishing.

Solve this problem, then there is a problem: in the realization of greedy algorithm, how to quickly find each currently the largest number of fish ponds and real-time updates of the number of fishing ponds it?

Routine, then, is to sweep all over again, to find the maximum value, and then update it more cumbersome and undesirable. In this case, it is thought a very efficient data structure for the most value - priority queue. We can use a priority queue to store the current number of fishing ponds can be caught, as long as the maintenance of a large root heap, you can easily implement to get the maximum value and update.

Finally, we summarize the priority queue + greedy method, we have five minutes to a unit of time, you can reach the farthest exhaustive of all fish ponds, each time minus the time spent on the road with the total time, that is, from fish currently farthest from pond to pond 1, the fishing time is obtained, then greedy to seek the optimal solution in the present case, taking the maximum priority queue and updated with the time to achieve greedy, and finally all the best optimal solution selected one of the biggest get the final answer.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #define int long long
 7 #define maxn 100+10
 8 #define INF 2147483647
 9 #define rep(i,s,e) for(register int i=s;i<=e;++i)
10 #define dwn(i,s,e) for(register int i=s;i>=e;++i)
11 using namespace std;
12 inline int read()
13 {
14     int x=0,f=1;
15     char c=getchar();
16     while(c<'0'||c>'9') {if(c=='-') f=-1; c=getchar();}
17     while(c>='0'&&c<='9') {x=x*10+c-'0'; c=getchar();}
18     return f*x;
19 }
20 inline void write(int x)
21 {
22     if(x<0) {putchar('-'); x=-x;}
23     if(x>9) write(x/10);
24     putchar
25     (x%10+'0');
26 }
27 int n,h,ans=0;
28 struct node
29 {
30     int init,reduce,time;
31 }x[maxn];
32 priority_queue<pair<int,int> > q;
33 signed main()
34 {
35     n=read();
36     h=read();
37     h*=12;
38     rep(i,1,n) x[i].init=read();
39     rep(i,1,n) x[i].reduce=read();
40     rep(i,1,n-1) x[i].time=read();
41     rep(i,1,n)
42     {
43         while(!q.empty()) q.pop();
44         int re=h,red=0,sit=0;
45         rep(j,1,i) 
46         {
47             q.push(make_pair(x[j].init,j));
48             if(j!=i) red+=x[j].time; 
49         }
50         re-=red;
51         if(re<=0){ans=max(ans,sit); continue;}
52         while(re--)
53         {
54             int top=q.top().first,num=q.top().second;
55             q.pop();
56             sit+=top;
57             if(top-x[num].reduce<0) q.push(make_pair(0,num));
58             else q.push(make_pair(top-x[num].reduce,num));
59         }
60         ans=max(ans,sit);
61     }
62     write(ans);
63     return 0;
64 }

Please Gangster treatise(Anyway, I do not know what that means treatise)

Guess you like

Origin www.cnblogs.com/handsome-zyc/p/11915320.html