Question:
When you download an object online, a file is stored in one or more sections, and you are asked how to select the maximum number of files that can be downloaded within the specified traffic. The size of each segment is the same.
Ideas:
I used to store the answer in the DP array, and saved the answer in the subscript. I started to think about a backpack, but the memory was not enough and the time was not enough.
In fact, this is the case. DP [I] [J] [0/1] saves the enumeration to the nth I, downloads J, and determines whether the last one is downloaded at the minimum cost. Finally, you can find the maximum cost that has not exceeded the limit.
It is worth noting that the last section may not be divisible by P, so do not calculate too much.
Code:
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cstdlib> 5 # include <cmath> 6 # include <algorithm> 7 # include <string> 8 # include <queue> 9 # include <stack> 10 # include <vector> 11 # include <map> 12 # include <set> 13 # include <functional> 14 # include <cctype> 15 # include <time. h> 16 17 using namespace STD; 18 19 const int INF = 1 <30; 20 const int maxn = 3055; 21 22 int DP [maxn] [Ma XN] [2]; // DP [I] [J] [k] indicates that the I th is obtained and J is obtained, take the I th or do not take 23 int A [maxn]; 24 int sum [maxn]; // prefix and 25 int N, P, L; 26 27 int main () {28 # ifdef phantom0129 freopen ("hnu12961.in", "r", stdin); 30 # endif // phantom0131 32 While (scanf ("% d ", & N, & P, & L )! = EOF) {33 If (n = 0 & P = 0 & l = 0) break; 34 35 sum [0] = 0; 36 For (INT I = 1; I <= N; I ++) scanf ("% d", & A [I]); 37 for (INT I = 1; I <= N; I ++) sum [I] = sum [I-1] + A [I]; 38 for (INT I = 0; I <= N; I ++) 39 for (Int J = 0; j <= N; j ++) 40 DP [I] [J] [0] = DP [I] [J] [1] = inf; 41 DP [0] [0] [0] = 0; 42 43 for (INT I = 1; I <= N; I ++) {44 for (Int J = 0; j <= I; j ++) 45 DP [I] [J] [0] = min (DP [I-1] [J] [0], DP [I-1] [J] [1]); 46 (Int J = 1; j <= I; j ++) 47 DP [I] [J] [1] = min (DP [I-1] [J-1] [0]-(sum [I-1]/P) * P, 48 DP [I-1] [J-1] [1]-(sum [I-1] + Pm)/P) * P) 49 + (I = n? Sum [I]: (sum [I] + Pm)/P) * P); 50} 51 int ans = 0; 52 for (INT I = 0; I <= N; I ++) 53 If (DP [N] [I] [0] <= L | DP [N] [I] [1] <= L) 54 ans = max (ANS, i); 55 printf ("% d \ n", ANS); 56} 57 58 return 0; 59}
View code
Hnu 12961 BitTorrent DP