Codeforces 551C GukiZ hates Boxes 二分答案

题目链接

题意:

一共有n个空地(是一个数轴,从x=1 到 x=n),每个空地上有a[i]块石头

有m个学生

目标是删除所有石头

一开始所有学生都站在 x=0的地方

每秒钟每个学生都可以在原地删除一块石头,或者向 → 移动一格距离

问:删除所有石头的最短时间

案例解析:

3 2

1 0 2

第一个学生第一秒向→走,第二秒删a[1]的一块石头

第二个学生一直走到头,删掉a[3] ,所以第二个学生花费是 1+1+1+2 = 5

两个学生可以同时运动。

思路:

二分答案,设答案为x,即需要x秒来搬完石头

先拿一个学生来

总要有一个学生来搬最远的那堆石头的

先让这个学生走到尽头

这个学生拥有的时间就是x

那么走到尽头后,还剩下的时间用来搬最后一堆石头

如果这堆石头搬不完,那么这个学生就利用完了,换一个学生

如果搬的完  那么这个学生还剩下的时间可以用来搬前一堆石头 一直把这个学生利用完。

如果所有学生都利用完了石头还是搬不完,那么x秒肯定是不够的

否则x秒就是够的

#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
#include <queue>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9) pt(x / 10);
	putchar(x % 10 + '0');
}
typedef long long ll;
typedef pair<ll, ll> pii;
const int inf = 1e9;
const int N = 1e5 + 10;
int n, m;
int a[N], b[N];
bool ok(ll x) {
	for (int i = 1; i <= n; i++)b[i] = a[i];
	int top = n, tmp = m;
	while (tmp-->0 && top) {
		ll lef = x - top;
		while (lef && top) {
			if (b[top] == 0) { top--;continue; }
			if (b[top] <= lef) {
				lef -= b[top--];
			}
			else { b[top] -= (int)lef;lef = 0; }
		}
	}
	while (top && b[top] == 0)top--;//找到最后一个并且不是0的点
	return top == 0;
}
int main() {
	rd(n); rd(m);
	int d = 1;
	for (int i = 1; i <= n; i++) {
		rd(a[i]);
	}
	while (a[n] == 0)n--; //把最后的0删掉
	ll l = 1, r = 1e15, ans;
	while (l <= r) {
		ll mid = (l + r) >> 1;
		if (ok(mid)) {
			ans = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	pt(ans);
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-29 01:22:52

Codeforces 551C GukiZ hates Boxes 二分答案的相关文章

Codeforces 551C GukiZ hates Boxes(二分)

Problem C. GukiZ hates Boxes Solution: 假设最后一个非零的位置为K,所有位置上的和为S 那么答案的范围在[K+1,K+S]. 二分这个答案ans,然后对每个人尽量在ans时间内搬最多的砖.可以得出至少需要多少个人才能搬完.这个可以在O(n)的时间内利用贪心得到 只要判断需要的人数是否小于等于实际的人数就好了 时间复杂度O(nlogS) #include <bits/stdc++.h> using namespace std; const int N = 1

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

题意: 就是n个学生帮助教授搬箱子, 箱子分成m 堆, 每个学生每秒可以选择的两个操作 操作1,为从i堆迈向第i+1堆, 操作2,从i-1堆箱子中帮忙抱走一个箱子 问抱走所有箱子的最少时间为多少, 每个学生每秒可以同时行动 题解: 二分时间   然后再当前时间下,一个一个派出所有的学生,使其走到他能走的最远距离 代码: #include<stdio.h> int flag, value[100005], value2[100005], n, m; void find(long long int

CodeForces 551C GukiZ hates Boxes

题目链接:CodeForces 551C GukiZ hates Boxes 解题思路: 题目要求最短时间,因此我们可以先考虑最长时间,最长时间一定是一个学生从开始走到结束,每走到一处就把该处箱子搬空,所以最长时间等于走到最后一个有箱子格子的步数ed加箱子总数sum. 接下来二分所需时间,直接搜索最短时间,每次都对假设时间进行判断.每次判断都从第一格开始,因为要搬空一个格子的箱子一定需要学生走到该处,同时所有学生可以同时移动,所以从头到尾判断要搬空该处需要多少学生,再与有的学生数量进行比较. 代

Codeforces551C:GukiZ hates Boxes(二分+贪心)

Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way. In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1?≤?i?≤?n) containing ai boxes. Luckily, m stude

Codeforces 670D2 Magic Powder - 2 二分答案

Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai - how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinar

CodeForces 343C Read Time 【二分答案】+【贪心】

<题目链接> 题目大意: 一条水平的磁道上有n个磁头和m个待扫描的点,磁头可以左右互不干扰的移动去扫描点,每秒移动一个单位(也可以停留在原地),求这些磁头扫描完这些所有的点最少需要要花多少时间. 解题分析: 本题用二分答案和贪心求解,先二分出这些磁头扫描完所有的点所需的时间,然后用贪心策略去模拟每个磁头扫描这些点.对这些磁头从左向右分析,假设二分出的总时间为mid,在该条件下,每个磁头扫描点的最优情况毫无疑问是,在保证扫描到当前 最左边未被扫描到的点的情况下,向右扫描尽可能远的距离(对于这些磁

codeforces 551 C GukiZ hates Boxes

--睡太晚了...脑子就傻了-- 这个题想的时候并没有想到该这样-- 题意大概是有n堆箱子从左往右依次排列,每堆ai个箱子,有m个人,最开始都站在第一个箱子的左边, 每一个人在每一秒钟都必须做出两种选择中的一种:1若他的位置有箱子则搬走一个箱子,2往右走一步. 问把所有箱子都搞掉的最少时间-- 很显然二分一下答案,若为x秒,则每个人都有x秒,一个一个排出去搬,看是否能够搬完-- 我竟然没想到-- #include<map> #include<string> #include<

Codeforces 825D Suitable Replacement - 贪心 - 二分答案

You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbi

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store