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. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

  2. 静态区间第k大(主席树)

    POJ 2104为例(主席树入门题) 思想: 可持久化线段树,也叫作函数式线段树,也叫主席树(高大上). 可持久化数据结构(Persistent data structure):利用函数式编程的思想使 ...

  3. [bzoj4826][Hnoi2017]影魔_单调栈_主席树

    影魔 bzoj-4826 Hnoi-2017 题目大意:给定一个$n$个数的序列$a$,求满足一下情况的点对个数: 注释:$1\le n,m\le 2\cdot 10^5$,$1\le p1,p2\l ...

  4. P1605 迷宫 洛谷

    https://www.luogu.org/problem/show?pid=1605 题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐 ...

  5. Linux纯Shell实现DNSPod动态域名

    http://www.anrip.com/post/872 开发背景: 公司有台嵌入式拨号上网设备,内置busybox和完整wget命令(支持https协议),但没有curl.python.ruby. ...

  6. AutoCAD如何编辑块,打散块

    选中块之后,点击最右侧的最后一个工具"分解"即可.  

  7. UML中的四种关系总结

    UML中的关系主要包含四种:关联关系.依赖关系.泛化关系.实现关系.当中关联关系还包含聚合关系和组合关系. 1. 关联关系(Association) 关联关系式一种结构化的关系,是指一种对象和还有一种 ...

  8. 【剑指Offer】俯视50题之31 - 40题

    面试题31连续子数组的最大和 面试题32从1到n整数中1出现的次数 面试题33把数组排成最小的数 面试题34丑数 面试题35第一个仅仅出现一次的字符 面试题36数组中的逆序对 面试题37两个链表的第一 ...

  9. HDU4930-Fighting the Landlords

    题意:斗地主,就是要自己出牌.使得对手在这一轮无法出牌,或者有出牌的可能.可是你的牌已经走完了.假设符合这些条件的话,输出Yes.否则输出No. 思路:先预处理能直接把牌走完的情况,假设不行的话就直接 ...

  10. java 翻页工具类

    Pagination类 package com.paic.bics.core.mybatis.page; import java.util.List; @SuppressWarnings(" ...