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

网页设置页数/总页数

Problem statement:

问题陈述:

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.

给定一个大小为N的整数A和一个整数B的数组。 高校图书馆有N个书包,第i本书有A [i]页。

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.

您必须将图书分配给B个学生,以便分配给学生的最大页面数最少。 一本书将分配给恰好一个学生。 每个学生必须至少分配一本书。 分配应该按连续的顺序进行,例如,不能为学生分配书1和书3,而跳过书2。计算并返回该最小可能数。 如果不可能进行有效分配,则返回-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,表示测试用例的数量。 接下来是对T个测试用例的描述:每个用例都以一个表示书数的正整数N开头。 第二行包含N个以空格分隔的正整数,表示每本书的页数。 第三行包含另一个整数B ,表示学生人数。

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.

对于每个测试用例,输出一行,其中包含每个学生必须为相应的测试用例阅读的最少页数。

Examples:

例子:

INPUT:
T=1
N=4
[10,20,30,40]
B=2

OUTPUT:	
60, one student assigned 60 pages and other 40.

INPUT:
T=1
N=4
[12 34 67 90]
B=2

OUTPUT:
113, one student assigned 113 pages and other 90.

Solution Approach (Binary Search Approach)

解决方法(二进制搜索方法)

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.

我们将使用一种逻辑,即可以按升序分配页面,因为一个学生可以拥有最大的页面数作为所有书籍的页面总和(在此实际上是不可能的,因为我们需要为每位候选人分配一些内容)并以最佳方式划分页面,我们需要将要分配给某位学生的不同书籍的所有页面中的最大值,而其余页面应分配给其他学生,以使分配给学生的最大页面数量为最低。

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.

因此,通过将start作为给定书的最大页数来解决二进制搜索开始和结束的限制,而end将是所有书中所有页数的总和。
我们将继续使用二进制搜索方法,并且每当以mid为中心时,我们都会检查这是否是可以最佳分配给B级学生的最大页面数。

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.

我们声明一个布尔函数isValid,该函数将检查传入的当前页数是否为not的有效情况,如果满足条件,则返回true,然后检查当前分配的页面和新分配的页面的最大值。
要检查的另一种条件是学生数量和书本数量,如果学生数量大于书本数量,则将false返回到isValid条件。

C++ Implementation:

C ++实现:

#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;
            }
            else
                st = 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[]))

    最坏情况下上述方法的时间复杂度: O(N * log(sum(book []))

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

    上述方法的空间复杂度为: O(1)



Also tagged in: Google, Codenation

还标记在: GoogleCodenation

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

问题来源:https://www.interviewbit.com/problems/allocate-books/

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

网页设置页数/总页数

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值