首页 > 其他分享 >ZOJ 2068 Chopsticks

ZOJ 2068 Chopsticks

时间:2022-11-09 19:06:19浏览次数:44  
标签:his 81 int ZOJ 134 2068 Chopsticks sets include


Description


It's December 2nd, Mr.L's birthday! He invited K people to join his birthday party, and would like to introduce his way of using chopsticks. So, he should prepare K+8 sets of chopsticks(for himself, his wife, his little son, little daughter, his mother, father, mother-in-law, father-in-law, and K other guests). But Mr.L suddenly discovered that his chopsticks are of quite different lengths! He should find a way of composing the K+8 sets, so that the total badness of all the sets is minimized.

Input

The first line in the input contains a single integer T, indicating the number of test cases(1<=T<=20). Each test case begins with two integers K, N(0<=K<=1000, 3K+24<=N<=5000), the number of guests and the number of chopsticks. There are N positive integers Li on the next line in non-decreasing order indicating the lengths of the chopsticks.(1<=Li<=32000).

Output

For each test case in the input, print a line containing the minimal total badness of all the sets.

Sample Input

1
1 40
1 8 10 16 19 22 27 33 36 40 47 52 56 61 63 71 72 75 81 81 84 88 96 98 103 110 113 118 124 128 129 134 134 139 148 157 157 160 162 164

Sample Output

23

Note

For the sample input, a possible collection of the 9 sets is:

8,10,16; 19,22,27; 61,63,75; 71,72,88; 81,81,84; 96,98,103; 128,129,148; 134,134,139; 157,157,160


动规,首先可以证明A,B一定是连续的,然后只要对筷子的长度排序,从大到小处理,保证C的存在即可.

#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<iostream>
#include<string>
#include<map>
#include<functional>
using namespace std;
const int maxn = 5005;
int T, K, n, a[maxn], f[maxn][1005];

int cmp(const int &a, const int &b)
{
return a > b;
}

int p(int x, int y)
{
return (a[x] - a[y])*(a[x] - a[y]);
}

int main()
{
cin >> T;
while (T--)
{
cin >> K >> n;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
sort(a + 1, a + n + 1, cmp);
memset(f, 1, sizeof(f)); f[0][0] = 0;
for (int i = 1; i <= n; i++)
for (int j = min(K + 8, i / 3); j >= 0; j--)
{
f[i][j] = f[i - 1][j];
if (i >= j + j + j)
f[i][j] = min(f[i][j], f[i - 2][j - 1] + p(i, i - 1));
}
cout << f[n][K + 8] << endl;
}
return 0;
}



标签:his,81,int,ZOJ,134,2068,Chopsticks,sets,include
From: https://blog.51cto.com/u_15870896/5838285

相关文章

  • ZOJ 3911 Prime Query
    PrimeQueryTimeLimit: 1Second     MemoryLimit: 196608KBYouaregivenasimpletask.Givenasequence A[i] with NHerearetheoperations:Av......
  • ZOJ 3905 Cake
    CakeTimeLimit: 4Seconds     MemoryLimit: 65536KBAliceandBoblikeeatingcakeverymuch.Oneday,AliceandBobwenttoabakeryandboughtm......
  • [bzoj3033] 太鼓达人 (欧拉回路)
    学会了欧拉回路pwpwpwpwpwpDescription七夕祭上,Vani牵着cl的手,在明亮的灯光和欢乐的气氛中愉快地穿行。这时,在前面忽然出现了一台太鼓达人机台,而在机台前坐着的是......
  • BZOJ P3732 Network(Kruskal重构树)
    Network题目描述:给\(N\)个点的无向图\(\left(1\leqN\leq15000\right)\),记为:\(1\dotsN\)图中有\(M\)条边\(\left(1\leqM\leq30000\right)\),第\(i\)......
  • 树上连通有关背包:【BZOJ4182】shopping &【HDU6566】The Hanged Man
    选这两道题是因为这两道题都是树上背包,而且选的点的要求都与连通性有关,而且都是按dfs序DP来模拟不断加入物品,而且都能用树剖和点分治优化(不过优化的点一个跟子树大小有......
  • 【XSY2444】【BZOJ4042】【CERC2014】【luogu4757】Parades(树形dp+状压dp)
    题面Description从前有个A国,它有\(n\)个城市和\(n-1\)条道路。每条路连接两个城市。城市之间两两可达。每个城市与不超过10条道路相连。现在给出\(m\)条路径,要求从这些......
  • 【XSY1976】【BZOJ2286】【SDOI2011】消耗战(虚树,dp)
    这题的dp思想还是比较容易想的,关键是如何保证时间复杂度,这时就用到了虚树的技巧。1.虚树是什么?(虚树的性质)不妨设现在询问给出了\(k\)个点,我们命名这些节点为关键节点。......
  • P2068 统计和
    水题,线段树板子(单点修改和区间和) #include<bits/stdc++.h>usingnamespacestd;typedefunsignedlonglongull;constintN=1e5+7;ulla[N],tree[N*4];voi......
  • 【BZOJ4987】Tree(树形dp)
    题意:给你一棵\(n\)个节点的树找出\(k\)个不同的点\(a_1,a_2,\cdots,a_k\),使得\(\sum\limits_{i=1}^{k-1}dis(a_i,a_{i+1})\)最小。首先有个容易想到的性质:这\(k......
  • 【bzoj4869】【六省联考2017】相逢是问候(扩展欧拉函数)
    和《花神游历各国》有异曲同工之妙。首先能想到扩展欧拉定理:\[a^b\equiv\begin{cases}a^{b\bmod\varphi(p)+\varphi(p)}&\text{if}b\geq\varphi(p)\\a^b&\text{if}......