题意:有n张标有“C”或“F”的卡片。

1、随机取前k张(1<=k<=n)

2、若这k张的第一张为“C”,则不翻转,否则,全部翻转这k张。

3、然后处理剩下的n-k张

4、重复步骤1~3直至处理完所有卡片。

求处理后卡片W的个数期望。

分析:期望dp从后往前推。

1、对于最后一张卡片,无论为C还是W,最后的结果都是W个数为0,因此dp[len] = 0.

2、对于卡片i,k有(len - i + 1)种选择

若卡片i为C,则前k张卡片是不反转的,预处理前缀和。

k = 1, 1 / (len - i + 1) * (0 + dp[i + 1])

k = 2, 1 / (len - i + 1) * (前两张卡片中W的个数 + dp[i + 2])

.......

k = len - i + 1, 1 / (len - i + 1) * (前len - i + 1张卡片中W的个数)

若卡片i为W,同理也处理处前缀和。

提取公因数1 / (len - i + 1)后,后面的式子合并dp[i + 1] + dp[i + 2] +...+ dp[len]可以边算边处理出后缀和sumsuf[i]。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1000000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
char s[MAXN];
LL a1[MAXN], a2[MAXN];
LL sum1[MAXN], sum2[MAXN];
double sumsuf[MAXN], dp[MAXN];
int main(){
freopen("foreign.in", "r", stdin);
freopen("foreign.out", "w", stdout);
scanf("%s", s + 1);
int len = strlen(s + 1);
for(int i = 1; i <= len; ++i){
if(s[i] == 'W') a1[i] = a1[i - 1] + 1;
else a1[i] = a1[i - 1];
}
for(int i = 1; i <= len; ++i){
if(s[i] == 'C') a2[i] = a2[i - 1] + 1;
else a2[i] = a2[i - 1];
}
for(int i = 1; i <= len; ++i){
sum1[i] = sum1[i - 1] + a1[i];
sum2[i] = sum2[i - 1] + a2[i];
}
dp[len] = 0;
sumsuf[len] = 0;
for(int i = len - 1; i >= 1; --i){
LL tmp;
if(s[i] == 'C'){
tmp = sum1[len] - sum1[i - 1] - a1[i] * (len - i + 1);
}
else{
tmp = sum2[len] - sum2[i - 1] - a2[i] * (len - i + 1);
}
dp[i] = (1 / (double)(len - i + 1)) * (sumsuf[i + 1] + tmp);
sumsuf[i] = sumsuf[i + 1] + dp[i];
}
printf("%.12lf\n", dp[1]);
return 0;
}

  

Gym - 101190F Foreign Postcards (期望dp)的更多相关文章

  1. 【概率dp】【数学期望】Gym - 101190F - Foreign Postcards

    http://blog.csdn.net/DorMOUSENone/article/details/73699630

  2. 2016 ACM-ICPC NEERC F. Foreign Postcards (概率DP)

    2016 ACM-ICPC NEERC F. Foreign Postcards 题意:有一串由C.W组成的字符串,每次截取长度为k(1<=k<=n且k随机)的前缀,如果该前缀首位为W,则 ...

  3. 【BZOJ-1419】Red is good 概率期望DP

    1419: Red is good Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 660  Solved: 257[Submit][Status][Di ...

  4. [NOIP2016]换教室 D1 T3 Floyed+期望DP

    [NOIP2016]换教室 D1 T3 Description 对于刚上大学的牛牛来说, 他面临的第一个问题是如何根据实际情况中情合适的课程. 在可以选择的课程中,有2n节课程安排在n个时间段上.在第 ...

  5. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  6. 【BZOJ-4008】亚瑟王 概率与期望 + DP

    4008: [HNOI2015]亚瑟王 Time Limit: 20 Sec  Memory Limit: 512 MBSec  Special JudgeSubmit: 832  Solved: 5 ...

  7. 期望dp BZOJ3450+BZOJ4318

    BZOJ3450 概率期望DP f[i]表示到i的期望得分,g[i]表示到i的期望长度. 分三种情况转移: ① s[i]=‘x’:f[i]=f[i-1],g[i]=0 ② s[i]=‘o’:f[i]= ...

  8. HDU 4405 期望DP

    期望DP算是第一题吧...虽然巨水但把思路理理清楚总是好的.. 题意:在一个1×n的格子上掷色子,从0点出发,掷了多少前进几步,同时有些格点直接相连,即若a,b相连,当落到a点时直接飞向b点.求走到n ...

  9. POJ 2096 【期望DP】

    题意: 有n种选择,每种选择对应m种状态.每种选择发生的概率相等,每种选择中对应的每种状态发生的概率相等. 求n种选择和m种状态中每种至少发生一次的期望. 期望DP好别扭啊.要用倒推的方法. dp[i ...

随机推荐

  1. HTML5模板引擎 Thymeleaf 教程(转)

    原文:http://www.open-open.com/lib/view/open1383622135586.html Thymeleaf是一个XML/XHTML/HTML5模板引擎,可用于Web与非 ...

  2. listenTo - backbone.js

    listenToobject.listenTo(other, event, callback) 让 object 监听 另一个(other)对象上的一个特定事件.不使用other.on(event, ...

  3. JS中,跨域调用(本地)另一个项目的方法

    IP地址,因为是本地的项目,所以我一开始写的是127.0.0.1...,但不对.应该写本机的IP地址才对!

  4. 刷题19. Remove Nth Node From End of List

    一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...

  5. python爬虫处理在线预览的pdf文档

    引言 最近在爬一个网站,然后爬到详情页的时候发现,目标内容是用pdf在线预览的 比如如下网站: https://camelot-py.readthedocs.io/en/master/_static/ ...

  6. android 基础学习(6)-----sqlite3查看表结构

    原文:http://blog.csdn.net/richnaly/article/details/7831933 sqlite3查看表结构 在android下通过adb shell命令可以进入sqli ...

  7. [FBCTF2019]Products Manager

    基于约束的SQL攻击 一.知识点: 1.数据库字符串比较: 在数据库对字符串进行比较时,如果两个字符串的长度不一样,则会将较短的字符串末尾填充空格,使两个字符串的长度一致,比如,字符串A:[Strin ...

  8. Day9 - J - 吉哥系列故事——恨7不成妻 HDU - 4507

    单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=7*2 77=7 ...

  9. Day3-R-Aggressive cows POJ2456

    Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are locat ...

  10. 常用mac/unix/linux命令

    1.查询ip地址 ifconfig 2.查找服务器上应用程序的端口分配 grep telnet /etc/services (telnet) telnet使用TCP/UDP端口号23 grep dom ...