HDU 4576 简单概率 + 滚动数组DP(大坑)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4576

坑大发了,居然加 % 也会超时;

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <sstream>
 4 #include <cmath>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <string>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <stack>
13 #include <algorithm>
14 using namespace std;
15 #define ll long long
16 #define _cle(m, a) memset(m, a, sizeof(m))
17 #define repu(i, a, b) for(int i = a; i < b; i++)
18 #define repd(i, a, b) for(int i = b; i >= a; i--)
19 #define sfi(n) scanf("%d", &n)
20 #define sfl(n) scanf("%I64d", &n)
21 #define pfi(n) printf("%d\n", n)
22 #define pfl(n) printf("%I64d\n", n)
23 #define MAXN 1000005
24 int n, l, m, r, d;
25 int t = 0, mm;
26 double dp[2][205];
27 int main()
28 {
29     while(~scanf("%d%d%d%d", &n, &m, &l, &r) && (n + m + l + r))
30     {
31         t = 0;
32         repu(i, 0, n + 1) dp[0][i] = 0.0;
33         dp[0][1] = 1.0;
34         int c;
35         repu(i, 0, m)
36         {
37             sfi(d);
38             if(l == 1 && r == n) continue;
39             c = d / n;
40             mm = d - n * c;
41             if(mm)
42             {
43                 t = !t;
44                 repu(j, 1, n + 1)
45                 {
46                     int lt, rt;
47                     if((n + (j - mm)) >= n) lt = (n + (j - mm)) - n;
48                     else lt = (n + (j - mm));
49                     if((j + mm) >= n) rt = (j + mm) - n;
50                     else rt = (j + mm);
51                     if(lt == 0) lt = n;
52                     if(rt == 0) rt = n;
53                     dp[t][j] = (dp[!t][lt] + dp[!t][rt]) / 2.0;
54                     //cout<<j<<" :"<<dp[t][j]<<endl;
55                 }
56             }
57         }
58         double ans = 0.0;
59         if(l == 1 && r == n) ans = 1.0;
60         else
61         repu(i, l, r + 1) ans += dp[t][i];
62         printf("%.4lf\n", ans);
63     }
64     return 0;
65 }

AC

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <sstream>
 4 #include <cmath>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <string>
 8 #include <vector>
 9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <stack>
13 #include <algorithm>
14 using namespace std;
15 #define ll long long
16 #define _cle(m, a) memset(m, a, sizeof(m))
17 #define repu(i, a, b) for(int i = a; i < b; i++)
18 #define repd(i, a, b) for(int i = b; i >= a; i--)
19 #define sfi(n) scanf("%d", &n)
20 #define sfl(n) scanf("%I64d", &n)
21 #define pfi(n) printf("%d\n", n)
22 #define pfl(n) printf("%I64d\n", n)
23 #define MAXN 1000005
24 int n, l, m, r, d;
25 int t = 0, mm;
26 double dp[2][205];
27 int main()
28 {
29     while(~scanf("%d%d%d%d", &n, &m, &l, &r) && (n + m + l + r))
30     {
31         t = 0;
32         repu(i, 0, n + 1) dp[0][i] = 0.0;
33         dp[0][1] = 1.0;
34         int c;
35         repu(i, 0, m)
36         {
37             sfi(d);
38             if(l == 1 && r == n) continue;
39             c = d / n;
40             mm = d - n * c;
41             if(mm)
42             {
43                 t = !t;
44                 repu(j, 1, n + 1)
45                 {
46                     int lt, rt;
47                     lt = (n + (j - mm)) % n;
48                     rt = (j + mm) % n;
49 //                    if((n + (j - mm)) >= n) lt = (n + (j - mm)) - n;
50 //                    else lt = (n + (j - mm));
51 //                    if((j + mm) >= n) rt = (j + mm) - n;
52 //                    else rt = (j + mm);
53                     if(lt == 0) lt = n;
54                     if(rt == 0) rt = n;
55                     dp[t][j] = (dp[!t][lt] + dp[!t][rt]) / 2.0;
56                     //cout<<j<<" :"<<dp[t][j]<<endl;
57                 }
58             }
59         }
60         double ans = 0.0;
61         if(l == 1 && r == n) ans = 1.0;
62         else
63         repu(i, l, r + 1) ans += dp[t][i];
64         printf("%.4lf\n", ans);
65     }
66     return 0;
67 }

TLE

% 慎用啊必须,虽然不知道为什么

时间: 2024-10-16 09:31:45

HDU 4576 简单概率 + 滚动数组DP(大坑)的相关文章

HDU 4576 Robot 概率DP 水题

Robot Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 3851    Accepted Submission(s): 1246 Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells.

Palindrome_滚动数组&amp;&amp;DP

Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into t

poj - 1159 - Palindrome(滚动数组dp)

题意:一个长为N的字符串( 3 <= N <= 5000),问最少插入多少个字符使其变成回文串. 题目链接:http://poj.org/problem?id=1159 -->>状态:dp[i][j]表示第i个字符到第j个字符组成的字符串变成回文串的最少插入次数. 状态转移方程: 若sz[i] == sz[j],则:dp[i][j] = dp[i + 1][j - 1]; 否则:dp[i][j] = min(dp[i + 1][j], dp[i][j - 1]) + 1; 提交,5

ural 2018. The Debut Album 滚动数组dp

点击打开链接 2018. The Debut Album Time limit: 2.0 second Memory limit: 64 MB Pop-group "Pink elephant" entered on recording their debut album. In fact they have only two songs: "My love" and "I miss you", but each of them has a la

HDU 3392 Pie(滚动数组优化)

Problem Description A lot of boys and girls come to our company to pie friends. After we get their information, we need give each of them an advice for help. We know everyone's height, and we believe that the less difference of a girl and a boy has,

URAL 1287. Mars Canals 滚动数组+DP

求从上到下 从左到右 从左上到右下 从右上到左下的 最长的S和s串 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1444; char a[maxn][maxn]; int dp[2][maxn][2][4]; int main() { int n; int ans1 = 0, ans2 = 0; scanf("%d

[ACM] HDU 4576 Robot (概率DP,滚动数组)

Robot Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells. The cells are numbered from 1 to n clockwise. At first the robot is in cell 1. Then Michael uses a remote control to send m commands to the ro

HDU - 4576 Robot(概率dp+滚动数组)

题意:所有的格子围成一个圈,标号为1~n,若从格子1出发,每次指令告知行走的步数,但可能逆时针也可能顺时针走,概率都是1/2,那么问走了m次指令后位于格子l~r(1≤l≤r≤n)的概率. 分析: 1.因为m次指令后不知道会走到哪,会有很多种可能,但是知道从哪里出发,所以起始状态是已知的,在最初的状态,位于格子1是必然的,概率为1. 2.本题应用滚动数组,因为每次指令后都会延伸出无数种可能,这些可能是在前一种状态的基础上延伸的,而且延伸过后前一种状态的值不再有意义,完全可以被当前状态所覆盖. 3.

POJ3071-Football(概率DP+滚动数组)

Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2769   Accepted: 1413 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all teams still in the