函数题
R6-1 Manager of Tasks
R6-1 Manager of Tasks
分数 8
作者 王灿
单位 浙江大学

There are NN tasks arranged in a sequence on a machine waiting to be executed, and their order cannot be changed. You need to divide these NN tasks into several groups, each containing several consecutive tasks. Starting from time 0, the tasks are processed in groups, and the time required to execute the ii-th task is TiT_i. Additionally, the machine requires a startup time SS before each group of tasks begins, so the time required for a group of tasks is the startup time SS plus the sum of the time required for each task in this group.

After a task is executed, it will wait briefly in the machine until all tasks in that group are completely executed. That is to say, the tasks in the same group will be completed at the same time. The cost of each task is its completion time multiplied by a cost coefficient CiC_i.

Please plan a grouping scheme for the machine to minimize the total cost.

For all testing data, 1N1000,0S50,1Ti,Ci1001\le N \le 1000, 0 \le S \le 50, 1\le T_i, C_i \le 100

Function Interface:

long long min_cost(int N, int S, int T[], int C[]);

where T, C are arrays of integers with N elements, and S is the startup time SS mentioned above.

Judge Program:

#include <stdio.h> #define MAXN 1000 long long min_cost(int N, int S, int T[], int C[]); int main() { int N, S; int T[MAXN], C[MAXN]; scanf("%d%d", &N, &S); for (int i = 0;i < N; ++ i) { scanf("%d%d", &T[i], &C[i]); } printf("%lld\n", min_cost(N, S, T, C)); return 0; } /* Your function will be put here */

Sample Input:

5
1
1 3
3 2
4 3
2 3
1 4

Sample Output:

153

Sample Explanation

We have grouped the tasks into 3 groups, which are {1, 2}, {3}, {4, 5}. The completion time corresponding to each task, in the order of the task numbers, is {5, 5, 10, 14, 14}. Similarly, the cost corresponding to each task, again in the order of the task numbers, is {15, 10, 30, 42, 56}. The total cost of these tasks is 153.

代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
C (gcc)
#include<stdlib.h>
long long min_cost(int N, int S, int T[], int C[]){
if(N == 0) return 0;
long long *f0 = (long long *)malloc((N+5)*(N+5)*2);
typedef long long mytype[N+5][N+5][2]
mytype f = (mytype) f0;
f[0][1][0] = (S+T[0])*C[0]; // cost
f[0][1][1] = (S+T[0]); // comp time
if(N == 1) return f[0][1][0];
for(int i=0; i<=N; i++){
for(int j=0; j<=N; j++) f[i][j][0] = f[i][j][1] = 1e15;
}
for(int i=1; i<N; i++){
long long c = 0, t = 0;
for(int j=i; j>=1; j--){
c += C[j];
t += T[j];
for(int k=j; k>=2; k--){
if(f[j-1][k-1][0] + (S+f[j-1][k-1][1]+t)*c < f[i][k][0]){
f[i][k][0] = f[j-1][k-1][0] + (S+f[j-1][k-1][1]+t)*c;
f[i][k][1] = S+f[j-1][k-1][1]+t;
}
}
}
c += C[0];
t += T[0];
f[i][1][0] = (S+t)*c;
f[i][1][1] = S+t;
}
long long min = 1e15;
for(int i=1; i<=N; i++){
if(f[N-1][i][0] < min) min = f[N-1][i][0];
}
return min;
/*
long long f[N+5][2];