Hdu-5884 Sort (k Fork Huffman Tree)

Source: Internet
Author: User
Problem Description recently, Bob has just learnt a naive sorting algorithm:merge sort. Now, Bob receives a task from Alice.
Alice would give Bob N sorted sequences, and the i-th sequence includes AI elements. Bob need to merge all of these sequences. He can write a program, which can merge no more than K sequences in one time. The cost of a merging operation is the sum of the length of these sequences. Unfortunately, Alice allows this program to use the no more than T cost. So Bob wants to know the smallest K-to-make, the program, complete in time.
Input the first line of input contains an integer t0, the number of test cases. T0 test Cases follow.
For each test case, the first line consists integers N (2≤n≤100000) and T (∑ni=1ai<t<231).
The next line there is N integers a1,a2,a3,..., an (∀i,0≤ai≤1000).
Output for each test cases, output the smallest K.
Sample Input
1 5 25 1 2 3 4 5
Sample Output
3
Source ACM/ICPC Asia Regional Qingdao Online
Analysis: Two +k fork Huffman Tree, note (n-1)% (k-1). = 0 O'Clock special treatment is needed.


#include <iostream> #include <string> #include <algorithm> #include <cstdlib> #include < cstdio> #include <set> #include <map> #include <vector> #include <cstring> #include <stack > #include <queue> #define INF 0x3f3f3f3f #define EPS 1e-9 #define MOD 1000000007 #define MAXN 200005 using Nam
Espace std;
typedef long Long LL;
int N,T,T,A[MAXN];
    void Nextint (int &x)//input plug-in {char C; Do C = GetChar ();  
    while (C < ' 0 ' | | c > ' 9 ');  
    x = C ' 0 ';  
while (' 0 ' <= (c = GetChar ()) && c<= ' 9 ') x = x*10 + C-' 0 ';
	} ll check (int mid) {deque <ll> q1,q2;
	for (int i = 1;i <= n;i++) q1.push_back (A[i]);
	int res = (n-1)% (mid-1);
	ll tmp = 0,ans = 0;
	if (res) res++;
		for (int i = 1;i <= res;i++) {tmp + = Q1.front ();  
	Q1.pop_front ();
	} ans + = tmp;
	if ((n-1)% (mid-1)) Q2.push_back (TMP);
		while (Q1.size () + q2.size () > 1) {ll tmp = 0; for (int i = 1;i <= mid;i++) {if (q2.empty () | |
				Q1.front () < Q2.front ()) && q1.size ()) {tmp + = Q1.front ();
			Q1.pop_front ();
				} else {tmp + = Q2.front ();
			Q2.pop_front ();
		}} ans + = tmp;
	Q2.push_back (TMP);
} return ans; 
	} int main () {nextint (T);
		while (t--) {nextint (n), Nextint (T);
		for (int i = 1;i <= n;i++) nextint (A[i]);
		Sort (a+1,a+1+n);
		int L = 2,r = n;
			while (l! = r) {int mid = (l+r)/2;
			if (check (mid) <= 1ll*t) R = Mid;	
		else L = mid+1;	
	} cout<<l<<endl; }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.