E. Convention(二分)

探讨了在给定牛群数量、车辆数量及车辆容量的情况下,如何通过优化调度策略,最小化牛群最大等待时间的问题。采用二分查找算法,结合贪心策略分配牛群到车辆,实现了最小化最大等待时间的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://codeforces.com/group/NVaJtLaLjS/contest/238203/problem/E

E. Convention

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Cows from all over the world are arriving at the local airport to attend the convention and eat grass. Specifically, there are NN cows arriving at the airport (1≤N≤1051≤N≤105) and cow ii arrives at time titi (0≤ti≤1090≤ti≤109). Farmer John has arranged MM (1≤M≤1051≤M≤105) buses to transport the cows from the airport. Each bus can hold up to CC cows in it (1≤C≤N1≤C≤N). Farmer John is waiting with the buses at the airport and would like to assign the arriving cows to the buses. A bus can leave at the time when the last cow on it arrives. Farmer John wants to be a good host and so does not want to keep the arriving cows waiting at the airport too long. What is the smallest possible value of the maximum waiting time of any one arriving cow if Farmer John coordinates his buses optimally? A cow's waiting time is the difference between her arrival time and the departure of her assigned bus.

It is guaranteed that MC≥NMC≥N.

Input

The first line contains three space separated integers NN, MM, and CC. The next line contains NN space separated integers representing the arrival time of each cow.

Output

Please write one line containing the optimal minimum maximum waiting time for any one arriving cow.

Example

input

Copy

6 3 2
1 1 10 14 4 3

output

Copy

4

题目大意:

给n头牛,m辆车,c车最多载牛数。再给n头牛的到达时间,等待时间=上车时间-到达时间;

车在任意一头牛到达后都可出发。求让牛最小的最大等待时间。

题目分析:

题中的这句话是关键:Please write one line containing the optimal minimum maximum waiting time for any one arriving cow.

最小的最大等待时间。暗示二分。二分解决最大化最小值等问题。

那么二分答案,在写个check 当车坐满人后或等待时间大于二分答案,则ans++;

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005;
int n,m,c;int a[N];
int check(int x)
{
	int s=1;int last=0;
	for(int i=0;i<n;i++)
	{
		if(i-last>=c||a[i]-a[last]>x)
		{
			s++;
			last=i;
		}
	}
	return m>=s;
}
int main()
{
	cin>>n>>m>>c;
	for(int i=0;i<n;i++)cin>>a[i];
	sort(a,a+n);
	int l=0,r=a[n-1]-a[0],mid;
	while(l<=r)
	{
		mid=(l+r)>>1;
		if(check(mid))r=mid-1;
		else l=mid+1;
	}
	cout<<r+1<<endl;
} 

 

第一部分: # -*- coding: gbk -*- import os import glob import cv2, math import numpy as np from PIL import Image import random import numpy as np import h5py import os from PIL import Image import scipy.io IMG_EXTENSIONS = [ '.jpg', '.JPG', '.jpeg', '.JPEG', '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', ] #图片转矩阵 def pil_to_np(img_PIL): '''Converts image in PIL format to np.array. From W x H x C [0...255] to C x W x H [0..1] ''' ar = np.array(img_PIL) if len(ar.shape) == 3: ar = ar.transpose(2, 0, 1) else: ar = ar[None, ...] return ar.astype(np.float32) / 255. #矩阵转图片 def np_to_pil(img_np): '''Converts image in np.array format to PIL image. From C x W x H [0..1] to W x H x C [0...255] ''' ar = np.clip(img_np * 255, 0, 255).astype(np.uint8) if img_np.shape[0] == 1: ar = ar[0] else: ar = ar.transpose(1, 2, 0) return Image.fromarray(ar) # 判断文件夹中是否有以上类型图片,没有则返回0 def is_image_file(filename): #如果不都为空、0、false,则any()返回true return any(filename.endswith(extension) for extension in IMG_EXTENSIONS) #返回文件夹内文件绝对路径组成的列表 def make_dataset(dir): images = [] assert os.path.isdir(dir), '%s is not a valid directory' % dir # os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]]) 通过在目录树中游走输出在目录中的文件名,top返回三项(root,dirs,files),分别代表: # 当前正在遍历的这个文件夹的本身的地址; list类型,内容是该文件夹中所有的目录的名字(不包括子目录); list类型,内容是该文件夹中所有的文件(不包括子目录) for root, _, fnames in sorted(os.walk(dir)): for fname in fnames: #print(fname) #拼接出图片的地址,并加入到images列表 path = os.path.join(root, fname) images.append(path) return images def hazy_simu(img_name,depth_or_trans_name,save_dir,pert_perlin = 0,airlight=0.76,is_imdepth=1,visual_range = [0.05, 0.1, 0.2, 0.5, 1]): """ This is the function for haze simulation with the parameters given by the paper: HAZERD: an outdoor scene dataset and benchmark for single image dehazing IEEE Internation Conference on Image Processing, Sep 2017 The paper and additional information on the project are available at: https://labsites.rochester.edu/gsharma/research/computer-vision/hazerd/ If you use this code, please cite our paper. IMPORTANT NOTE: The code uses the convention that pixel locations with a depth value of 0 correspond to objects that are very far and for the simulation of haze these are placed a distance of 2 times the visual range. Authors: Yanfu Zhang: yzh185@ur.rochester.edu Li Ding: l.ding@rochester.edu Gaurav Sharma: gaurav.sharma@rochester.edu Last update: May 2017 python version update : Aug 2021 Authors : Haoying Sun : 1913434222@qq.com parse inputs and set default values Set default parameter values. Some of these are used only if they are not passed in :param img_name: the directory and name of a haze-free RGB image, the name should be in the format of ..._RGB.jpg :param depth_name: the corresponding directory and name of the depth map, in .mat file, the name should be in the format of ..._depth.mat :param save_dir: the directory to save the simulated images :param pert_perlin: 1 for adding perlin noise, default 0 :param airlight: 3*1 matrix in the range [0,1] :param visual_range: a vector of any size :return: image name of hazy image """
03-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值