Question:
There are n tasks, each of which takes one unit of time to complete, and each task has a specified time period. If the time limit is exceeded, a fine will be imposed. Find the task combination with the minimum penalty and the minimum penalty amount.
Input:
An integer n represents the number of tasks. The following n rows have two integers: the first is the time period, and the second is the time-out penalty.
Output:
Number of completed tasks within the term, number of completed tasks beyond the term, and number of fines.
The key to this question is the fine amount, so we should try our best to complete the tasks with a large penalty amount first, sort the tasks according to the fine amount from large to small, and process each task in sequence. After a task that can be completed in advance is obtained, the task is sorted by time period, so that the tasks with a large time period are as low as possible, so as to ensure the maximum number of tasks completed in advance.
Code:
# Include <iostream>
# Include <cstring>
Using namespace STD;
Typedef struct test {
Int K, D, W;
} Text;
Int main (){
Text tlist [100], list [100];
Int P [100];
Int temp [1010];
Memset (temp, 0, sizeof (temp ));
Int N, I, j, T;
Cin> N;
For (I = 1; I <= N; I ++ ){
Cin> tlist [I]. D> tlist [I]. W;
Tlist [I]. k = I;
Temp [tlist [I]. W] ++;
}
For (I = 2; I <= 1000; I ++) // sort the count
Temp [I] + = temp [I-1];
For (j = N; j> = 1; j --){
List [temp [tlist [J]. W] = tlist [J];
Temp [tlist [J]. W] --;
}
Int num = 0; // number of tasks completed in advance
For (I = N; I> = 1; I --) {// because the Count sorting is from small to large, the task searches from large to small.
T = 0;
For (j = 1; j <= num; j ++)
If (list [p [J]. d <= num)
T ++;
If (T <list [I]. d) {// if the time limit of the I-th task is less than the number of tasks to be completed in advance, add the I-th task to the task sequence that can be completed in advance.
Num ++;
P [num] = I;
List [I]. k =-list [I]. K;
J = num;
While (j> 1) {// The time limit for the tasks completed in advance from small to large
If (list [p [J]. d <list [p [J-1]. d ){
T = P [J];
P [J] = P [J-1];
[J-1] = T;
J --;
}
Else break;
}
}
}
For (I = 1; I <= num; I ++)
Cout <-list [p [I]. k <"";
Cout <Endl;
T = 0;
For (I = 1; I <= N; I ++)
If (list [I]. k> 0 ){
Cout <list [I]. k <"";
T + = list [I]. W;
}
Cout <Endl <t;
Return 0;
}