跳到正文

休闲·娱乐

P11289

YYX_2013约 33 分钟
原文
文章信息与版本

文章信息

发布
2026年10月2日
收录
2026年10月2日
更新
2026年10月2日
篇幅
约 33 分钟

版本

  1. v1当前

P11289 我的代码处于Accepted与Judging的叠加态

推荐的写法
C++
#include<bits/stdc++.h>
using namespace std;

struct input{
    long long i, s, t;
};

bool cmp(const input &x, const input &y){
    return x.t < y.t;
}

struct fre{
    long long i;
    bool operator < (const fre &other) const {
        return i > other.i;
    }
};

struct busy{
    long long i, ft;
    bool operator < (const busy &other) const {
        if(ft != other.ft){
            return ft > other.ft;
        }
        return i > other.i;
    }
};

struct res{
    long long i;
    long long pi;
    bool operator<(const res &other) const {
        if (i != other.i) return i < other.i;
        return pi < other.pi;
    }
};

long long n, m;
input in[200010];
res ans[200010];

priority_queue<fre> free_pq;
priority_queue<busy> busy_pq;

int main(){
    cin >> n >> m;
    for(long long i = 1; i <= n; i++){
        in[i].i = i;
        cin >> in[i].s >> in[i].t;
    }
    sort(in + 1, in + n + 1, cmp);
    for(long long i = 1; i <= m; i++){
        free_pq.push(fre{i});
    }
    for(long long i = 1; i <= n; i++){
        while(!busy_pq.empty() && busy_pq.top().ft <= in[i].t){
            free_pq.push(fre{busy_pq.top().i});
            busy_pq.pop();
        }
        long long x, y;
        if(!free_pq.empty()){
            x = free_pq.top().i;
            free_pq.pop();
            y = in[i].t + in[i].s;
        }
        else{
            busy z = busy_pq.top();
            busy_pq.pop();
            x = z.i;
            y = z.ft + in[i].s;
        }
        ans[i].i  = x;
        ans[i].pi = in[i].i;
        busy_pq.push(busy{x, y});
    }
    sort(ans + 1, ans + n + 1);
    long long x = 1;
    for(long long i = 1; i <= m; i++){
        long long y = x;
        while(x <= n && ans[x].i == i){
            ++x;
        }
        cout << (x - y);
        for(long long j = y; j < x; j++){
            cout << ' ' << ans[j].pi;
        }
        cout << '\n';
    }
    return 0;
}
不推荐的写法
C++
// 引入 C++ 标准库的万能头文件,包含所有常用标准库
#include<bits/stdc++.h>
// 使用标准命名空间,避免重复书写 std::
using namespace std;
// 定义打印任务信息结构体,存储:文件原始编号、所需打印时长、命令下发时刻
struct The_Printing_Task_Information_Structure_containing_one_sixty_four_bit_Integer_named__The_Original_File_Serial_Number__and_two_sixty_four_bit_Integers_named__The_Required_Printing_Duration__and__The_Command_Issuing_Timestamp__Fields{
// 定义 64 位整数成员:文件原始编号
    long long The_Original_File_Serial_Number_Field;
// 定义 64 位整数成员:所需打印时长
    long long The_Required_Printing_Duration_Field;
// 定义 64 位整数成员:命令下发时刻
    long long The_Command_Issuing_Timestamp_Field;
};
// 定义比较函数,按命令下发时刻升序排列打印任务信息结构体
bool The_Comparator_Function_that_Orders_Printing_Task_Information_Structures_by__The_Command_Issuing_Timestamp__Ascendingly(const The_Printing_Task_Information_Structure_containing_one_sixty_four_bit_Integer_named__The_Original_File_Serial_Number__and_two_sixty_four_bit_Integers_named__The_Required_Printing_Duration__and__The_Command_Issuing_Timestamp__Fields &The_First_Printing_Task_Information_Structure, const The_Printing_Task_Information_Structure_containing_one_sixty_four_bit_Integer_named__The_Original_File_Serial_Number__and_two_sixty_four_bit_Integers_named__The_Required_Printing_Duration__and__The_Command_Issuing_Timestamp__Fields &The_Second_Printing_Task_Information_Structure){
// 返回第一个任务的下发时刻是否小于第二个任务的下发时刻
    return The_First_Printing_Task_Information_Structure.The_Command_Issuing_Timestamp_Field < The_Second_Printing_Task_Information_Structure.The_Command_Issuing_Timestamp_Field;
}
// 定义空闲打印机信息结构体,存储:打印机唯一编号
struct The_Idle_Printer_Information_Structure_containing_one_sixty_four_bit_Integer_named__The_Printer_Unique_Identification_Number__Field{
// 定义 64 位整数成员:打印机唯一编号
    long long The_Printer_Unique_Identification_Number_Field;
// 重载小于运算符,使优先队列按打印机编号升序排列(编号小的优先级高)
    bool operator < (const The_Idle_Printer_Information_Structure_containing_one_sixty_four_bit_Integer_named__The_Printer_Unique_Identification_Number__Field &The_Other_Idle_Printer_Information_Structure) const {
// 返回当前打印机编号是否大于另一个打印机编号,以实现小根堆效果
        return The_Printer_Unique_Identification_Number_Field > The_Other_Idle_Printer_Information_Structure.The_Printer_Unique_Identification_Number_Field;
    }
};
// 定义忙碌打印机信息结构体,存储:打印机唯一编号、打印机下次可用时刻
struct The_Busy_Printer_Information_Structure_containing_two_sixty_four_bit_Integers_named__The_Printer_Unique_Identification_Number__and__The_Printer_Next_Available_Timestamp__Fields{
// 定义 64 位整数成员:打印机唯一编号
    long long The_Printer_Unique_Identification_Number_Field;
// 定义 64 位整数成员:打印机下次可用时刻
    long long The_Printer_Next_Available_Timestamp_Field;
// 重载小于运算符,使优先队列按下次可用时刻升序、编号升序排列
    bool operator < (const The_Busy_Printer_Information_Structure_containing_two_sixty_four_bit_Integers_named__The_Printer_Unique_Identification_Number__and__The_Printer_Next_Available_Timestamp__Fields &The_Other_Busy_Printer_Information_Structure) const {
// 如果两个打印机的下次可用时刻不同
        if(The_Printer_Next_Available_Timestamp_Field != The_Other_Busy_Printer_Information_Structure.The_Printer_Next_Available_Timestamp_Field){
// 返回下次可用时刻较晚者优先级更低,使时刻较早者位于堆顶
            return The_Printer_Next_Available_Timestamp_Field > The_Other_Busy_Printer_Information_Structure.The_Printer_Next_Available_Timestamp_Field;
        }
// 若下次可用时刻相同,返回编号较大者优先级更低,使编号较小者位于堆顶
        return The_Printer_Unique_Identification_Number_Field > The_Other_Busy_Printer_Information_Structure.The_Printer_Unique_Identification_Number_Field;
    }
};
// 定义打印机任务分配结果结构体,存储:分配的打印机唯一编号、分配的文件原始编号
struct The_Printer_Task_Assignment_Result_Structure_containing_two_sixty_four_bit_Integers_named__The_Assigned_Printer_Unique_Identification_Number__and__The_Assigned_File_Original_Serial_Number__Fields{
// 定义 64 位整数成员:分配的打印机唯一编号
    long long The_Assigned_Printer_Unique_Identification_Number_Field;
// 定义 64 位整数成员:分配的文件原始编号
    long long The_Assigned_File_Original_Serial_Number_Field;
// 重载小于运算符,按打印机编号升序、文件原始编号升序排列
    bool operator<(const The_Printer_Task_Assignment_Result_Structure_containing_two_sixty_four_bit_Integers_named__The_Assigned_Printer_Unique_Identification_Number__and__The_Assigned_File_Original_Serial_Number__Fields &The_Other_Printer_Task_Assignment_Result_Structure) const {
// 如果分配的打印机编号不同
        if (The_Assigned_Printer_Unique_Identification_Number_Field != The_Other_Printer_Task_Assignment_Result_Structure.The_Assigned_Printer_Unique_Identification_Number_Field) return The_Assigned_Printer_Unique_Identification_Number_Field < The_Other_Printer_Task_Assignment_Result_Structure.The_Assigned_Printer_Unique_Identification_Number_Field;
// 否则按分配的文件原始编号升序排列
        return The_Assigned_File_Original_Serial_Number_Field < The_Other_Printer_Task_Assignment_Result_Structure.The_Assigned_File_Original_Serial_Number_Field;
    }
};
// 定义全局变量:待打印文件总数、可用打印机总数
long long The_Total_Number_of_Files_to_be_Printed, The_Total_Number_of_Available_Printers;
// 定义全局数组:按输入顺序存储打印任务信息结构体,最大数量 200010
The_Printing_Task_Information_Structure_containing_one_sixty_four_bit_Integer_named__The_Original_File_Serial_Number__and_two_sixty_four_bit_Integers_named__The_Required_Printing_Duration__and__The_Command_Issuing_Timestamp__Fields The_Array_of_Printing_Task_Information_Structures_Indexed_by_Input_Order[200010];
// 定义全局数组:按处理顺序存储打印机任务分配结果结构体,最大数量 200010
The_Printer_Task_Assignment_Result_Structure_containing_two_sixty_four_bit_Integers_named__The_Assigned_Printer_Unique_Identification_Number__and__The_Assigned_File_Original_Serial_Number__Fields The_Array_of_Printer_Task_Assignment_Result_Structures_Indexed_by_Processing_Order[200010];
// 定义优先队列:存储空闲打印机信息结构体,按打印机唯一编号升序排列
priority_queue<The_Idle_Printer_Information_Structure_containing_one_sixty_four_bit_Integer_named__The_Printer_Unique_Identification_Number__Field> The_Priority_Queue_of_Idle_Printer_Information_Structures_Ordered_by_Printer_Unique_Identification_Number_Ascendingly;
// 定义优先队列:存储忙碌打印机信息结构体,按下次可用时刻升序排列
priority_queue<The_Busy_Printer_Information_Structure_containing_two_sixty_four_bit_Integers_named__The_Printer_Unique_Identification_Number__and__The_Printer_Next_Available_Timestamp__Fields> The_Priority_Queue_of_Busy_Printer_Information_Structures_Ordered_by_Next_Available_Timestamp_Ascendingly;
// 主函数入口
int main(){
// 读入待打印文件总数和可用打印机总数
    cin >> The_Total_Number_of_Files_to_be_Printed >> The_Total_Number_of_Available_Printers;
// 循环读入每个文件的打印任务信息
    for(long long The_Current_File_Input_Sequence_Index = 1; The_Current_File_Input_Sequence_Index <= The_Total_Number_of_Files_to_be_Printed; The_Current_File_Input_Sequence_Index++){
// 设置当前文件的原始编号为输入顺序编号
        The_Array_of_Printing_Task_Information_Structures_Indexed_by_Input_Order[The_Current_File_Input_Sequence_Index].The_Original_File_Serial_Number_Field = The_Current_File_Input_Sequence_Index;
// 读入当前文件的所需打印时长和命令下发时刻
        cin >> The_Array_of_Printing_Task_Information_Structures_Indexed_by_Input_Order[The_Current_File_Input_Sequence_Index].The_Required_Printing_Duration_Field >> The_Array_of_Printing_Task_Information_Structures_Indexed_by_Input_Order[The_Current_File_Input_Sequence_Index].The_Command_Issuing_Timestamp_Field;
    }
// 按命令下发时刻升序排序所有打印任务信息结构体
    sort(The_Array_of_Printing_Task_Information_Structures_Indexed_by_Input_Order + 1, The_Array_of_Printing_Task_Information_Structures_Indexed_by_Input_Order + The_Total_Number_of_Files_to_be_Printed + 1, The_Comparator_Function_that_Orders_Printing_Task_Information_Structures_by__The_Command_Issuing_Timestamp__Ascendingly);
// 初始化所有打印机为空闲状态,加入空闲优先队列
    for(long long The_Current_Printer_Initialization_Index = 1; The_Current_Printer_Initialization_Index <= The_Total_Number_of_Available_Printers; The_Current_Printer_Initialization_Index++){
// 将当前编号的打印机作为空闲打印机信息结构体压入空闲优先队列
        The_Priority_Queue_of_Idle_Printer_Information_Structures_Ordered_by_Printer_Unique_Identification_Number_Ascendingly.push(The_Idle_Printer_Information_Structure_containing_one_sixty_four_bit_Integer_named__The_Printer_Unique_Identification_Number__Field{The_Current_Printer_Initialization_Index});
    }
// 按时间顺序处理每个打印任务
    for(long long The_Current_File_Processing_Index = 1; The_Current_File_Processing_Index <= The_Total_Number_of_Files_to_be_Printed; The_Current_File_Processing_Index++){
// 释放所有在当前任务下发时刻已经完成打印的忙碌打印机
        while(!The_Priority_Queue_of_Busy_Printer_Information_Structures_Ordered_by_Next_Available_Timestamp_Ascendingly.empty() && The_Priority_Queue_of_Busy_Printer_Information_Structures_Ordered_by_Next_Available_Timestamp_Ascendingly.top().The_Printer_Next_Available_Timestamp_Field <= The_Array_of_Printing_Task_Information_Structures_Indexed_by_Input_Order[The_Current_File_Processing_Index].The_Command_Issuing_Timestamp_Field){
// 将已完成任务的打印机编号作为空闲打印机压入空闲优先队列
            The_Priority_Queue_of_Idle_Printer_Information_Structures_Ordered_by_Printer_Unique_Identification_Number_Ascendingly.push(The_Idle_Printer_Information_Structure_containing_one_sixty_four_bit_Integer_named__The_Printer_Unique_Identification_Number__Field{The_Priority_Queue_of_Busy_Printer_Information_Structures_Ordered_by_Next_Available_Timestamp_Ascendingly.top().The_Printer_Unique_Identification_Number_Field});
// 从忙碌优先队列中弹出该打印机
            The_Priority_Queue_of_Busy_Printer_Information_Structures_Ordered_by_Next_Available_Timestamp_Ascendingly.pop();
        }
// 定义变量:选中的打印机唯一编号、计算出的打印机下次可用时刻
        long long The_Selected_Printer_Unique_Identification_Number, The_Calculated_Printer_Next_Available_Timestamp;
// 如果存在空闲打印机
        if(!The_Priority_Queue_of_Idle_Printer_Information_Structures_Ordered_by_Printer_Unique_Identification_Number_Ascendingly.empty()){
// 选择空闲优先队列堆顶的打印机编号(等待时间最短且编号最小)
            The_Selected_Printer_Unique_Identification_Number = The_Priority_Queue_of_Idle_Printer_Information_Structures_Ordered_by_Printer_Unique_Identification_Number_Ascendingly.top().The_Printer_Unique_Identification_Number_Field;
// 从空闲优先队列中弹出该打印机
            The_Priority_Queue_of_Idle_Printer_Information_Structures_Ordered_by_Printer_Unique_Identification_Number_Ascendingly.pop();
// 计算该打印机完成当前任务后的下次可用时刻 = 命令下发时刻 + 所需打印时长
            The_Calculated_Printer_Next_Available_Timestamp = The_Array_of_Printing_Task_Information_Structures_Indexed_by_Input_Order[The_Current_File_Processing_Index].The_Command_Issuing_Timestamp_Field + The_Array_of_Printing_Task_Information_Structures_Indexed_by_Input_Order[The_Current_File_Processing_Index].The_Required_Printing_Duration_Field;
        }
// 否则没有空闲打印机
        else{
// 从忙碌优先队列中取出下次可用时刻最早的打印机信息结构体
            The_Busy_Printer_Information_Structure_containing_two_sixty_four_bit_Integers_named__The_Printer_Unique_Identification_Number__and__The_Printer_Next_Available_Timestamp__Fields The_Busy_Printer_That_Will_Finish_Earliest = The_Priority_Queue_of_Busy_Printer_Information_Structures_Ordered_by_Next_Available_Timestamp_Ascendingly.top();
// 从忙碌优先队列中弹出该打印机
            The_Priority_Queue_of_Busy_Printer_Information_Structures_Ordered_by_Next_Available_Timestamp_Ascendingly.pop();
// 选中该打印机的唯一编号
            The_Selected_Printer_Unique_Identification_Number = The_Busy_Printer_That_Will_Finish_Earliest.The_Printer_Unique_Identification_Number_Field;
// 计算该打印机完成当前任务后的下次可用时刻 = 该打印机下次可用时刻 + 所需打印时长
            The_Calculated_Printer_Next_Available_Timestamp = The_Busy_Printer_That_Will_Finish_Earliest.The_Printer_Next_Available_Timestamp_Field + The_Array_of_Printing_Task_Information_Structures_Indexed_by_Input_Order[The_Current_File_Processing_Index].The_Required_Printing_Duration_Field;
        }
// 记录当前任务分配结果中的打印机唯一编号
        The_Array_of_Printer_Task_Assignment_Result_Structures_Indexed_by_Processing_Order[The_Current_File_Processing_Index].The_Assigned_Printer_Unique_Identification_Number_Field  = The_Selected_Printer_Unique_Identification_Number;
// 记录当前任务分配结果中的文件原始编号
        The_Array_of_Printer_Task_Assignment_Result_Structures_Indexed_by_Processing_Order[The_Current_File_Processing_Index].The_Assigned_File_Original_Serial_Number_Field = The_Array_of_Printing_Task_Information_Structures_Indexed_by_Input_Order[The_Current_File_Processing_Index].The_Original_File_Serial_Number_Field;
// 将该打印机以新的下次可用时刻压入忙碌优先队列
        The_Priority_Queue_of_Busy_Printer_Information_Structures_Ordered_by_Next_Available_Timestamp_Ascendingly.push(The_Busy_Printer_Information_Structure_containing_two_sixty_four_bit_Integers_named__The_Printer_Unique_Identification_Number__and__The_Printer_Next_Available_Timestamp__Fields{The_Selected_Printer_Unique_Identification_Number, The_Calculated_Printer_Next_Available_Timestamp});
    }
// 按打印机编号升序、文件原始编号升序排序所有任务分配结果
    sort(The_Array_of_Printer_Task_Assignment_Result_Structures_Indexed_by_Processing_Order + 1, The_Array_of_Printer_Task_Assignment_Result_Structures_Indexed_by_Processing_Order + The_Total_Number_of_Files_to_be_Printed + 1);
// 定义扫描结果数组的当前索引
    long long The_Current_Result_Array_Scanning_Index = 1;
// 遍历每台打印机,输出其打印的文件
    for(long long The_Current_Printer_Unique_Identification_Number_Being_Output = 1; The_Current_Printer_Unique_Identification_Number_Being_Output <= The_Total_Number_of_Available_Printers; The_Current_Printer_Unique_Identification_Number_Being_Output++){
// 记录当前打印机在结果数组中的起始索引
        long long The_Start_Index_of_Current_Printer_Results_in_Sorted_Result_Array = The_Current_Result_Array_Scanning_Index;
// 跳过所有分配给当前打印机的任务
        while(The_Current_Result_Array_Scanning_Index <= The_Total_Number_of_Files_to_be_Printed && The_Array_of_Printer_Task_Assignment_Result_Structures_Indexed_by_Processing_Order[The_Current_Result_Array_Scanning_Index].The_Assigned_Printer_Unique_Identification_Number_Field == The_Current_Printer_Unique_Identification_Number_Being_Output){
// 结果数组扫描索引自增
            ++The_Current_Result_Array_Scanning_Index;
        }
// 输出当前打印机打印的文件数量
        cout << (The_Current_Result_Array_Scanning_Index - The_Start_Index_of_Current_Printer_Results_in_Sorted_Result_Array);
// 遍历当前打印机的所有结果,输出文件原始编号
        for(long long The_Current_Result_Array_Index_for_Output = The_Start_Index_of_Current_Printer_Results_in_Sorted_Result_Array; The_Current_Result_Array_Index_for_Output < The_Current_Result_Array_Scanning_Index; The_Current_Result_Array_Index_for_Output++){
// 输出空格和文件原始编号
            cout << ' ' << The_Array_of_Printer_Task_Assignment_Result_Structures_Indexed_by_Processing_Order[The_Current_Result_Array_Index_for_Output].The_Assigned_File_Original_Serial_Number_Field;
        }
// 输出换行符,准备下一台打印机的输出
        cout << '\n';
    }
// 程序正常结束
    return 0;
}
原文