Codeforces 551C - GukiZ hates Boxes (二分 + 贪心)

来源:互联网 发布:菜鸟商城源码 编辑:程序博客网 时间:2024/04/28 12:53

题意

有n堆东西,m个人,每个人在每一秒只能选择走到下一堆或者拿掉一个东西,问拿掉全部东西最少要多久。

思路

二分时间。

Check的时候,我们可以先拿出一个人,走到最近的非零堆,然后尽可能地搬东西,重复m次,看能不能搬完。

代码里的Check写得有点丑( TДT)

代码

#include <stack>#include <cstdio>#include <list>#include <cassert>#include <set>#include <fstream>#include <iostream>#include <string>#include <sstream>#include <vector>#include <queue>#include <functional>#include <cstring>#include <algorithm>#include <cctype>//#pragma comment(linker, "/STACK:102400000,102400000")#include <string>#include <map>#include <cmath>//#include <ext/pb_ds/assoc_container.hpp>//#include <ext/pb_ds/hash_policy.hpp>using namespace std;//using namespace __gnu_pbds;#define LL long long#define ULL unsigned long long#define SZ(x) (int)x.size()#define Lowbit(x) ((x) & (-x))#define MP(a, b) make_pair(a, b)#define MS(arr, num) memset(arr, num, sizeof(arr))#define PB push_back#define X first#define Y second#define ROP freopen("input.txt", "r", stdin);#define MID(a, b) (a + ((b - a) >> 1))#define LC rt << 1, l, mid#define RC rt << 1|1, mid + 1, r#define LRT rt << 1#define RRT rt << 1|1#define FOR(i, a, b) for (int i=(a); (i) < (b); (i)++)#define FOOR(i, a, b) for (int i = (a); (i)<=(b); (i)++)const double PI = acos(-1.0);const int INF = 0x3f3f3f3f;const double eps = 1e-4;const int MAXN = 1e5+10;const int MOD = 8e4+7;const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };const int seed = 131;int cases = 0;typedef pair<int, int> pii;int tmp[MAXN], arr[MAXN], n, pos, m;void Solve(LL mid){    bool first = true;    LL rem = mid;    while (true)    {        if (pos == n) return;        while (!tmp[pos])        {            pos++;            if (!first) rem--;            if (pos == n) return;        }        if (first) rem -= (pos+1);        first = false;        if (rem <= 0) return;        if (tmp[pos] > rem)        {            tmp[pos] -= rem;            break;        }        else        {            rem -= tmp[pos];            pos++;            rem--;        }    }}bool Check(LL mid){    pos = 0;    copy(arr, arr+n, tmp);    for (int i = 0; i < m; i++)        Solve(mid);    return pos == n;}int main(){    //ROP;    scanf("%d%d", &n, &m);    for (int i = 0; i < n; i++) scanf("%d", &arr[i]);    LL l = 0, r = (1ll<<60);    while (l < r)    {        LL mid = MID(l, r);        if (Check(mid)) r = mid;        else l = mid+1;    }    printf("%I64d\n", r);    return 0;}
0 0
原创粉丝点击