The scheduling problem per unit time | Greedy

Update the three programming questions of today's "Computational Theory and Algorithm Design" final exam. Questions without test cases are really unacceptable. I shared my thoughts during the exam, and finally gave out the self-test use cases I wrote. For reference only, please point out any errors~


02

Grades 15 opening time Wednesday, June 24, 2020 15:10
discount 0.8 Discount time Wednesday, June 24, 2020 18:30
Late submission allowed no Closing time Wednesday, June 24, 2020 18:30

Title description:

At present, a machine has to process n tasks, and each task can be completed in a unit of time. Each task has a deadline (d1, d2,,…, dn), and a penalty factor (w1, w2,,…, wn) that is not completed within the deadline. Find the smallest sum of penalty factors in all task arrangements. For example, if there are 3 tasks, the deadline is (2,2,2) and the penalty factor is (6,7,8). The penalty factors of 123, 132, 213, 231, 312, and 321 are 8, 7, 8, 6, 7, and 6, respectively. It can be seen that 6 is the smallest sum of penalty factors in all task arrangements.

Input format:

There are three lines, the first line is an integer n (0<n<=10000), the second line is n positive integers d1, d2,,..., dn (guaranteed to be in int), and the third line is n positive integers w1, w2,,…, wn (guaranteed to be in int).

Output format:

One number per line is the sum of the minimum penalty factor.

  Test input Expected output time limit Memory limit Extra process
Test case 1  
  1. 3↵
  2. 2 2 2↵
  3. 6 7 8↵
 
  1. 6↵
1 second 64M 0

       This question is obviously greedy. How to be greedy? The general idea is: Prioritize tasks with heavier punishment, and arrange for each task to be completed at the time closest to the dll. It can be proved that this question satisfies the nature of greedy choice, so the greedy strategy is also correct.


1. Data preprocessing

       First, the task is represented by a structure, including the task's DDL and penalty weight. Then create a thing[] array of tasks, and sort the array in descending order with weight as the key.

struct node {
    int deadLine;  //ddl
    int weight;  //惩罚
} thing[MAXN];

bool cmp(struct node x, struct node y) {
    if (x.weight == y.weight)  //当惩罚一样时
        return x.deadLine < y.deadLine;  //优先ddl在前面的
    else
        return x.weight > y.weight;  //优先惩罚更重的
}

       The input processing and the initialization of various arrays are implemented in an Init() function. In order to facilitate the scheduling of time periods, define a time slot: represented by the array done[ ]. If done[ i] = true, it means that the i-th unit time period is idle. So you should initialize done to all true, and assign false every time you allocate a time to avoid conflicts.

void Init() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &thing[i].deadLine);
    for (int i = 1; i <= n; i++)
        scanf("%d", &thing[i].weight);
    for(int i = 0;i <= n; i++)
        done[i] = true;
}

2. Greedy realization

      According to our sorted order (that is, prioritize tasks with heavier penalties), consider each task in turn and schedule it to complete at the time closest to the dll. If it cannot be found, it cannot be completed.

/* 为任务t寻找合适的时间
 * 找不到则返回false */
bool Done(struct node t) {
    //找到最接近ddl的空闲时间段
    for(int i = t.deadLine; i >= 1; i--)
        if(done[i]) {
            done[i] = false;
            return true;
        }
    //找不到则返回假
    return false;
}

int Calc() {
    sort(thing + 1, thing + n + 1, cmp);  //对任务以惩罚为关键字排序

    int ans= 0;
    for(int i = 1; i <= n; i++) {
        if(!Done(thing[i]))  //对于找不到合适时间的任务
            ans += thing[i].weight;  //加入惩罚
    }
    return ans;
}


The complete code and test cases are attached below :

#include <cstdio>
#include <algorithm>
#define MAXN 10050
using namespace std;

int n;
bool done[MAXN];   //第i个时间段是否有空

struct node {
    int deadLine;  //ddl
    int weight;  //惩罚
} thing[MAXN];

bool cmp(struct node x, struct node y) {
    if (x.weight == y.weight)  //当惩罚一样时
        return x.deadLine < y.deadLine;  //优先ddl在前面的
    else
        return x.weight > y.weight;  //优先惩罚更重的
}

void Init() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &thing[i].deadLine);
    for (int i = 1; i <= n; i++)
        scanf("%d", &thing[i].weight);
    for(int i = 0;i <= n; i++)
        done[i] = true;
}

/* 为任务t寻找合适的时间
 * 找不到则返回false */
bool Done(struct node t) {
    //找到最接近ddl的空闲时间段
    for(int i = t.deadLine; i >= 1; i--)
        if(done[i]) {
            done[i] = false;
            return true;
        }
    //找不到则返回假
    return false;
}

int Calc() {
    sort(thing + 1, thing + n + 1, cmp);  //对任务以惩罚为关键字排序

    int ans= 0;
    for(int i = 1; i <= n; i++) {
        if(!Done(thing[i]))  //对于找不到合适时间的任务
            ans += thing[i].weight;  //加入惩罚
    }
    return ans;
}

int main() {
    Init();
    printf("%d\n", Calc());
}

Test case:

4
1 1 3 4
7 8 9 10
Output: 7


4
2 2 2 1
16 7 8 2
Output: 9


5
5 5 2 2 3
12 3 1 2 9
Output: 0


6
3 3 3 4 4 4
7 8 9 12 1 3
Output: 4


8
1 1 1 2 2 2 3 3
1 2 3 1 3 3 5 4
Output: 10


3
2 2 2

6 7 8
output: 6


4
1 1 2 3
1 2 7 9
Output: 1


6
1 3 4 4 6 1
21 2 20 9 25 15
Output: 15


6
6 1 4 1 4 3
0 4 5 10 16 23
Output: 4


7
4 2 4 3 1 4 6
70 60 50 40 30 20 10
Output: 50



end 

Welcome to pay attention to the personal public account "  Chicken Wing Programming" , here is a serious and well-behaved code farmer.

---- Be the most well-behaved blogger and the most solid programmer ----

Aim to write each article carefully, and usually summarize the notes into push updates~

Insert picture description here

 

Guess you like

Origin blog.csdn.net/weixin_43787043/article/details/106950641