Total time limit: 2000 ms memory limit: 65536kb 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. 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. enter 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 shocould 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 has Ds 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 has Ds 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 3 3 1 2 3 4 4 10 15 30 0 3 4 1 2 3 0 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 east central North America 1999
The detailed idea of this question can be found from the black book that the greedy method can solve, but there is a problem that I have not understood. The Black Book writes the complexity of the ordinary algorithm O (kN ^ 2), and there is no problem, but why is the heap reduced to "O" (knlogn )?
If a heap is used, the minimum value is retrieved from the heap for a total of N enumerations. In each enumeration, the minimum value is retrieved from the heap for K times, if we only calculate the time complexity of the minimum value, it is indeed O (knlogn), but if we calculate the time of nlogn on the heap during each enumeration process, the resulting complexity result should be O (N (klogn + nlogn). Because k <n, we can conclude that the complexity should be O (N ^ 2 * logn.
So I used the simplest Traversal method to obtain the minimum AC.
Paste code
#include <iostream>#include <stdio.h>#include <memory.h>using namespace std;int n;int coun[26] = {0}, temp[26]={0}, sum = 0;int f[26],d[26],t[26];int h = 0;int main(){ while(cin>>n) { if(n == 0) break; memset(f,0,sizeof(f)); memset(d,0,sizeof(d)); memset(t,0,sizeof(t)); scanf("%d",&h); h *= 12; for(int i = 0; i < n; i ++) scanf("%d",&f[i]); for(int i = 0; i < n; i ++) scanf("%d",&d[i]); for(int i = 0; i < n-1; i ++) scanf("%d",&t[i]); memset(coun,0,sizeof(coun)); sum = -1; for(int i = 0; i < n; i ++) { memset(temp,0,sizeof(temp)); int tim = 0; for(int j = 0; j < i; j ++) { tim += t[j]; } tim = h - tim; //printf("%d\n",tim); int tf[26]; memcpy(tf,f,sizeof(tf)); /*for(int i = 0; i <n; i++) printf("%d\n",tf[i]); */ int ts = 0; for(int k = 0; k < tim; k ++) { int ti = 0; //compute the max lake int tn = 0; // the index for(int j = 0; j <= i; j ++) { if( tf[j] > ti) { ti = tf[j]; tn = j; } } if(ti == 0) { temp[0] += tim - k; break; } ts += ti; tf[tn] -= d[tn]; if( tf[tn] < 0 ) tf[tn] = 0; temp[tn] ++; } if(ts > sum) { sum = ts; memcpy(coun,temp,sizeof(coun)); } } printf("%d",coun[0]*5); for(int i = 1; i < n; i ++) printf(", %d",coun[i]*5); printf("\n"); printf("Number of fish expected: %d \n\n",sum); } return 0;}
Note a few boundary issues. In my initial code, the initial sum value is assigned to 0 in each case, which leads to a situation, if the number of fish in all the lakes in the example is 0, then because of the IF (TS> sum) condition (this condition cannot be changed to the equal sign, according to the meaning of the question, the same output should be the time spent on the previous lake as long as possible. If it is changed to the equal sign, it will overwrite the previous policy for a longer time in the latter Lake ), no computing results will be assigned to coun [26], and the output will be 0 for each lake, but this is not the case, the output result should be spent all the time on the first Lake. Therefore, the initial sum =-1.