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——1469 COURSES
COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24192 Accepted: 9426 Descript ...
- Codeforces 284E(概率)
题意: 在T秒内,按输入顺序给出n首曲子的播放顺序,玩家需要从第一曲向后听. 第i首曲子有pi和ti两个属性,pi代表在当前这1秒内有pi/100的概率听出这首曲子是什么, ti代表如果一直持续听ti ...
- Spring集成Jedis(不依赖spring-data-redis)(单机/集群模式)(待实践)
Jedis是Redis的Java客户端,Spring将Jedis连接池作为一个Bean来配置.如果在Spring Data的官网上可以发现,Spring Data Redis已经将Jedis集成进去了 ...
- Java正则表达式过滤出字母、数字和中文
原文:http://blog.csdn.net/k21325/article/details/54090066 1.Java中过滤出字母.数字和中文的正则表达式 (1)过滤出字母的正则表达式 [^(A ...
- Unix时间戳(Unix timestamp)转换
http://tool.chinaz.com/Tools/unixtime.aspx 如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)? Java time JavaScr ...
- [Javascript] Flattening nested arrays: a little exercise in functional refactoring
In this lesson we write an imperative function to flatten nested arrays, and then use the popular ma ...
- Apple Swift编程语言新手教程
文件夹 1 简单介绍 2 Swift入门 3 简单值 4 控制流 5 函数与闭包 6 对象与类 7 枚举与结构 1 简单介绍 今天凌晨Apple刚刚公布了Swift编程 ...
- IOS-Storyboard控制器切换之TabBar(3)
TabBar与Push相反,他以底部为导航 以头部为内容区域,如图: 打开storyboard文件,创建一个UITabBarController文件到画板中,默认带了2个 UIViewControll ...
- BestCoder Round #56/hdu5463 Clarke and minecraft 水题
Clarke and minecraft 问题描述 克拉克是一名人格分裂患者.某一天,克拉克分裂成了一个游戏玩家,玩起了minecraft.渐渐地,克拉克建起了一座城堡. 有一天,克拉克为了让更多的人 ...
- 在shell脚本中执行shell脚本
任务下发 主节点脚本 任务管控脚本 [root@admin1 ~]# cat mytest.sh echo lovecode [root@admin1 ~]# cat myfor.sh #!/bin/ ...