Gym

来源:互联网 发布:淘宝t恤100字好评评语 编辑:程序博客网 时间:2024/06/03 21:14

原题:

ACM ICPC 2016–2017, Northeastern European Regional Contest.
St. Petersburg – Barnaul – Tbilisi – Almaty, December 4, 2016
Problem F. Foreign Postcards
Input file: foreign.in
Output file: foreign.out
Fedor is an avid traveler. As a result of his hobby, he has gathered a big collection of postcards from
all over the world. Each postcard has a unique picture on the front side and some fields for address
information and text on the back side.
During one of the parties at Fedor’s house, he decided to show all his of postcards to the guests. To
achieve that, he wants to lay them all out on the table. Initially, all of his postcards are arranged in a
single stack that Fedor is holding in his hands. Unfortunately, some of the postcards in that stack can
be turned incorrectly — upside down. Ideally, Fedor would like all postcards on the table to lie with the
picture on top, but looking at every postcard and turning it over individually can be very time-consuming.
Instead, he came up with the following process:
1. Let nbe the number of postcards remaining in the stack in his hands. Fedor chooses a random
number kuniformly between 1 andn and takes topk postcards from the stack.
2. He looks at the topmost postcard among thesek postcards. If it is oriented in the wrong way, he
turns the whole stack ofk postcardsupside down together.
3. He then puts these kpostcards on the table without any further rotations.
4. If there are still some postcards remaining in the stack in his hands, Fedor goes back to step 1.
Of course, after all the postcards are on the table, there might still be some that lie back side up. What
is the expected number of such postcards?
Input
The input consists of a single line of “C” and “W” characters — i-th character corresponds toi-th postcard
in the stack, counting from the top of the stack. “C” means thati-th postcard is orientedcorrectly in the
initial stack, and “W” means thati-th postcard is oriented in thewrong way. The number of characters
is between 1 and 106inclusive.
Output
Output one real number — the expected number of incorrectly placed postcards on the table. The
absolute or relative error should not exceed 109.
Examples
foreign.in foreign.out
CWCC 1.0
WWCWCCW 2.333333333333
Page 8 of 15


题意:

       一副牌分正反面,堆在一起,每次随机从牌顶选择k张,如果第一张是反面就把所有牌翻面放在一边,重复在牌堆选牌直至所有牌被选完,求所有牌中反面的牌的期望。


思路:

      对所有牌求翻面的总和,Sum[i]为i~n的牌中所有选择的翻面牌的总和,如果s[i]==s[i+1],则Sum[i]=Sum[i+1],否则Sum[i]=(double)(len-i)*(len-1-i)/2.0-Sum[i+1];(即求Sum[i+1]的牌的正面和),

dp[i]=p*Sum[i]+p*Exp,Exp+=dp[i]。


#include <iostream>#include <iomanip>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <deque>#include <string>#include <cmath>#include <vector>#include <utility>#include <set>#include <map>#include <sstream>#include <climits>//#pragma comment(linker, "/STACK:1024000000,1024000000")#define pi acos(-1.0)#define INF 2147483647using namespace std;typedef long long ll;typedef pair <int,int > P;double dp[1000010];double Sum[1000010];string s;int main (){    freopen("foreign.in", "r", stdin);    freopen("foreign.out", "w", stdout);    cin>>s;    memset(dp,0,sizeof(dp));    memset(Sum,0,sizeof(Sum));    int len=s.size();    double p,Exp=0;    for(int i=len-2; i>=0; i--)    {        if(s[i]==s[i+1])            Sum[i]=Sum[i+1];        else            Sum[i]=(double)(len-i)*(len-1-i)/2.0-Sum[i+1];    }    for(int i=len-1; i>=0; i--)    {        p=1.0/(1.0*(len-i));        dp[i]=p*Sum[i]+p*Exp;        Exp+=dp[i];    }    printf("%.10lf\n",dp[0]);    return 0;}


原创粉丝点击