【HDOJ】2195 Monotone SE Min

简单DP。将[0,1]的浮点数离散化为[0,1000]的整数。最后再除以1000^2.

 1 /* 2195 */
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5
 6 #define MAXN 1000
 7 #define MAXL 205
 8 #define INF  0xfffffff
 9
10 int dp[MAXL][MAXN+5];
11 char s[MAXL];
12
13 int main() {
14     int len;
15     int i, j, k;
16     int bi, tmp;
17     double ans;
18
19     #ifndef ONLINE_JUDGE
20         freopen("data.in", "r", stdin);
21     #endif
22
23     while (scanf("%s", s) != EOF) {
24         len = strlen(s);
25         bi = (s[0]==‘0‘) ? 0:MAXN;
26         for (j=0; j<=MAXN; ++j) {
27             dp[0][j] = (j-bi)*(j-bi);
28         }
29         for (i=1; i<len; ++i) {
30             bi = (s[i]==‘0‘) ? 0:MAXN;
31             tmp = INF;
32             for (j=0; j<=MAXN; ++j) {
33                 if (dp[i-1][j] < tmp)
34                     tmp = dp[i-1][j];
35                 dp[i][j] = tmp + (j-bi)*(j-bi);
36             }
37         }
38         tmp = INF;
39         i = len-1;
40         for (j=0; j<=MAXN; ++j)
41             if (dp[i][j] < tmp)
42                 tmp = dp[i][j];
43         ans = tmp / 1000000.0;
44         printf("%.3lf\n", ans);
45     }
46
47     return 0;
48 }
时间: 2024-11-06 23:14:51

【HDOJ】2195 Monotone SE Min的相关文章

【HDOJ】3789 奥运排序问题

写了个函数指针,这题目很水,但是佷烦. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdlib> 5 using namespace std; 6 7 #define MAXN 305 8 9 typedef struct { 10 int id, g, j; 11 float gp, jp; 12 } count_st; 13 14 typedef

【HDOJ】2544 最短路

Dijkstra. 1 #include <stdio.h> 2 #include <string.h> 3 4 #define INF 0xfffffff 5 6 int map[105][105]; 7 int dp[105]; 8 char visit[105]; 9 10 int main() { 11 int n, m; 12 int i, j, k, min; 13 14 while (scanf("%d %d",&n,&m)!=EO

【HDOJ】2732 Leapin&#39; Lizards

贪心+网络流.对于每个结点,构建入点和出点.对于每一个lizard>0,构建边s->in position of lizard, 容量为1.对于pillar>0, 构建边in position of pillar -> out position of pillar, 容量为number of pillar.若沿四个方向移动距离d可以超过边界,则构建边out position of pillar -> t, 容量为INF:否则, 对于曼哈顿距离l(l>0 and l<

【HDOJ】2333 Assemble

二分+贪心策略.其中注释处很重要. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 using namespace std; 7 8 #define MAXL 25 9 #define MAXN 1005 10 #define INF 1000000000 11 12 typed

【HDOJ】2363 Cycling

二分+Dijkstra. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 8 #define MAXN 105 9 #define INF 1000001 10 11 int n, m; 12 int map[MAXN][MAXN], a

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl