码迷,mamicode.com
首页 > 其他好文 > 详细

HDU-3506 二维四边形不等式

时间:2018-09-24 11:18:22      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:ide   四边形不等式   first   several   hid   ons   elf   ble   scanf   

Monkey Party

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2665    Accepted Submission(s): 1074


Problem Description
Far away from our world, there is a banana forest. And many lovely monkeys live there. One day, SDH(Song Da Hou), who is the king of banana forest, decides to hold a big party to celebrate Crazy Bananas Day. But the little monkeys don‘t know each other, so as the king, SDH must do something. 
Now there are n monkeys sitting in a circle, and each monkey has a making friends time. Also, each monkey has two neighbor. SDH wants to introduce them to each other, and the rules are: 
1.every time, he can only introduce one monkey and one of this monkey‘s neighbor. 
2.if he introduce A and B, then every monkey A already knows will know every monkey B already knows, and the total time for this introducing is the sum of the making friends time of all the monkeys A and B already knows; 
3.each little monkey knows himself; 
In order to begin the party and eat bananas as soon as possible, SDH want to know the mininal time he needs on introducing. 
 

 

Input
There is several test cases. In each case, the first line is n(1 ≤ n ≤ 1000), which is the number of monkeys. The next line contains n positive integers(less than 1000), means the making friends time(in order, the first one and the last one are neighbors). The input is end of file.
 

 

Output
For each case, you should print a line giving the mininal time SDH needs on introducing.
 

 

Sample Input
8 5 2 4 7 6 1 3 9
 

 

Sample Output
105
 

 

Author
PerfectCai
 

 

Source
 
二维四边形不等式:
定理一:
如果dp[i][j] = Min(dp[i][k] + dp[k + 1][j] + w(i, j)), 若
(1)w满足四边形不等式
(2)对于a < b < c < d,满足w(a, d) > w(b, c)
则dp满足四边形不等式
 
定理二:
若dp满足四边形不等式,设p[i][j]为dp[i][j]转移来的k
则p[i][j - 1] <= p[i][j] <= p[i + 1][j];
 
具体操作:
在转移时只需讲k由p[i][j - 1]枚举至p[i + 1][j]即可。可以证明,时间复杂度n2.
感觉比一维四边形不等式好写的多...
技术分享图片
 1 #include <iostream>
 2 #include <fstream>
 3 #include <sstream>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <string>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <queue>
11 #include <stack>
12 #include <vector>
13 #include <set>
14 #include <map>
15 #include <list>
16 #include <iomanip>
17 #include <cctype>
18 #include <cassert>
19 #include <bitset>
20 #include <ctime>
21 
22 using namespace std;
23 
24 #define pau system("pause")
25 #define ll long long
26 #define pii pair<int, int>
27 #define pb push_back
28 #define mp make_pair
29 #define mp make_pair
30 #define pli pair<ll, int>
31 #define clr(a, x) memset(a, x, sizeof(a)
32 
33 const double pi = acos(-1.0);
34 const int INF = 0x3f3f3f3f;
35 const int MOD = 1e9 + 7;
36 const double EPS = 1e-9;
37 
38 /*
39 #include <ext/pb_ds/assoc_container.hpp>
40 #include <ext/pb_ds/tree_policy.hpp>
41 using namespace __gnu_pbds;
42 #define TREE tree<pli, null_type, greater<pli>, rb_tree_tag, tree_order_statistics_node_update>
43 TREE T;
44 */
45 
46 int n, a[2015], dp[2015][2015], p[2015][2015], sum[2015];
47 int main() {
48     while (~scanf("%d", &n)) {
49         for (int i = 1; i <= n; ++i) {
50             scanf("%d", &a[i]);
51             a[i + n] = a[i];
52         }
53         for (int i = 1; i <= n << 1; ++i) {
54                 sum[i] = sum[i - 1] + a[i];
55         }
56         for (int i = 1; i <= n << 1; ++i) {
57                 p[i][i] = i;
58                 dp[i][i] = 0;
59         }
60         for (int l = 2; l <= n; ++l) {
61                 for (int i = 1; i <= 2 * n - l + 1; ++i) {
62                         int j = i + l - 1;
63                         dp[i][j] = INF;
64                         for (int k = p[i][j - 1]; k <= p[i + 1][j]; ++k) {
65                                 if (dp[i][k] + dp[k + 1][j] < dp[i][j]) {
66                                         dp[i][j] = dp[i][k] + dp[k + 1][j];
67                                         p[i][j] = k;
68                                 }
69                         }
70                         dp[i][j] += sum[j] - sum[i - 1];
71                 }
72         }
73         int ans = INF;
74         for (int i = 1; i <= n; ++i) {
75                 ans = min(ans, dp[i][i + n - 1]);
76         }
77         printf("%d\n", ans);
78     }
79     return 0;
80 }
View Code

 

HDU-3506 二维四边形不等式

标签:ide   四边形不等式   first   several   hid   ons   elf   ble   scanf   

原文地址:https://www.cnblogs.com/BIGTOM/p/9694562.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!