Saturday, January 27, 2024

[Hackerearth] Text Wrap

 

Problem Link    : Text Wrap
Category        : Add Hoc
Contest         : January Easy '24

#include "bits/stdc++.h"
using namespace std;
#define int     long long int
#define endl    '\n'
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout.precision(12);
 
    bool FILEIO = 1;
    if (FILEIO and fopen("d3.txt", "r")) {
        freopen("d3.txt", "r", stdin);
    }
 
    int n, m;
    cin >> n >> m;
    deque<int> lines(n);
    for (int &line : lines) {
        cin >> line;
    }
    int lo = 0;
    int hi = 1e16;
    int ans = 0;
    while (lo <= hi) {
        int mid = lo + hi >> 1;
        int linesRequired = 0;
        deque<int> tmp = lines;
        while (tmp.size()) {
            int sum = tmp.front(); tmp.pop_front();
            if (sum > mid) {
                linesRequired = m + 1;
                break;
            }
            linesRequired++;
            while (tmp.size() and sum + 1 + tmp.front() <= mid) {
                sum += 1 + tmp.front();
                tmp.pop_front();
            }
        }
        if (linesRequired <= m) {
            ans = mid;
            hi = mid - 1;
        } else {
            lo = mid + 1;
        }
    }
    cout << ans << endl;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.