牛 客 oj Exercise 10.2 Find the Kth Decimal (set) && Exercise 10.3 Move Fruit (Huffman Tree)

 

I originally thought it would be annoying to do it with set. Later, it was not refreshing to think about the simplest watch, and I was a bit balanced. .

 

 

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
#include <cmath>
#include <climits>
 
using namespace std;
 
const int MAXN = 1005;
const int INF = INT_MAX;

int main(){
//	freopen("in.txt", "r", stdin);
	int n, k, x;
	while(~scanf("%d", &n)){
		set<int> myset;
		for(int i = 0; i < n; i++){
			scanf("%d", &x);
			myset.insert(x);
		}
		scanf("%d", &k);
		set<int>::iterator it;
		int time = 1;
		for(it = myset.begin(); it != myset.end(); it++){
			if(time == k){
				cout << *it << endl;
				break;
			}
			time ++;
		}
	}
	return 0;
}

 

 

Huffman tree water problem, review the priority queue by the way, but remember to output the maximum weight and not the vertex value!

 

 

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <climits>
 
using namespace std;
 
const int MAXN = 1005;
const int INF = INT_MAX;

int main(){
//	freopen("in.txt", "r", stdin);
	int n, x;
	while(~scanf("%d", &n)){
		if(n == 0) break;
		priority_queue<int, vector<int>, greater<int> > myqueue;
		for(int i = 0; i < n; i++){
			scanf("%d", &x);
			myqueue.push(x);
		}
		int ans = 0;
		while(myqueue.size() > 1){
			int x1 = myqueue.top();
			myqueue.pop();
			int x2 = myqueue.top();
			myqueue.pop();
			ans += (x1 + x2);
			myqueue.push(x1 + x2);
		}
		printf("%d\n", ans);
	}
	return 0;
}

 

Published 411 original articles · Like 72 · Visits 160,000+

Guess you like

Origin blog.csdn.net/Flynn_curry/article/details/105031722