bnu 51640 Training Plan DP
https://www.bnuoj.com/bnuoj/problem_show.php?pid=51640
dp[i][j]表示前j个数,分成了i组,最小需要多少精力。
那么,求解订票dp[i][j]的时候,要么,第i组不做题,要么,第i组做1题、2题、3题....j题
先把数组排好序。然后暴力
dp[i][j] = dp[i - 1][j] //第i组不做题。
然后枚举一个k,表示[K.....j]是第i组做的题。那么就是dp[i - 1][k - 1] + (a[j] - a[k])^2
复杂度是O(n^3)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
int a[maxn];
LL dp[maxn][maxn];
void work() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
}
sort(a + , a + + n);
for (int i = ; i <= n; ++i) dp[][i] = 1e18;
dp[][] = ;
for (int i = ; i <= m; ++i) {
for (int j = ; j <= n; ++j) {
dp[i][j] = dp[i - ][j];
for (int k = ; k <= j; ++k) {
dp[i][j] = min(dp[i][j], dp[i - ][k - ] + 1LL * (a[j] - a[k]) * (a[j] - a[k]));
}
}
}
printf("%lld\n", dp[m][n]);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
bnu 51640 Training Plan DP的更多相关文章
- 北京师范大学第十四届ACM决赛-重现赛 F:Training Plan(DP)
传送门 题意 将n个数分成m个集合,\(V_i表示max(x-y),x,y∈第\)i个集合,\(求minΣV_i\) 分析 我们先对难度排序,令dp[i][j]表示前i个数分成j个集合的最小费用 转移 ...
- HDU 3757 Evacuation Plan DP
跟 UVa 1474 - Evacuation Plan 一个题,但是在杭电上能交过,在UVa上交不过……不知道哪里有问题…… 将施工队位置和避难所位置排序. dp[i][j] 代表前 i 个避难所收 ...
- BNU 13289 Energetic Pandas DP
Energetic Pandas There are n bamboos of different weights Wi. There are n pandas of different capa ...
- Project Management Process
Project Management ProcessDescription .............................................................. ...
- Codeforces Round #156 (Div. 2)---A. Greg's Workout
Greg's Workout time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- UVALive 6467 Strahler Order 拓扑排序
这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ ...
- 每日英语:Best Ways to Ramp Up to A Marathon
For the record number of American runners who completed an official race event last year, the questi ...
- Educational Codeforces Round 42 (Rated for Div. 2) A
A. Equator time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- Debugging a SQL Server query with WinDbg
Debugging a SQL Server query with WinDbg May 13, 2014 · Klaus Aschenbrenner · 5 Comments (Be sure to ...
随机推荐
- POJ 3255_Roadblocks
题意: 无向图,求单源次短路,每条边可以走多次. 分析: 对于每个点记录最短路和次短路,次短路可以是由最短路+边或者是次短路+边更新而来.在更新每个点的最短路时,相应更新次短路,如果最短路更新了,就令 ...
- JavaScript高级篇之Function对象
JavaScript高级篇之Function对象 一: Function对象引入: Function对象是js的方法对象,可以用Function实例化出任何js方法对象. 例如: <%@ pag ...
- HDU——3072 Intelligence System
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- idea中javaweb的mysql8.0.15配置问题
mysql8.0.x以后的版本在连接数据库的时候有些不同. 首先: Class.forName("com.mysql.cj.jdbc.Driver"); 其次: DriverMan ...
- 选择器的使用(first-child和last-child选择器)
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...
- Why It is so hard to explain or show some thing
Why it is hard to explain something or learn something? For example, when I first know the hadoop, I ...
- mainboard
MAINBOARD ★ CPU(type, speed, amount, cache, slot or socket, fan) ★ RAM(the most capacity, amount, fr ...
- 86. LotusScript中的数组函数
R6对LotusScript有一些改进和增强,自那之后.Notes对象的接口时有补充和更新,但语言本身没有变化.那些改进就包括添加诸如ArrayGetIndex.ArrayUnique的实用函数. 但 ...
- jQuery -> 获取指定上下文中的DOM元素
jQuery函数的第二个參数能够指定DOM元素的搜索范围. 第二个參数可分为下面类型 DOM reference jQuery wrapper document 代码演示样例 <!DOCTYPE ...
- 将代码设置的剪切板内容通过输入法软件粘贴入app搜索框
#进入app搜索框位置--双击#等待输入法软件弹出#将代码设置的剪切板内容通过输入法软件粘贴入app搜索框#搜索 import win32apiimport timeimport win32clipb ...