2019 Multi-University Training Contest 1 D.Vacation 二分或者思维

Vacation

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3015    Accepted Submission(s): 1404
Special Judge

 

Problem Description

Tom and Jerry are going on a vacation. They are now driving on a one-way road and several cars are in front of them. To be more specific, there are n cars in front of them. The ith car has a length of li, the head of it is si from the stop-line, and its maximum velocity is vi. The car Tom and Jerry are driving is l0 in length, and s0from the stop-line, with a maximum velocity of v0.
The traffic light has a very long cycle. You can assume that it is always green light. However, since the road is too narrow, no car can get ahead of other cars. Even if your speed can be greater than the car in front of you, you still can only drive at the same speed as the anterior car. But when not affected by the car ahead, the driver will drive at the maximum speed. You can assume that every driver here is very good at driving, so that the distance of adjacent cars can be kept to be 0.
Though Tom and Jerry know that they can pass the stop-line during green light, they still want to know the minimum time they need to pass the stop-line. We say a car passes the stop-line once the head of the car passes it.
Please notice that even after a car passes the stop-line, it still runs on the road, and cannot be overtaken.

 

 

Input

This problem contains multiple test cases.
For each test case, the first line contains an integer n (1≤n≤105,∑n≤2×106), the number of cars.
The next three lines each contains n+1 integers, li,si,vi (1≤si,vi,li≤109). It's guaranteed that si≥si+1+li+1,∀i∈[0,n−1]

 

 

Output

For each test case, output one line containing the answer. Your answer will be accepted if its absolute or relative error does not exceed 10−6.
Formally, let your answer be a, and the jury's answer is b. Your answer is considered correct if |a−b|max(1,|b|)≤10−6.
The answer is guaranteed to exist.

 

 

Sample Input

 

1 2 2 7 1 2 1 2 1 2 2 10 7 1 6 2 1

 

 

Sample Output

 

3.5000000000 5.0000000000

 

 

Source

2019 Multi-University Training Contest 1

 

 

Recommend

We have carefully selected several similar problems for you:  6730 6729 6728 6727 6726 

 

题意:

有n+1辆车,属性有长度l,距离终点的距离s,速度v,问你最末尾的车偷到达终点的时间?

分析:

思维:

如果距离终点远的车如果能赶上前面的车,则和前面的车粘在一起,则时间就变为黏在一起的车到终点的时间(注意是一起走的,所以直接算时间就行)。

二分:

二分枚举一下时间,判断是否可以到即可

 

 

#include<bits/stdc++.h>
using namespace std;
#define maxn 500005
typedef long long ll;
const ll MOD=1e9+7;
int n;
ll l[maxn],s[maxn],v[maxn];
ll sum[maxn];
 
 
int main()
{
    while(scanf("%d",&n)!=-1)
	{
		n++;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&l[i]);
			sum[i]=sum[i-1]+l[i];
			
		}
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&s[i]);
		}
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&v[i]);
		}
		
		double ans=s[1]/v[1];
		
		for(int i=1;i<=n;i++)
		{
			ans=max(ans,1.0*(s[i]+sum[i]-sum[1])/(1.0*v[i]));
		}
		printf("%.6f\n",ans);
	}
    return 0;
}

 

#include<bits/stdc++.h>
using namespace std;
#define maxn 500005
typedef long long ll;
const ll MOD=1e9+7;
const double eps=1e-8;
int n;
double l[maxn],s[maxn],v[maxn],temp[maxn];
double sum[maxn];

int dcmp(double x)
{
    if(fabs(x)<eps)
        return 0;
    else if(x>0)
        return 1;
    else
        return -1;
}

int check(double mid)
{
    for(int i=1; i<=n; i++)
        temp[i]=s[i];
    for(int i=n; i>=1; i--)
    {
        temp[i]-=mid*v[i];
        if(i!=n)
            temp[i]=max(temp[i],temp[i+1]+l[i+1]);
    }
    if(dcmp(temp[1])<=0)
        return 1;
    else
        return 0;
}


int main()
{
    while(scanf("%d",&n)!=-1)
    {
        n++;
        for(int i=1; i<=n; i++)
        {
            scanf("%lf",&l[i]);
            sum[i]=sum[i-1]+l[i];

        }
        for(int i=1; i<=n; i++)
        {
            scanf("%lf",&s[i]);
        }
        for(int i=1; i<=n; i++)
        {
            scanf("%lf",&v[i]);
        }
        double ans;

        double l=0,r=1e9+10;
        for(int i=0; i<=100; i++)
        {



            double mid=(l+r)/2;
            if(check(mid))
            {
                ans=mid;
                r=mid;
            }
            else
                l=mid;

        }
        printf("%.6f\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值