玲珑杯 1066(“玲珑杯”ACM比赛 Round #6)(区间DP+四边形不等式优化)
来源:互联网 发布:炉石盒子mac 编辑:程序博客网 时间:2024/04/27 03:25
1066 - Buy Candy
Time Limit:1s Memory Limit:1024MByte
Submissions:185Solved:39
DESCRIPTION
PigVan ( Mr.Van's pet ) encounter a hard problem in his school. There are n groups children sit in a line. However, all groups dislike each other. Each group have a value which means how others dislike him. And PigVan wants to make all of them become one group. He can only merge two adjacent group into one. And if he wants to merge two groups of children, he needs to buy f(s) candies to them. ( s is the sum of their dislike value andf is a polynomial function of s ).
He wonder the minimal cost he will spend on candies.
INPUT
The first line is a single integer T, indicating the number of test cases.
For each test case:
In the first line, there are an integer n (1≤n≤1000)n (1≤n≤1000)
In the second line, there are nn integers. The iithth integer ssii (1 ≤ ssii ≤ 40), indicating the dislike value of the iithth child.
In the third line, there are an integer m (1≤m≤4)m (1≤m≤4)In the next line, there are m+1m+1 integers a0 , a1 ,..., am.
The polynomial function f(x) = a0 + a1x + a2x2 +...+ amxm(1≤(1≤ai≤5)≤5)
For each test case:
In the first line, there are an integer n (1≤n≤1000)n (1≤n≤1000)
In the second line, there are nn integers. The iithth integer ssii (1 ≤ ssii ≤ 40), indicating the dislike value of the iithth child.
In the third line, there are an integer m (1≤m≤4)m (1≤m≤4)In the next line, there are m+1m+1 integers a0 , a1 ,..., am.
The polynomial function f(x) = a0 + a1x + a2x2 +...+ amxm(1≤(1≤ai≤5)≤5)
OUTPUT
For each test case, output an integer indicating the answer.
SAMPLE INPUT
153 1 8 9 922 1 2
SAMPLE OUTPUT
2840
SOLUTION
“玲珑杯”ACM比赛 Round #6
#include <stdio.h>#include <string.h>#include <vector>#define LL long longusing namespace std;const int maxn = 1005;const long long inf = 1LL << 62;LL dp[maxn][maxn];int p[maxn][maxn];LL a[1005] = { 0 };LL b[55];int n, m;LL jici(LL x, int y){LL ret = 1;for (int i = 1; i <= y; i++)ret *= x;return ret;}LL fun(LL x){LL ret = 0;for (int i = 0; i <= m; i++)ret += jici(x, i)*b[i];return ret;}int main(){int T;scanf("%d", &T);while (T--){scanf("%d", &n);for (int i = 1; i <= n; i++){scanf("%lld", &a[i]);a[i] += a[i - 1];dp[i][i] = 0;p[i][i] = i;}scanf("%d", &m);for (int i = 0; i <= m; i++)scanf("%lld", &b[i]);LL sum = 0;for (int len = 1; len <= n - 1; len++){for (int i = 1; i + len <= n; i++){int j = i + len;LL F = fun(a[j] - a[i-1]);dp[i][j] = inf;for (int k = p[i][j - 1]; k <= p[i + 1][j]; k++){if (dp[i][j] > dp[i][k] + dp[k + 1][j]+F){dp[i][j] = dp[i][k] + dp[k + 1][j] + F;p[i][j] = k;}}}}printf("%lld\n", dp[1][n]);}return 0;}
0 0
- 玲珑杯 1066(“玲珑杯”ACM比赛 Round #6)(区间DP+四边形不等式优化)
- 石子合并,四边形不等式优化(Buy Candy,玲珑杯 Round#6 A lonlife 1066)
- “玲珑杯”ACM比赛 Round #21-A- 西方制度 (区间DP+平行四边形优化)
- “玲珑杯”ACM比赛 Round #4 E -- array(dp)
- “玲珑杯”ACM比赛 Round #18 C(dp)
- “玲珑杯”ACM比赛 Round #12【dp】
- “玲珑杯”ACM比赛 Round #21-C-战舰萝莉(线段树区间更新)
- “玲珑杯”ACM比赛 Round #5 C -- Coco(递归)
- “玲珑杯”ACM比赛 Round #18 A(穷举)
- “玲珑杯”ACM比赛 Round #18 A -- 图论你先敲完模板(DP+思路)
- 玲珑杯”ACM比赛 Round #4 E -- array【DP】
- “玲珑杯”ACM比赛 Round #4
- 玲珑杯”ACM比赛 Round #5
- “玲珑杯”ACM比赛 Round #7
- “玲珑杯”ACM比赛 Round #7
- “玲珑杯”ACM比赛 Round #11 " ---1097
- “玲珑杯”ACM比赛 Round #11 D
- “玲珑杯”ACM比赛 Round #11
- 谈谈java中的WeakReference
- 函数定义 匿名函数
- AngularJs与Django标签冲突如何解决?
- 1简介
- POJ 1150 The Last Non-zero Digit 已翻译
- 玲珑杯 1066(“玲珑杯”ACM比赛 Round #6)(区间DP+四边形不等式优化)
- IPython[Notebook]安装
- 学程序之路-不止与平凡
- 如何获取控件的坐标位置
- 4.5字符串和格式化输入或输出 关键概念
- GDT详解
- 递归--类循环排列
- 2016 NOIP第一题 买铅笔
- steam (游戏平台)