首页 > 其他分享 >Meeting on the Line(贪心,二分,三分)

Meeting on the Line(贪心,二分,三分)

时间:2022-09-27 22:45:04浏览次数:64  
标签:const int double maxn Line include Meeting check 贪心

Meeting on the Line(贪心算法)(二分,三分)

题目大意:类似(实数版)货仓选址,给你n个位置(不用再这n个位置中选,可以任意选实数),再给你这n个位置出发的准备时间(可以转化成距离来看),求一个位置,到每个位置的最大值最小

AC代码(贪心思想,类似货仓选址)

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn=100010;
const int INF=0x3f3f3f3f;
int x[maxn],t[maxn];
int main(void)
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int N;scanf("%d",&N);
		for(int i=0;i<N;i++)scanf("%d",&x[i]);
		int l=INF,r=-INF;
		for(int j=0;j<N;j++)
		{
			scanf("%d",&t[j]);
			l=min(l,x[j]-t[j]);//把多出来的准备时间看成位置,那么就可以延长他的距离
			r=max(r,x[j]+t[j]);//最后找出来最左边的位置和最右边的位置,取个中间的值,就可以得到到所有位置的最大值满足最小
		}
		printf("%.1lf\n",1.0*(l+r)/2);
	}
	return 0;
}

AC代码(三分)

#include <cstdio>//三分有个特点,就是要把区间除以三,导致很多的实数区间再除以三之后会有很多的小数位
#include <iostream>//所以三分在输出答案的时候要特别注意,输出的小数点的位数一定要比答案给的精度多两三位
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
const double e=1e-8;
const int maxn=200010;
const int INF=0x3f3f3f3f;
double pos[maxn],t[maxn];
int N;
double check(double x)
{
	double Max=0;
	for(int i=0;i<N;i++)
		Max=max(Max,abs(x-pos[i])+t[i]);
	return Max;
}
int main(void)
{
	int T;scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&N);
		long double l=0,r=INF;
		for(int i=0;i<N;i++)
			scanf("%lf",&pos[i]);
		for(int i=0;i<N;i++)
			scanf("%lf",&t[i]);
		while(r-l>e)//经典三分
		{
			double midl=l+(r-l)/3;
			double midr=l+(r-l)/3*2;
			if(check(midr)>check(midl)) r=midr;
			else l=midl;
		}
		cout<<setprecision(7)<<r<<endl;//题目是要误差小于1e-6,所以输出1e-7的话基本就没什么问题了
	}
	return 0;
}

AC代码(二分)

//可以二分时间或者位置,这里二分位置,(时间和位置是可以相互转换的)

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
const double e=1e-7;//不太清楚为什么1e-8会超时,明明1e-7的时间很短
const int maxn=100010;
const int INF=0x3f3f3f3f;
int pos[maxn],t[maxn];
int N;
bool check(double x)
{
	double l=0,r=0;
	for(int i=0;i<N;i++)
	{
		if(x>pos[i])l=max(l,x-pos[i]+t[i]);
		else r=max(r,pos[i]+t[i]-x);
	}
	return r>=l;
}
int main(void)
{
	int T;scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&N);
		for(int i=0;i<N;i++)scanf("%d",&pos[i]);
		for(int j=0;j<N;j++)scanf("%d",&t[j]);
		double l=0,r=INF;
		while(r-l>e)
		{
			double mid=(r+l)/2;
			if(check(mid)) l=mid;
			else r=mid;
		}
		printf("%.7lf\n",r);
	}
	return 0;
}

标签:const,int,double,maxn,Line,include,Meeting,check,贪心
From: https://www.cnblogs.com/WUTONGHUA02/p/16736267.html

相关文章

  • 算法设计与分析课-实验-贪心
    算法设计与分析课贪心算法第一题最小延迟调度:贪心算法的基本思想:贪心算法的基本思想为从整体中找到每个小局部的最优解,并将所有局部最优解合并成整体的最优解。能够......
  • 货仓选址(贪心)(也可以二分)(还可以三分)
    货仓选址(贪心)题目大意:给你N个位置(在同一条轴上的),求出在这些位置中的一个位置,使得所有位置到这个位置的和最小利用贪心思想,如果选取的位置不在中心,那么向左或者向右移动......
  • C# Console.WriteLine()不在输出窗口显示内容
    以前调试代码的时候,用Console.Writeline(“xxxxx”)可以直接在VS的“输出”窗口显示要打印的东西,今天用VS2019跑代码的时候突然发现没有输出内容了,搞得我一脸懵逼。查了相......
  • ABC 244 C - Yamanote Line Game (交互题)
    https://atcoder.jp/contests/abc244/tasks/abc244_c题目大意:有两个人,分别叫做AB。给定一个数字,A先手,每个人可以从[1,2*n+1]这个范围内说出一个数字,说不出的人就输;我......
  • Pipeline 责任链
    publicclassMain{publicstaticvoidmain(String[]args){Pipelinep=newPipeline();p.addLast(newHandler1());p.addLast(new......
  • [luogu p8251] [NOI Online 2022 提高组] 丹钓战
    [P8251NOIOnline2022提高组]丹钓战-洛谷|计算机科学教育新生态(luogu.com.cn)容易发现对于一次查询\([L,R]\),\(L\)一定是第一个入栈的,也是成功的,答案至少为......
  • Hive beeline方式连接Could not open connection to the HS2 server(补充)
    想启动beeline客户端却发现连不上beeline-ujdbc:hive2://hadoop102:10000-natguigu参考了https://blog.csdn.net/aubekpan/article/details/93758340的博客发现了问......
  • The 2022 ICPC Asia Regionals Online Contest (II) B
    BNon-decreasingArray我们可以知道两个操作都是一样的那我们直接记dp[i][j]为前i个数选了j个数并且以ai为结尾的max状态转移直接枚举dp[k][j-1]+(a[i]-a[k])^2即可......
  • 贪心杂题
    \(Preface\)最近咋老考贪心嘞?我的数据结构呢?贪心这玩意确实重要。但是我蒟蒻,老做不出来,而且经常写挂某些需要用到一些小技巧的题。。。具体地,刷贪心题既是刷思维也是刷......
  • 关于使用conda create -n offline_per python=3.7时报错的问题
    为使用多个服务器节点进行训练offline_per,因为需要安装Atari,Mujoco,d4rl以及dm-control等,准备在多个节点上安装cond环境。但遇到一些问题:  经过研究,发现可能是默认......