题解:P13315 [GCJ 2012 #1A] Password Problem

· · 题解

P13315 [GCJ 2012 #1A] Password Problem

题意

  1. 直接补完后面的密码(保证后面是正确的),如果前面是正确的就停止,否则重新输一遍(肯定正确)。

  2. 先退几位,再按第一种方式补完。

  3. 按一下回车,再重新输一遍。

思路

Code

#include<bits/stdc++.h>
#define int long long
#define f(i,j,k) for(int i=j;i<=k;i++)
#define F(i,j,k) for(int i=j;i>=k;i--)
char Buf[1<<23],*P1=Buf,*P2=Buf;
#define getchar() (P1==P2&&(P2=(P1=Buf)+fread(Buf,1,1<<23,stdin),P1==P2)?EOF:*P1++)
const int maxn=3e5+10;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
inline double readDouble(){
    double x=0; int f=1,cnt=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
    if(ch=='.'){
        ch=getchar();
        while (ch>='0'&&ch<='9') x+=(ch-48)/std::pow(10,cnt),ch=getchar(),cnt++;
    }
    return x*f;
}
inline void write(int x){
    if(x<0) {x=~(x-1); putchar('-');}
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
int T=read(),n,m;
double p[maxn],ans;
inline void solve(int x){
    printf("Case #%lld: ",x);
    n=read(); m=read(); p[0]=1;
    f(i,1,n) p[i]=readDouble(),p[i]*=p[i-1];
    ans=m+2;
    f(i,0,n){
        double tmp=p[i]*((n-i+m-i+1)*1.0)+(1.0-p[i])*((2*m+n-i*2+2)*1.0);
        ans=std::min(ans,tmp);
    }
    printf("%.7lf\n",ans);
}
inline void work(){f(i,1,T) solve(i);}
signed main(){work();return !!!!!("YZren");}