题目链接hdu4546

题目大意:给n长度的数字序列,从中取出k个元素(0<k<=n)求和,找出k个元素之和为第m小的和值。

解题思路:第一次做这样组合的题,第一直觉会想用爆搜穷举出所有组合,然后扔进multiset中维护这些值,搜索的复杂度是指数级,内存也超限。发现找到第m个小的可以直接用优先队列来模拟,因为m<10000,所以可以模拟到第m大就结束(其实用multiset也可以,不过没想出来)。思路是这样的:初始状态为0,每次出队的值一定是将来加上又一个值后最小的,然后将来的那个值要的情况和不要的情况分别入队,从而为下下次的选取提供基础比如:(1 2 3 4 5),1要入队(sum = 1),1不要入队(sum = 0);2要入队(sum = 2),2不要入队(sum = 0);3要入队(sum = 3),3不要入队(sum = 0);4要入队(sum = 4),4不要入队(sum = 0)…无形之间就把(1),(2),(3),(4),…,(1,2),(1,3)…都考虑完毕,这里有个前提:有序。(Ps:不要试图用前缀和枚举,因为中间间隔的组合方案没能考虑到,只能是连续的


AC代码1

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>

using namespace std;
const int maxn = (int)1e4+5;

int n,m;  //n个题目 第m小
int num[maxn];
//int prefix_sum[maxn];
//multiset<int,less<int> > v;

struct node {
	int now_point; //当前位置
	int now_point_sum; //到达当前指针内容的总和
	
	node(): now_point(0),now_point_sum(0) {}
	node(int now_point, int now_point_sum): now_point(now_point),now_point_sum(now_point_sum) {}
	~node() {}
	
	bool operator > (const node& A) const {  //true --> 把后面那项抬到更接近队尾的位置
		if(now_point_sum + num[now_point]> A.now_point_sum + num[A.now_point]) {
			return true;
		}
		return false;
	}
	
	node operator + (int A) const {
		node tmp;
		if(A == 1) {
			tmp.now_point_sum = this->now_point_sum + num[this->now_point];
		}else {
			tmp.now_point_sum = this->now_point_sum;
		}
		tmp.now_point = this->now_point + 1;
		return tmp;
	}
	
};

priority_queue<node, vector<node>, greater<node> > pq;

int main() {
	ios::sync_with_stdio(false);
	int T;
	node now;
	cin >> T;
	for (int k = 1; k <= T; k++) {
// v.clear();
		cin >> n >> m;
// prefix_sum[0] = 0;
		for (int i = 1; i <= n; i++) {
			cin >> num[i];
// prefix_sum[i] = prefix_sum[i-1] + num[i];
		}
		
/*不能用前缀和 因为存在类似 第1个和第3个的组合无法表示 总组合数应该是2^n-1而不是n*(n-1)/2 而且指数时间一般也超时 for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { v.insert(prefix_sum[i] - prefix_sum[i-j]); } } */
/* for (multiset<int,less<int> >::iterator it = v.begin(); it != v.end() && m; it++,m--) { if(m == 1) { cout << *it << '\n'; break; } } cout << v.size() << '\n'; */
		sort(num + 1, num + 1 + n);
		while (!pq.empty()) {
			pq.pop();
		}
		pq.push(node(1,0));

		while (!pq.empty()) {
			now = pq.top();
			pq.pop();
			if(--m == 0) {
				cout << "Case #" << k << ": " << now.now_point_sum + num[now.now_point] << '\n';
				break;
			}
			if(now.now_point == n) {
				continue;
			}
			pq.push(now + 1);
			pq.push(now + 0);
		}
	}
	return 0;
}

AC代码2

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>

using namespace std;
const int maxn = (int)1e4+5;

struct node {
	int now_sum; //当前的值
	int nxt_point; //下一次指针
	int nxt_sum; //下一次到达的值
	
	node() {}
	node(int a, int b, int c):now_sum(a),nxt_point(b),nxt_sum(c) {}
};

struct myCompare {  //直接自定义比较器的仿函数
public:
	bool operator () (const node& A,const node& B) {
    return A.nxt_sum > B.nxt_sum; //b小 true 先出队
	}
};

priority_queue<node, vector<node>, myCompare > pq;
int n,m,num[maxn];

int main() {
	ios::sync_with_stdio(false);
	int T;
	cin >> T;
	for (int k = 1; k <= T; k++) {
		cin >> n >> m;
		for (int i = 0; i < n; i++) {
			cin >> num[i];
		}
		while (!pq.empty()) {
			pq.pop();
		}
		sort(num,num+n);
		pq.push(node(0,0,num[0]));
		while (!pq.empty()) {
			node now = pq.top();
			pq.pop();
// cout << now.nxt_point << " " << now.now_sum << '\n';
			if(--m == 0) {
				cout << "Case #" << k << ": " << now.nxt_sum << '\n';
				break;
			}
			if(now.nxt_point < n - 1) {
				pq.push(node(now.now_sum, now.nxt_point + 1, now.now_sum + num[now.nxt_point + 1]));
				pq.push(node(now.nxt_sum, now.nxt_point + 1, now.nxt_sum+ num[now.nxt_point + 1]));
			}
		}
	}
	return 0;
}