1 条题解

  • 0
    @ 2025-4-9 20:19:22

    题意分析

    这个问题描述了一个手册分配的场景,需要将若干本页数不同的宣传手册分配给多个学校。分配过程需要满足特定的数量规则和顺序规则。

    解题思路

    数量规则方面,每所学校分配的手册数量只能是总手册数除以学校数的向上取整(UIP)或向下取整(LIP),且优先将UIP数量的手册分配给编号较小的学校。在顺序规则方面,所有手册需要先按页数升序排序,页数相同时保持原始输入顺序,然后严格按照排序后的顺序依次分配给各学校,同时每所学校内部的手册要保持原始相对顺序。最终需要确定分配给指定目标学校的第一本手册的页数。这可以通过自定义比较函数实现稳定排序。然后计算UIPUIPLIPLIP的值,确定每所学校的分配数量,其中前booksbooks取余schoolsschools所学校分配UIPUIP本,其余分配LIPLIP本。接下来需要遍历所有学校,累加每所学校的分配数量,当到达目标学校时,输出排序后手册列表中对应位置的页数即可。整个过程需要注意数组索引的处理和边界条件的判断,确保能准确定位到目标学校的首本手册。

    代码实现

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int main() {
    	int schools, target_school, books;
    	while (cin >> schools >> target_school >> books) {
    		vector<int> pages(books);
    		for (int i = 0; i < books; ++i) {
    			cin >> pages[i];
    		}
    		
    		// Step 1: Sort the pages in ascending order
    		sort(pages.begin(), pages.end());
    		
    		// Step 2: Calculate the number of books per school
    		int lip = books / schools;
    		int uip = lip + 1;
    		int uip_schools = books % schools;
    		int lip_schools = schools - uip_schools;
    		
    		// Step 3: Distribute the books
    		vector<int> school_books(schools, 0);
    		int current_book = 0;
    		
    		// Distribute UIP books first
    		for (int i = 0; i < uip_schools; ++i) {
    			school_books[i] = uip;
    		}
    		
    		// Distribute LIP books next
    		for (int i = uip_schools; i < schools; ++i) {
    			school_books[i] = lip;
    		}
    		
    		// Step 4: Find the first book of the target school
    		int book_start = 0;
    		for (int i = 0; i < target_school; ++i) {
    			book_start += school_books[i];
    		}
    		
    		cout << pages[book_start] << endl;
    	}
    	return 0;
    }
    
    • 1

    信息

    ID
    969
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    2
    已通过
    1
    上传者