Link:
http://acm.hdu.edu.cn/showproblem.php?pid=5550
Test instructions
A building has n (2≤n≤4000) layers, each floor can be built with a table tennis room or a swimming room, and each room must have at least one in the building.
Each layer is known to have a TI table tennis player and a PI swimmer (1≤ti,pi≤1e9).
Ask how to build a house, in order to make all the athletes to the corresponding room the minimum total distance, the output minimum value.
Analysis:
Because each room must have at least one in the building, so there must be a state: Layer I is a room, the first i+1 layer is another room.
So you can set D[i][x]: The first layer is X room, and the i+1 layer is another room, the first I layer of the optimal solution.
The state transition equation is: d[i][x] = min (d[k][x^1] + k+1~i layer is the smallest total distance produced by x Room), 1≤k<i.
Where x^1 represents another room, the l~r layer is x room when the minimum total distance produced can be prefixed and preprocessing, the specific look at the code.
BTW, this is titled 2015 CCPC's silver medal title.
Code:
1#include <cstdio>2#include <algorithm>3 using namespacestd;4 5typedefLong Long intLLI;6 ConstLLI INF =0x3f3f3f3f3f3f3f3f;7 Const intup =4000+5;8LLI sum[up][2];//Sum[i][x]: Top I-level x total number of athletes9LLI dist[up][2];//Dist[i][x]: The total distance of the front I layer all x athletes to the No. 0 floorTenLLI d[up][2];//D[i][x]: The first layer is X room, and the i+1 layer is another room, the optimal solution of the front I layer One ALLI ToLeft (intLintRintx) {//The total distance of all x athletes to the L-1 level of the L~R layer - return(dist[r][x]-dist[l-1][X])-(sum[r][x]-sum[l-1][X]) * (L-1); - } the -LLI ToRight (intLintRintx) {//The total distance of all x athletes to the r+1 level of the L~R layer - return(sum[r][x]-sum[l-1][X]) * (r+1)-(dist[r][x]-dist[l-1][x]); - } + -LLI Process (intLintRintx) {//The minimum total distance of all x athletes to the L-1 layer or r+1 layer of the L~R layer + intM = L + (r-l)/2; A returnToLeft (L, M, x) + (m+1>r?0: ToRight (m+1, R, x)); at } - - intMain () { - intT, N; - LLI T, p; -scanf"%d", &T); in for(intCases =1; Cases <= T; cases++) { -scanf"%d", &n); to for(inti =1; I <= N; i++) { +scanf"%lld%lld", &t, &p); -sum[i][0] = sum[i-1][0] +T; thesum[i][1] = sum[i-1][1] +p; *dist[i][0] = dist[i-1][0] + t*i; $dist[i][1] = dist[i-1][1] + p*i;Panax Notoginseng } -LLI ans =INF; the for(inti =1; I < n; i++) { +d[i][0] = ToRight (1I1);//The first I floor is 0 rooms, the first i+1 floor is the total distance of 1 rooms Ad[i][1] = ToRight (1I0);//The first I floor is 1 rooms, the first i+1 floor is the total distance of 0 rooms the for(intK =1; K < I; k++) { +d[i][0] = min (d[i][0], d[k][1] + process (k +1I1));//The first k+1~i floor is 0 rooms . -d[i][1] = min (d[i][1], d[k][0] + process (k +1I0));//The first k+1~i floor is 1 rooms . $ } $ans = min (ans, d[i][0] + toleft (i+1N0));//the back i+1 floor is 1 rooms . -ans = min (ans, d[i][1] + toleft (i+1N1));//the back i+1 floor is 0 rooms . - } theprintf"Case #%d:%lld\n", cases, ans); - }Wuyi return 0; the}
HDU 5550-game Rooms (DP + prefix and preprocessing)