标签:des blog http io ar os sp for strong
Description
The cars waiting to board the ferry form a single queue, and the operator directs each car in turn to drive onto the port (left) or starboard (right) lane of the ferry so as to balance the load. Each car in the queue has a different length, which the operator estimates by inspecting the queue. Based on this inspection, the operator decides which side of the ferry each car should board, and boards as many cars as possible from the queue, subject to the length limit of the ferry. Your job is to write a program that will tell the operator which car to load on which side so as to maximize the number of cars loaded.
The first line of input contains a single integer between 1 and 100: the length of the ferry (in metres). For each car in the queue there is an additional line of input specifying the length of the car (in cm, an integer between 100 and 10000 inclusive). A final line of input contains the integer 0. The cars must be loaded in order, subject to the constraint that the total length of cars on either side does not exceed the length of the ferry. Subject to this constraint as many cars should be loaded as possible, starting with the first car in the queue and loading cars in order until no more can be loaded.
The first line of outuput should give the number of cars that can be loaded onto the ferry. For each car that can be loaded onto the ferry, in the order the cars appear in the input, output a line containing "port" if the car is to be directed to the port side and "starboard" if the car is to be directed to the starboard side. If several arrangements of the cars meet the criteria above, any one will do.
1 50 2500 3000 1000 1000 1500 700 800 0
6 port starboard starboard starboard port port
[解题方法]
dp[i][j]=1 表示左边车占了j长度时,可以放i辆车
设前i辆车总长度为sum[i],则:
// 对于dp[i][j]左边占了j长度,右边就必然占了sum[i]-j的长度了
// dp[i][j]=0 表示这种状态不可行
// 设V为公路长度,w[i]为第i辆车的长度,状态转移如下:(0<=j<=V)
// dp[i][j] = (j>=w[i]&&dp[i-1][j-w[i]]) || (sum[i]-j<=V&&dp[i-1][j])
路径记录 值得学习冏= =
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<cstdlib> #include<queue> #include<vector> #include<set> using namespace std; const int N = 2010; const int M = 10010; int dp[N][M]; int w[N], sum[N]; int pre[N][M]; int ans[N]; int main() { int t, v, n, k, vj, i, j; scanf("%d", &t); while (t--) { scanf("%d", &v); v *= 100; memset(dp, 0x00, sizeof(dp)); dp[0][0] = 1; n = 0; sum[0] = 0; while (scanf("%d", &k) && k) { n++; sum[n] = sum[n - 1] + k; w[n] = k; } vj = -1; for (int i = 1; i <= n; i++) { for (int j = 0; j <= v; j++) { if (j >= w[i] && dp[i - 1][j - w[i]]) { k = i; vj = j; dp[i][j] = 1; pre[i][j] = j - w[i]; } else if (sum[i] - j <= v && dp[i - 1][j]) { k = i; vj = j; dp[i][j] = 1; pre[i][j] = j; } } } i = k; while (i--) { j = pre[i + 1][vj]; if (j == vj) ans[i] = 1; else ans[i] = 0; vj = j; } printf("%d\n", k); for (int i = 0; i < k; i++) { if (ans[i]) printf("starboard\n"); else printf("port\n"); } if (t) printf("\n"); } return 0; }
标签:des blog http io ar os sp for strong
原文地址:http://www.cnblogs.com/a972290869/p/4107750.html