您的位置:首页 > 其它

[置顶] Gym 101190F Foreign Postcards

2017-08-29 19:29 393 查看
题意和转移就不讲了,讲讲比较 迷 的一点吧。

比赛结束之前,WA好多遍也没过,,

我开始还以为状态转移不对,或者写搓了,但是队友坚持查错,最终发现了这些有趣的地方。

目前只能怀疑和编译器有关系。

这样写是正确的,可以AC:

dp[0][0]=s[0]=='C'?1:0;

这样写是不行的,27组测试数据处会WA:

(s[0]=='C')?dp[0][0]=1:dp[0][0]=0;

还有,if{}else{}也不行,27组测试数据处会WA:

if (s[0]=='C') {
dp[0][0]=1.0;
} else {
dp[0][0]=0.0;
}
先贴上AC代码:

#include<map>
#include<set>
#include<list>
#include<stack>
#include<deque>
#include<queue>
#include<vector>
#include<sstream>
#include<iomanip>
#include<iostream>
#include<math.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<functional>
#define CPY(A,B)memcpy(A,B,sizeof(A))
#define fin freopen("in.txt","r",stdin)
#define fout freopen("out.txt","w",stdout)
typedef long long LL;
typedef unsigned long long uLL;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
const LL INFF=0x3f3f3f3f3f3f3f3fLL;
const double OO=1e20;
const double EPS=1e-9;
const double PI=acos (-1.0);
int dx[]= {0,1,0,-1};
int dy[]= {1,0,-1,0};
using namespace std;
int gcd (const LL &a,const LL &b) {
return b==0?a:gcd (b,a%b);
}
inline int dcmp (double a,double b) {
if (fabs (a-b) <EPS) { return 0; }
return a>b;
}
const int M=1e6+50;
double dp[M][2];
string s;
int ss;
bool judge (int n) {
return s
=='C'?true:false;
}
void Init() {
cin>>s;
ss=s.size();
//    if (judge (0) ) {
//        dp[0][0]=1.0;
//        dp[0][1]=0.0;
//    } else {
//        dp[0][0]=0.0;
//        dp[0][1]=1.0;
//    }
}
int main() {
freopen ("foreign.in","r",stdin);
freopen ("foreign.out","w",stdout);
Init();
dp[0][0]=s[0]=='C'?1:0;
dp[0][1]=s[0]=='C'?0:1;
double p,ans=0.0;
for (int i=1; i<ss; ++i) {
p=1.0/ (ss+1-i);
//        dp[i][0]= (1.0-p) *dp[i-1][0];
//        if (judge (i) ) {
//            dp[i][0]+=p;
//        }
dp[i][0]=judge (i) ? (p+ (1-p) *dp[i-1][0]) : ( (1-p) * dp[i-1][0]);
dp[i][1]=1.0-dp[i][0];
(judge (i) ) ?ans+=dp[i][1]:ans+=dp[i][0];
}
printf ("%.10f\n",ans);
return 0;
}

WA在第27组的代码:

#include<map>
#include<set>
#include<list>
#include<stack>
#include<deque>
#include<queue>
#include<vector>
#include<sstream>
#include<iomanip>
#include<iostream>
#include<math.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<functional>
#define CPY(A,B)memcpy(A,B,sizeof(A))
#define fin freopen("in.txt","r",stdin)
#define fout freopen("out.txt","w",stdout)
typedef long long LL;
typedef unsigned long long uLL;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
const LL INFF=0x3f3f3f3f3f3f3f3fLL;
const double OO=1e20;
const double EPS=1e-9;
const double PI=acos (-1.0);
int dx[]= {0,1,0,-1};
int dy[]= {1,0,-1,0};
using namespace std;
int gcd (const LL &a,const LL &b) {
return b==0?a:gcd (b,a%b);
}
inline int dcmp (double a,double b) {
if (fabs (a-b) <EPS) { return 0; }
return a>b;
}
const int M=1e6+50;
double dp[M][2];
string s;
int ss;
bool judge (int n) {
return s
=='C'?true:false;
}
void Init() {
cin>>s;
ss=s.size();
if (judge (0) ) {
dp[0][0]=1.0;
dp[0][1]=0.0;
} else {
dp[0][0]=0.0;
dp[0][1]=1.0;
}//这样用if和else会WA
}
int main() {
freopen ("foreign.in","r",stdin);
freopen ("foreign.out","w",stdout);
Init();
double p,ans=0.0;
for (int i=1; i<ss; ++i) {
p=1.0/ (ss+1-i);
dp[i][0]= (1.0-p) *dp[i-1][0];
if (judge (i) ) {
dp[i][0]+=p;
}//这里用if也会WA
//judge (i) ?dp[i][0]=(p+ (1-p) *dp[i-1][0]) :dp[i][0]=( (1-p) * dp[i-1][0]);//这样写,也会WA
dp[i][1]=1.0-dp[i][0];
(judge (i) ) ?ans+=dp[i][1]:ans+=dp[i][0];
}
printf ("%.10f\n",ans);
return 0;
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: