日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

网页设置页数/总页数_图书分配问题(分配最小页数)

發(fā)布時(shí)間:2025/3/11 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 网页设置页数/总页数_图书分配问题(分配最小页数) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

網(wǎng)頁(yè)設(shè)置頁(yè)數(shù)/總頁(yè)數(shù)

Problem statement:

問(wèn)題陳述:

Given an array of integers A of size N and an integer B. College library has N bags, the ith book has A[i] number of pages.

給定一個(gè)大小為N的整數(shù)A和一個(gè)整數(shù)B的數(shù)組。 高校圖書(shū)館有N個(gè)書(shū)包,第i本書(shū)有A [i]頁(yè)。

You have to allocate books to B number of students so that the maximum number of pages allocated to a student is minimum. A book will be allocated to exactly one student. Each student has to be allocated at least one book. Allotment should be in contiguous order, for example, A student cannot be allocated book 1 and book 3, skipping book 2. Calculate and return that minimum possible number. Return -1 if a valid assignment is not possible.

您必須將圖書(shū)分配給B個(gè)學(xué)生,以便分配給學(xué)生的最大頁(yè)面數(shù)最少。 一本書(shū)將分配給恰好一個(gè)學(xué)生。 每個(gè)學(xué)生必須至少分配一本書(shū)。 分配應(yīng)該按連續(xù)的順序進(jìn)行,例如,不能為學(xué)生分配書(shū)1和書(shū)3,而跳過(guò)書(shū)2。計(jì)算并返回該最小可能數(shù)。 如果不可能進(jìn)行有效分配,則返回-1 。

Input:

輸入:

The first line contains T denoting the number of test cases. Then follows a description of T test cases: Each case begins with a single positive integer N denoting the number of books. The second line contains N space-separated positive integers denoting the pages of each book. And the third line contains another integer B, denoting the number of students.

第一行包含T,表示測(cè)試用例的數(shù)量。 接下來(lái)是對(duì)T個(gè)測(cè)試用例的描述:每個(gè)用例都以一個(gè)表示書(shū)數(shù)的正整數(shù)N開(kāi)頭。 第二行包含N個(gè)以空格分隔的正整數(shù),表示每本書(shū)的頁(yè)數(shù)。 第三行包含另一個(gè)整數(shù)B ,表示學(xué)生人數(shù)。

Output:

輸出:

For each test case, output a single line containing the minimum number of pages each student has to read for the corresponding test case.

對(duì)于每個(gè)測(cè)試用例,輸出一行,其中包含每個(gè)學(xué)生必須為相應(yīng)的測(cè)試用例閱讀的最少頁(yè)數(shù)。

Examples:

例子:

INPUT: T=1 N=4 [10,20,30,40] B=2OUTPUT: 60, one student assigned 60 pages and other 40.INPUT: T=1 N=4 [12 34 67 90] B=2OUTPUT: 113, one student assigned 113 pages and other 90.

Solution Approach (Binary Search Approach)

解決方法(二進(jìn)制搜索方法)

We will use the logic that the pages can be assigned in increasing order manner since one student can have the maximum number of pages as the sum of pages of all the books (which is practically not possible here as we need to assign something to every candidate) and to divide the page in an optimal manner we need to have a max of all the pages of different books to be assigned to some student and remaining pages should be distributed to other students so that the maximum amount of pages assigned to a student is minimum.

我們將使用一種邏輯,即可以按升序分配頁(yè)面,因?yàn)橐粋€(gè)學(xué)生可以擁有最大的頁(yè)面數(shù)作為所有書(shū)籍的頁(yè)面總和(在此實(shí)際上是不可能的,因?yàn)槲覀冃枰獮槊课缓蜻x人分配一些內(nèi)容)并以最佳方式劃分頁(yè)面,我們需要將要分配給某位學(xué)生的不同書(shū)籍的所有頁(yè)面中的最大值,而其余頁(yè)面應(yīng)分配給其他學(xué)生,以使分配給學(xué)生的最大頁(yè)面數(shù)量為最低。

So the constraint for binary search start and end is solved by taking start as the max of pages of the given book and end will be the sum of all the pages from all books.
We will keep using the binary search method and each time we take mid as the pivot we check if that is the maximum number of pages which can be assigned to the B number of student optimally.

