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的更多相关文章

  1. 北京师范大学第十四届ACM决赛-重现赛 F:Training Plan(DP)

    传送门 题意 将n个数分成m个集合,\(V_i表示max(x-y),x,y∈第\)i个集合,\(求minΣV_i\) 分析 我们先对难度排序,令dp[i][j]表示前i个数分成j个集合的最小费用 转移 ...

  2. HDU 3757 Evacuation Plan DP

    跟 UVa 1474 - Evacuation Plan 一个题,但是在杭电上能交过,在UVa上交不过……不知道哪里有问题…… 将施工队位置和避难所位置排序. dp[i][j] 代表前 i 个避难所收 ...

  3. BNU 13289 Energetic Pandas DP

     Energetic Pandas  There are n bamboos of different weights Wi. There are n pandas of different capa ...

  4. Project Management Process

    Project Management ProcessDescription .............................................................. ...

  5. Codeforces Round #156 (Div. 2)---A. Greg&#39;s Workout

    Greg's Workout time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  6. UVALive 6467 Strahler Order 拓扑排序

    这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ ...

  7. 每日英语: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 ...

  8. 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 ...

  9. Debugging a SQL Server query with WinDbg

    Debugging a SQL Server query with WinDbg May 13, 2014 · Klaus Aschenbrenner · 5 Comments (Be sure to ...

随机推荐

  1. php-fpm回顾和总结

    时间久了很容易忘,这里做个备份 FastCGI协议php语言的实现,可以高效处理来自web端的动态请求 php-fpm维护一个或者多个php-cgi进程池,处理请求时不需要频繁创建进程 所以比传统的C ...

  2. python函数值传递还是引用传递

    c/c++中有值传递引用传递的区别.但是python中是值传递还是引用传递呢?首先看python中对变量的定义 "python中变量是指向某个内存的, 而内存中的内容是不可变的." ...

  3. qiniu

    @RestControllerpublic class QiNiuController { private static final Logger logger = LoggerFactory.get ...

  4. how to get geometry type of layer using IMapServer3 and IMapLayerInfo? (C#)

    http://forums.arcgis.com/threads/11481-how-to-get-geometry-type-of-layer-using-IMapServer3-and-IMapL ...

  5. 【CV论文阅读】Detecting events and key actors in multi-person videos

    论文主要介绍一种多人协作的视频事件识别的方法,使用attention模型+RNN网络,最近粗浅地学习了RNN网络,它比较适合用于处理序列的存在上下文作用的数据. NCAA Basketball数据集 ...

  6. go-import下划线的作用

    原文:http://studygolang.com/articles/4356 ------------------------------------------------------------ ...

  7. 如何以正确的顺序重新安装驱动程序 | Dell 中国

      购买 支持 社区 我的帐户     购买 支持 社区   如何以正确的顺序重新安装驱动程序 在戴尔笔记本电脑或台式机上手动重新安装Microsoft Windows操作系统后,您还必须以正确的顺序 ...

  8. hdu 5015 233 Matrix (矩阵高速幂)

    233 Matrix Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  9. 某法院HP-P4500存储数据恢复案例

    好久没出来写博客了.过春节来了一直非常忙.尤其是近期,忙着做了好几个大单子.先是一个医院50TB的HP-EVA4400,接着是一个法院12TB的HP-P4500,前几天还有做了一个某游乐城12TB的V ...

  10. 对“使用MyEclipse,写的jsp代码因有汉字而无法保存”问题的解决

    使用MyEclipse编辑jsp时.有时会出现"使用MyEclipse,写的jsp代码因有汉字而无法保存"的现象,怎样解决呢? Window-->Preferences--& ...