试题 算法训练 粘木棍

问题描述

  有N根木棍,需要将其粘贴成M个长木棍,使得最长的和最短的的差距最小。

输入格式

  第一行两个整数N,M。
  一行N个整数,表示木棍的长度。

输出格式

  一行一个整数,表示最小的差距

样例输入

3 2
10 20 40

样例输出

10

数据规模和约定

  N, M<=7

题解:从大到小排序,如果n==m的话,就直接输出最大值-最小值。否则的话,就先把a数组从大到小存到b数组里,然后一边对b排序,一边往b的最小值里填数,最后就是最大值-最小值

证明:三个数,a>b>c两个桶的话,肯定是b+c和a的情况是最好的,(比如a+b的话肯定比之前a和c的差距大,a+c的话肯定比之前a和b的差距大)也就是从大到小排序,把前m个放进m个桶里,然后后边的都比前m个桶里的数小,然后每读一个就放进最小的桶里,也就是两个最小的放一块,然后排序,然后重复操作。从大到小一个是因为要把小的放在后边然后依次往桶里放,一个是因为后边没放的也是从大到小排序的,把大的放进最小桶里,比小的放进最小桶里,大的放进更大桶里拉开的差距更小。

代码

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first 
#define yy second 
#define endl '\n'
#define O_O ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int n, m, a[N], b[N];
int main()
{
	cin >> n >> m;
	for(int i = 1; i <= n; i ++) cin >> a[i];
	sort(a + 1, a + 1 + n, greater<int>());
	if(n == m)
	{
		cout << a[1] - a[n] << endl;
	}
	else
	{
		for(int i = 1; i <= m; i ++)
		{
			b[i] = a[i];
		}
		for(int i = m + 1; i <= n; i ++)
		{
			b[m] += a[i];
			sort(b + 1, b + 1 + m, greater<int>());
		}
		cout << b[1] - b[m] << endl;
	}
	return 0;
}

但是~这个有的数据可以hack掉,比如5 3 2 2 2 3 3 ,答案应该是0,但是这个代码只会输出2。

所以也可以用m进制爆搜来做

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
//#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define LLu unsigned long long
#define PLL pair<ll, ll>
#define PII pair<int, int>
#define xx first 
#define yy second 
#define endl '\n'
#define O_O ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int gcd(int a, int b) {return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) {return a/gcd(a, b)*b;}
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-6;
int n, m, a[N], b[10], c[10];
int main()
{
	cin >> n >> m;
	for(int i = 1; i <= n; i ++) cin >> a[i];
	int tot = 1;
	for(int i = 1; i <= n; i ++)
	{
		tot *= m;
	}
	int ans = INF;
	for(int i = 0; i < tot; i ++)
	{
		int x = i;
		int id = n;
		mem(c, 0);
		mem(b, 0);
		for(int j = 1; j <= n; j ++)
		{
			int t = x%m + 1;
			x /= m;
			c[id] = t;
			id --;
		}
		for(int j = 1; j <= n; j ++)
		{
			b[c[j]] += a[j];
		}
		vector<int>cnt;
		for(int j = 1; j <= m; j ++)
		{
			if(b[j])
				cnt.push_back(b[j]);
		}
		if(cnt.size() == m)
		{
			sort(b + 1, b + 1 + m);
			ans = min(ans, b[m] - b[1]);
		}
	}
	cout << ans << endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值