Codeforces Round #204 (Div. 2) C
写了一记忆化 TLE了 把double换成long long就过了 double 这么耗时间啊
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<stdlib.h> 5 #include<algorithm> 6 #include<cmath> 7 using namespace std; 8 #define N 4010 9 #define INF 0xfffffff 10 #define LL long long 11 double a[N]; 12 LL dp[N][N],b[N][2]; 13 int n; 14 LL dfs(int i,int r1,int r2,LL ss) 15 { 16 if(i>2*n) 17 return dp[r1][r2] = 0; 18 if(r1<=0&&r2<=0) 19 return 0; 20 if(dp[r1][r2]!=INF) 21 return dp[r1][r2]; 22 LL bs = ss,as = ss,s1,s2; 23 if(r1) 24 { 25 s1 = (b[i][1]+dfs(i+1,r1-1,r2,ss+b[i][1])); 26 as += (b[i][1]+dfs(i+1,r1-1,r2,ss+b[i][1])); 27 } 28 if(r2) 29 { 30 s2 = b[i][0]+dfs(i+1,r1,r2-1,ss+b[i][0]); 31 bs+=b[i][0]+dfs(i+1,r1,r2-1,ss+b[i][0]); 32 } 33 if(r1&&r2) 34 { 35 if(abs(bs)<abs(as)) 36 return dp[r1][r2] =s2 ; 37 else 38 return dp[r1][r2] = s1; 39 } 40 else if(r1) 41 return dp[r1][r2] = s1; 42 else 43 return dp[r1][r2] = s2; 44 } 45 int main() 46 { 47 int i,j; 48 scanf("%d",&n); 49 for(i = 0; i <= 2*n ; i++) 50 for(j =0 ; j <= 2*n ; j++) 51 dp[i][j] = INF; 52 for(i = 1; i <= 2*n ; i++) 53 { 54 scanf("%lf",&a[i]); 55 b[i][1] = (floor(a[i])-a[i])*10000000; 56 b[i][0] = (ceil(a[i])-a[i])*10000000; 57 } 58 /*for(i = 2*n-1 ;i >=1 ; i--) 59 for(j = 2*n; j >= 1 ; j--) 60 { 61 double 62 }*/ 63 LL s = dfs(1,n,n,0); 64 printf("%.3lf\n",(double)fabs(s)/10000000.0); 65 return 0; 66 }