[Blue Bridge Cup] Q & A

Posted by imderek on Sun, 27 Feb 2022 05:08:29 +0100

answering question

have n n n students asked the teacher to answer questions at the same time. Each student has estimated the time of answering questions in advance.

The teacher can arrange the order of answering questions, and the students should enter the teacher's office to answer questions in turn. A student's Q & a process is as follows:

  • First enter the office, No i i i's classmates need s i s_i Time in milliseconds.
  • Then the students ask questions and the teacher answers them. The number is i i i's classmates need a i a_i ai = milliseconds.
  • After answering the questions, the students are very happy and will send a message in the course group. The time needed can be ignored.
  • Finally, the students pack up and leave the office. They need to e i e_i ei) milliseconds. General needs 10 10 10 seconds 20 20 20 seconds or 30 30 30 seconds, i.e e i e_i ei = value is 10000 10000 10000, 20000 20000 20000 or 30000 30000 30000.

After one student leaves the office, the next student can enter the office.

Answer questions from 0 0 Start at 0. The teacher wants to reasonably arrange the order of Q & a so that the sum of messages sent by students in the course group is the smallest.

Enter description
The first line of input contains an integer n n n. Indicates the number of students.

next n n n lines, describe each student's time. Among them i i Line i contains three integers s i , a i , e i s_i, a_i,e_i si, ai, ei, meaning as above.

Among them, 1 ≤ n ≤ 1000 , 1 ≤ s i ≤ 60000 , 1 ≤ a i ≤ 1 0 6 , e i ∈ 10000 , 20000 , 30000 1 ≤ n ≤ 1000,1 ≤ s_i ≤ 60000,1 ≤ a_i ≤ 10^6, e_i ∈ {10000, 20000, 30000} 1 ≤ n ≤ 1000, 1 ≤ si ≤ 60000, 1 ≤ ai ≤ 106, ei ∈ 100002000030000, i.e e i e_i ei must be 10000 , 20000 , 30000 10000,20000,30000 One of 10000, 20000 and 30000.

Output description
Output an integer indicating the minimum sum of the time when students send messages in the course group.

Sample input and output
Examples
input

3
10000 10000 10000
20000 50000 20000
30000 20000 30000

output

280000

thinking

  • Greedy
  • As the topics are related to the chronological order, the key problem is sorting
  • Read the question, s i , a i s_i,a_i si and ai can be combined into a time period, that is, the time period before a student sends a message, e i e_i ei is regarded as a separate time period, that is, the time period after a student sends a message

How to sort it?

  • To minimize the sum of all message sending moments, we should first meet the minimum of all time spent by a student (before and after sending messages), that is, the total time should be sorted from small to large
  • Secondly, if the total time spent by the two students is the same, those who spend less time before sending messages should ask first, that is, the time before sending messages should be sorted from small to large
  • Thirdly, if the total time is the same and the time before sending the message is the same, the smaller the time after sending the message, the better, that is, the time after sending the message is sorted from small to large
  • In short, the less time it takes, the better

After the sorting problem is solved, the problem is basically solved

  • Pay attention to the data range. It is too small to use int type in this question. You should use long long

The code is as follows

#include <algorithm>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;

vector<pair<int, int>> a;//Store the time spent by a student
int n;//Number of students

//Core: sorting
bool cmp(const pair<int, int>& a, const pair<int, int>& b) {
    if (a.first + a.second == b.first + b.second) {
        if (a.first == b.first) return a.second < b.second;
        return a.first < b.first;
    } else {
        return a.first + a.second < b.first + b.second;
    }
}

void init() {
    scanf("%d", &n);
    a.resize(n);
    int r = 0, s = 0, t = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d%d%d", &r, &s, &t);
        a[i] = make_pair(r + s, t);
    }
    sort(a.begin(), a.end(), cmp);
}

void solve() {
    ll min_time = a[0].first;
    ll cur_time = a[0].first;
    for (int i = 1; i < n; i++) {
        cur_time += (ll)(a[i].first + a[i - 1].second);
        min_time += cur_time;
    }
    printf("%lld\n", min_time);
}

int main(void) {
    init();
    solve();
    return 0;
}

Topics: Algorithm greedy algorithm