【链接】点击打开链接


【题意】


有一辆火车,它的长度为L,然后假设这辆车现在随机可能地出现在0..D之间,然后假设它已经耗光了油.
问你它需要走的期望距离是多少.
这里要走的距离指的是车里最近的加油站的距离
如果车覆盖了加油站那么它不用动.
一开始给你n个加油站的位置,同时0和D也有一个加油站

【题解】

把这些加油站按照x顺序排(0和D也有加油站);
然后看看x[i+1]和x[i]的间隔temp = x[i+1]-x[i];
然后如果间隔temp小于等于L,则直接跳过.
否则大于L的话;
则分成两段,在x[i]+L..X[i]+L+(temp-L)/2这一段
(要到左边x[i]那个加油站);
距离从0..(temp-L)/2
因为均匀分布则期望就为(temp-L)/4
然后X[i]+L+(temp-L)/2..x[i]+L+temp-L这一段也同样
(要到右边x[i+1]那个加油站)
也是从0..(temp-L)/2均匀分布
因此期望也是(temp-L)/4
然后这一段长度为(temp-L)/2,则对答案的贡献都为(temp-L)/(2*D)
因此答案递增2*(temp-L)/4 * (temp-L)/(2*D)
也即(temp-L)^2/(4*D);
double会溢出,要用long double

【错的次数】


2

【反思】


觉得自己的想法没问题,就要想想精度问题了>_<

【代码】

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <cstdlib>
#include <cmath>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb emplace_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)
#define sz(x) ((int) x.size()) typedef pair<int, int> pii;
typedef pair<LL, LL> pll; //mt19937 myrand(time(0));
//int get_rand(int n){return myrand()%n + 1;}
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e5; double D, L;
int n;
double x[N + 10]; int main() {
//Open();
//Close();
ios::sync_with_stdio(0);
cin >> D >> L >> n;
rep1(i, 1, n) cin >> x[i];
x[++n] = 0, x[++n] = D;
sort(x + 1, x + 1 + n);
long double ans = 0;
rep1(i, 1, n - 1) {
if (x[i + 1] - x[i] - L <= (1e-6)) continue;
long double temp = x[i + 1] - x[i];
temp -= L;
temp /= 2.0;
ans += (temp*temp) / D;
}
cout << fixed << setprecision(10) << ans << endl;
return 0;
}

【CS Round #43 C】Rectangle Partition的更多相关文章

  1. 【CS Round #43 B】Rectangle Partition

    [链接]https://csacademy.com/contest/round-43/task/rectangle-partition/ [题意] 水题 [题解] 横着过去,把相邻的边的宽记录下来. ...

  2. 【CS Round #43 E】Coprime Pairs

    [链接]点击打开链接 [题意] 让你选择n个数字,组成一个数组,使得这n个数字中恰好有k对,它们是互质的. [题解] 我们可以先找出前n个质数,那么接下来的问题就转化为,凑出rest = n*(n-1 ...

  3. 【CS Round #43 D】Bad Triplet

    [链接]点击打开链接 [题意] 给你n个点m条边的无权无向联通图; 让你找3个点A,B,C 使得A->B=B->C=A->C 这里X->Y表示点X到点Y的最短路长度. [题解] ...

  4. 【CS Round #43 A】Expected Dice

    [链接]https://csacademy.com/contest/round-43/task/expected-dice/ [题意] 大水题 [题解] 把36种可能的结果都存下来. 然后把重复出现的 ...

  5. 【CS round 34】Minimize Max Diff

    [题目链接]:https://csacademy.com/contest/round-34/task/minimize-max-diff/ [题意] 给你n个数字; 数组按顺序不下降; 让你删掉k个数 ...

  6. 【CS Round 34】Max Or Subarray

    [题目链接]:https://csacademy.com/contest/round-34/summary/ [题意] 让你找一个最短的连续子串; 使得这个子串里面所有数字or起来最大; [题解] 对 ...

  7. 【CS Round #36 (Div. 2 only) A】Bicycle Rental

    [题目链接]:https://csacademy.com/contest/round-36/task/bicycle-rental/ [题意] 让你从n辆车中选一辆车; 每一辆车有3个属性 1.到达车 ...

  8. 【CS Round #37 (Div. 2 only) D】Reconstruct Graph

    [Link]:https://csacademy.com/contest/round-37/task/reconstruct-graph/statement/ [Description] 给你一张图; ...

  9. 【CS Round #37 (Div. 2 only) B】Group Split

    [Link]:https://csacademy.com/contest/round-37/task/group-split/ [Description] 让你把一个数分成两个数a.b的和; (a,b ...

随机推荐

  1. Windows7下修改pip源

    以下列举三种方式的pip源配置: 1. 设置环境变量PIP_CONFIG_FILE指向pip.ini源配置文件,pip.ini文件内容如下: [global] index-url = http://m ...

  2. 10.cocos2dx C++为Sprite添加触摸事件监听器

    1.首先头文件定义事件处理的函数原型 private: bool onTouchBegan(Touch* tTouch,Event* eEvent);//手指按下事件 void onTouchMove ...

  3. SpringMVC的注解方式

    mvc-servlet.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  4. 洛谷P2818 天使的起誓

    题目描述 Tenshi非常幸运地被选为掌管智慧之匙的天使.在正式任职之前,她必须和其他新当选的天使一样要宣誓.宣誓仪式是每位天使各自表述自己的使命,他们的发言稿放在n个呈圆形排列的宝盒中.这些宝盒按顺 ...

  5. js函数的解析与执行过程

    function f(a,b,c){ alert(a);//函数字符串 alert(b); var b = 5; function a(){ } } f(1,2); //预处理 lexicalEnvi ...

  6. JSP_Learn

    // 解决中文乱码的问题String name = new String((request.getParameter("name")).getBytes("ISO-885 ...

  7. 【重构】C# VS 配置引用程序集的路径(分离exe和dll从指定路径调用)

    原文:[重构]C# VS 配置引用程序集的路径(分离exe和dll从指定路径调用) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/CocoWu892 ...

  8. 03005_SQL查询语句

    查询语句,在开发中使用的次数最多,此处使用“zhangwu” 账务表. 1.准备工作 (1)创建财务表: CREATE TABLE zhangwu ( id INT PRIMARY KEY AUTO_ ...

  9. WPF转换器

    1. 前文 在普遍的也业务系统中, 数据要驱动到操作的用户界面, 它实际储存的方式和表达方式会多种多样, 数据库存储的数字 0或1, 在界面用户看到显示只是 成功或失败, 或者存储的字符.或更多的格式 ...

  10. Resource Access Based on Multiple Credentials

    A collection of multiple user credentials each associated with one of multiple different users is ob ...