Hdu4546 -- DP/priority queue-many priority queue questions recently

Source: Internet
Author: User

I recently encountered many questions about the priority queue. Today, cf B is the priority queue. If the test is AC ..

The general practice of this question is to use the priority queue for many people.

However, I saw in discuss that using the DP decision-making backpack to do this kind of thinking is particularly good. I will talk about its ideas here. As for the priority queue, I will not bother to say it.

 

There is a code segment that is very core and we can use it later, as if I have not used it myself.

1         j=cnt=0,k=n;;2         while(cnt<q)3         {4             cnt+=k;5             j++;6             if (j==n)7                 break;8             k=(k*(n-j)/(j+1));9         }

That is to say, the final result is q <= the number of all combinations.

> = The front is the letter J.

1         for (m=0,i=n-1;j!=0;j--)2             m+=a[i--];

So here we will find the worst case to find out the greatest difficulty of J-Question.

1 memset (F, 0, sizeof (f); 2 F [0] = 1; // I modified 3 for (I = 0; I <N; I ++) 4 {5 for (j = m; j-A [I]> = 0; j --) 6 {// If (F [J-A [I]) 7 f [J] + = f [J-A [I] written by the original author; 8 // F [A [I] ++; 9 Written by the original author} 10} 11 F [0] = 0; // my modified

Here we can regard it as a 01 backpack. The F [x] array defines the number of solutions with difficulty <= x

Of course, in the above code, we can only find the number of solutions for F [x] When the difficulty is exactly X.

But it doesn't matter. We only need to traverse 1-m after the above code is executed and accumulate with recursion.

1         for (i=1;i<=m;i++)2             f[i]+=f[i-1];

Then you can compare my writing method with that of the original author. We are just a little different in initialization.

In fact, if you think about it, you will find that there is a bug in my program, that is, the input difficulty is not 0 by default ..

Then I went to ask for data with the HDU Admin. I had to give it to me in the hope of a try.

Then their data contains 0 and more than one group -.-

1 # include <iostream> 2 # include <queue> 3 # include <algorithm> 4 using namespace STD; 5 6 int n, m; 7 typedef long ll; 8 const int size = 11010; 9 LL arr [size]; 10 struct node11 {12 ll sum; 13 LL nextsum; 14 int nextid; 15 node (){}; 16 node (ll a, LL B, int C): sum (A), nextsum (B), nextid (c) {}; 17 bool operator <(const node & Q) const18 {19 Return nextsum> q. nextsum; // small first team: 20} 21}; 22 priority _ Queue <node> q; 23 24 ll solve () 25 {26 while (! Q. empty () 27 Q. pop (); 28 Q. push (node (0, arr [0], 0); 29 int CNT; 30 LL ans, VAR; 31 node now; 32 CNT = arr [N] = 0; 33 while (CNT <m) 34 {35 now = Q. top (); 36 Q. pop (); 37 Var = now. nextsum; 38 If (now. nextid> = N) 39 {40 continue; 41} 42 Q. push (node (now. sum, now. sum + arr [now. nextid + 1], now. nextid + 1); 43 Q. push (node (now. nextsum, now. nextsum + arr [now. nextid + 1], now. nextid + 1); 44 + + CNT; 45} 46 Return var; 47} 48 49 int main () 50 {51 cin. sync_with_stdio (false); 52 int t; 53 CIN> T; 54 LL ans; 55 for (int K = 1; k <= T; ++ K) 56 {57 CIN> N> m; 58 for (INT I = 0; I <n; ++ I) 59 {60 CIN> arr [I]; 61} 62 sort (ARR, arr + n); 63 ans = solve (); 64 cout <"case #" <k <": "<ans <Endl; 65} 66 return 0; 67}
View code

Paste the code of the priority queue. As for the backpack, you will be too lazy to paste it. You can see it in discuss.

 

Today:

Sometimes

You expect everything to be new

Sometimes

You expect everything to be as usual

Sometimes

You expect everything to be old

However

Past and future cannot go now

 

Hdu4546 -- DP/priority queue-many priority queue questions recently

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.