C. Discrete Acceleration ——计录自己第一个独立AC的实数二分题

题目

  • 题意

**给你一个数组,不包含  但是要求你插入最少的数(随意大小),使得没有任何一个子数组的和为  。**
有一条长为 l l l 的道路,左边 0 0 0 位置有一辆车,右边 l l l 位置有一辆车,这中间有 n n n 个加速站,每次经过一个就会速度加一(一开始速度是 1 1 1 ),问你辆车相遇的最小时间

  • 思路

很明显所用时间具有单调性,因为时间少于答案就会不满足,就是碰不到面,时间多于最终答案,就会两车就会背道而驰。所以可以二分时间。
难点其实是check函数的编写,直接看代码吧

  • 代码
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=2e5+10,mod=1e9+7;
int a[N];
#define PII pair<int,int>
// #define x first
#define y second
#define PB push_back
#define eps 1e-6
int n,l;
bool check(double mid)
{
	
	double ld=0,rd=0,lt=mid,rt=mid;
	double speedl=1,speedr=1;
	for(int i=1;i<=n+1;i++){
		double dis=a[i]-a[i-1];
		if((dis/speedl-lt)>=eps){
			ld+=(lt)*speedl;break;
		}
		else{
			ld+=(a[i]-a[i-1]);
			lt-=(dis/speedl);
			speedl++;
		}
	}
	for(int i=n;i>=0;i--){
		double dis=a[i+1]-a[i];
		//cout<<"rt:"<<rt<<endl;
		if((dis/speedr-rt)>eps){
			rd+=(rt)*speedr;break;
		}
		else{
			rd+=(a[i+1]-a[i]);
			rt-=(dis/speedr);
			speedr++;
			//cout<<"rd: "<<rd<<endl;
		}
	}
	// cout<<mid<<endl;
	// cout<<ld<<" "<<rd<<endl;
	// cout<<endl;
	if((ld+rd-l)>=eps){
		return true;
	}
	else{
		return false;
	}
}
void solve()
{
	cin>>n>>l;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	a[n+1]=l;
	double L=0,R=l;
	while((R-L)>(eps)){
		double mid=(L+R)/2;
		if(check(mid)){
			R=mid;
		}
		else{
			L=mid;
		}
	}
	cout<<fixed<<setprecision(10)<<L<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while (t--)
		solve();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂的码泰君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值