因此,通過(guò)將start作為給定書(shū)的最大頁(yè)數(shù)來(lái)解決二進(jìn)制搜索開(kāi)始和結(jié)束的限制,而end將是所有書(shū)中所有頁(yè)數(shù)的總和。
我們將繼續(xù)使用二進(jìn)制搜索方法,并且每當(dāng)以mid為中心時(shí),我們都會(huì)檢查這是否是可以最佳分配給B級(jí)學(xué)生的最大頁(yè)面數(shù)。

We declare a bool function isValid which will check if the current number of pages that we pass into it is a valid case of not is it satisfy the condition then we return true and then check the maximum of currently assigned pages and new assigned pages.
One more condition to check is the number of students and the number of books if the number of students is greater than the number of books then we return false to isValid condition.

我們聲明一個(gè)布爾函數(shù)isValid,該函數(shù)將檢查傳入的當(dāng)前頁(yè)數(shù)是否為not的有效情況,如果滿足條件,則返回true,然后檢查當(dāng)前分配的頁(yè)面和新分配的頁(yè)面的最大值。
要檢查的另一種條件是學(xué)生數(shù)量和書(shū)本數(shù)量,如果學(xué)生數(shù)量大于書(shū)本數(shù)量,則將false返回到isValid條件。

C++ Implementation:

C ++實(shí)現(xiàn):

#include <bits/stdc++.h> using namespace std;typedef long long ll;// boolean function to check if the num // pages is valid partition or not bool isValid(ll book[], ll n, ll sum, ll B) {// if number of books is less than number of student.if (n < B) return false;else {// initiailse cur for number of pages // that student can have.ll cur = 0; // initialise number student count for // assigned number of pages.ll cnt = 1; for (ll i = 0; i < n; i++) {cur += book[i];// if(cur number of pages is greater than num number // of pages that one student can have then we incrase // the number of student by one and start again with // current number of pages.if (cur > sum) {cnt++;cur = book[i];// if number of student is greater than B than // return false.if (cnt > B) return false;}}// if allocation is possible then return true.if (cnt <= B) return true;}return false; }int main() {ll t;cout << "Enter number of test cases: ";cin >> t;while (t--) {ll n;cout << "Enter number of books: ";cin >> n;ll book[n];cout << "Enter book pages: ";for (ll i = 0; i < n; i++)cin >> book[i];ll B;cout << "Enter number of students: ";cin >> B;// initialise st for binary search as // the maximum value among the book.ll st = *max_element(book, book + n); // initialisze end for binary search as // the sum of all the values of books.ll end = accumulate(book, book + n, 0); ll ans = INT_MAX;while (st <= end) {// find mid of binary search.ll mid = st + (end - st) / 2; // check for valid condition then assign the answer.if (isValid(book, n, mid, B)) {ans = mid;end = mid - 1;}elsest = mid + 1;}if (ans == INT_MAX) {cout << "Allocation not possible: ";cout << -1 << "\n";}else {cout << "Minimum number of pages: ";cout << ans << "\n";}}return 0; }

Output:

輸出:

Enter number of test cases: 4 Enter number of books: 4 Enter book pages: 10 20 30 40 Enter number of students: 2 Minimum number of pages: 60 Enter number of books: 4 Enter book pages: 12 34 67 90 Enter number of students: 2 Minimum number of pages: 113 Enter number of books: 5 Enter book pages: 25 46 28 49 24 Enter number of students: 4 Minimum number of pages: 71 Enter number of books: 3 Enter book pages: 20 20 20 Enter number of students: 3 Minimum number of pages: 20
  • Time Complexity for above approach in worst case: O(N*log(sum(book[]))

    最壞情況下上述方法的時(shí)間復(fù)雜度: O(N * log(sum(book []))

  • Space Complexity for the above approach is: O(1)

    上述方法的空間復(fù)雜度為: O(1)



Also tagged in: Google, Codenation

還標(biāo)記在: Google , Codenation

Problem source: https://www.interviewbit.com/problems/allocate-books/

問(wèn)題來(lái)源:https://www.interviewbit.com/problems/allocate-books/

翻譯自: https://www.includehelp.com/icp/book-allocation-problem-allocate-minimum-number-of-pages.aspx

網(wǎng)頁(yè)設(shè)置頁(yè)數(shù)/總頁(yè)數(shù)

總結(jié)

以上是生活随笔為你收集整理的网页设置页数/总页数_图书分配问题(分配最小页数)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。