D. As Fast As Possible (数学:公式推导)

本文解析了CodeForces竞赛中一道关于计算最短时间的问题。通过数学推导找到最优解,实现人与车辆的合理分配,确保所有人都能在最短时间内到达目的地。

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

  • 题目链接:http://codeforces.com/contest/701/problem/D
  • 题意:给你5个整数:人数n,起点到终点的距离 l,人行走的速度 v1,车的速度 v2,车的座位数 k。人和车同时从起点出发,车每次能载k个人,每个人只能坐一次车,忽略车掉头以及乘客上下车的时间,问所有人到终点所需要的最短时间?
  • 思路:因为是求所有人到终点的最短时间,所以,所有人应该是同时到达终点的。并且每个人都是一段路坐车,一段路走路,且坐车行进的距离都相等。
    • 设坐车的距离为 l1 ,则行走的距离就是 l - l1
    • 将每次坐车的人分组,则可以分成 cnt = (n+k-1)/k 组。
      • 第一组人的上车地点在 d1 = 0 处,车前进 l1 再返回
      • 设第二组人的上车地点为 d2 ,车前进 l1t1 = l1 / v2, 车返回时接到第二组人需 t2 = (l1 - t1*v1)/(v1+v2) 。则 d2 = v1*(t1+t2) = 2*v1*l1/(v1+v2)。接到第二组人以后再前进 l1
      • 设第三组人上车的地点为 d3,由于过程和接第二组人时相同,所以 d3 - d2 = d2 - d1
      • 由此得出规律:最后一组人上车的地点为 2*v1*l1*(cnt-1)/(v1+v2)。并且因为是最小时间,所以最后一组人直接在终点下车。即其上车地点又等于 l - l1。联立可求得 l1
    • ans = l1/v2 + (l - l1)/v1

#include <bits/stdc++.h>
#define pi acos(-1)
#define se second
#define fi first
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int INF = 1e9+7;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1e5 + 10 ;
const LL mod = 1e9+7;



int main()
{
    fastio;
    LL n, l, v1, v2, k;
    cin >> n >> l >> v1 >> v2 >> k;
    LL cnt = (n+k-1)/k;
    long double l1 = (long double)(l*(v1 + v2))/(2*(cnt-1)*v1 + v1 + v2);
    long double ans = l1/v2 + (l-l1)/v1;
    cout << fixed << setprecision(15) << ans << endl;
}
void ctmf( const unsigned char* const src, unsigned char* const dst, const int width, const int height, const int src_step_row, const int src_step_col, const int dst_step_row, const int dst_step_col, const int r ) { /* * Processing the image in vertical stripes is an optimization made * necessary by the limited size of the CPU cache. Each histogram is 544 * bytes big and therefore I can fit a limited number of them in the cache. * That number may sometimes be smaller than the image width, which would be * the number of histograms I would need without stripes. * * I need to keep histograms in the cache so that they are available * quickly when processing a new row. Each row needs access to the previous * row's histograms. If there are too many histograms to fit in the cache, * thrashing to RAM happens. * * To solve this problem, I figure out the maximum number of histograms * that can fit in cache. From this is determined the number of stripes in * an image. The formulas below make the stripes all the same size and use * as few stripes as possible. * * Note that each stripe causes an overlap on the neighboring stripes, as * when mowing the lawn. That overlap is proportional to r. When the overlap * is a significant size in comparison with the stripe size, then we are not * O(1) anymore, but O(r). In fact, we have been O(r) all along, but the * initialization term was neglected, as it has been (and rightly so) in B. * Weiss, "Fast Median and Bilateral Filtering", SIGGRAPH, 2006. Processing * by stripes only makes that initialization term bigger. * * Also, note that the leftmost and rightmost stripes don't need overlap. * A flag is passed to ctmf_helper() so that it treats these cases as if the * image was zero-padded. */ const int CACHE_SIZE = 256 * 1024; const int STRIPES = (int)ceil( (double) (width - 2 * r) / (CACHE_SIZE / sizeof(Histogram) - 2 * r) ); const int STRIPE_SIZE = (int)ceil( (double) ( width + STRIPES * 2 * r - 2 * r ) / STRIPES ); int i; for ( i = 0; i < width; i += STRIPE_SIZE - 2 * r ) { int stripe = STRIPE_SIZE; /* Make sure that the filter kernel fits into one stripe. */ if ( i + STRIPE_SIZE - 2 * r >= width || width - (i + STRIPE_SIZE - 2 * r) < 2 * r + 1 ) { stripe = width - i; } ctmf_helper( src + i*src_step_col, dst + i * dst_step_col, stripe, height, src_step_row, src_step_col, dst_step_row, dst_step_col, r, i == 0, stripe == width - i ); if ( stripe == width - i ) { break; } } }
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值