The question of downgrading and upgrading when the median is obtained in the bipartite method (thought of by LA3971)

Source: Internet
Author: User

[Cpp]
/*************************************** **********************************
> File Name: 12124.cpp
> Author: BobLee
> Mail: wustboli@gmail.com
> Created Time: Mon 25 Mar 2013 08:36:44 CST
**************************************** ********************************/
 
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <cmath>
# Include <algorithm>
# Include <vector>
# Include <map>
# Include <string>
Using namespace std;
 
Const int maxn = 1010;
 
Struct co
{
Int price;
Int qua;
};
 
Map <string, int> id;
Vector <co> com [maxn];
Int N, B;
Int cnt;
 
Int ID (string s)
{
If (! Id. count (s ))
Id [s] = cnt ++;
Return id [s];
}
 
Bool fun (int q)
{
Int sum = 0;
For (int I = 0; I <cnt; I ++)
{
Int cheap = B + 1;
Int m = com [I]. size ();
For (int j = 0; j <m; j ++)
{
If (com [I] [j]. qua> = q)
Cheap = min (cheap, com [I] [j]. price );
}
If (cheap> B)
Return false;
Sum + = cheap;
If (sum> B)
Return false;
}
Return true;
}
 
Int main ()
{
# Ifndef ONLINE_JUDGE
Freopen ("in.txt", "r", stdin );
# Endif
Int t;
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d", & N, & B );
Char type [30], name [30];
Int price, quaa;
Int maxq = 0;
Cnt = 0;
For (int I = 0; I <N; I ++)
Com [I]. clear ();
For (int I = 0; I <N; I ++)
{
Scanf ("% s % d", type, name, & price, & quaa );
Maxq = max (maxq, quaa );
Com [ID (type)]. push_back (co) {price, quaa });
}
Int L = 0;
Int R = maxq;
While (L <R)
{
Int M = (L + R + 1)/2;
// Cout <L <"<R <" "<M <endl;
If (fun (M ))
{
L = M;
}
Else
R = M-1;
// Cout <L <"<R <" "<M <endl;
// Getchar ();
// If (M = 0)
// Break;
}
Printf ("% d \ n", L );
}
Return 0;
 
}

/*************************************** **********************************
> File Name: 12124.cpp
> Author: BobLee
> Mail: wustboli@gmail.com
> Created Time: Mon 25 Mar 2013 08:36:44 CST
**************************************** ********************************/

# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <cmath>
# Include <algorithm>
# Include <vector>
# Include <map>
# Include <string>
Using namespace std;

Const int maxn = 1010;

Struct co
{
Int price;
Int qua;
};

Map <string, int> id;
Vector <co> com [maxn];
Int N, B;
Int cnt;

Int ID (string s)
{
If (! Id. count (s ))
Id [s] = cnt ++;
Return id [s];
}

Bool fun (int q)
{
Int sum = 0;
For (int I = 0; I <cnt; I ++)
{
Int cheap = B + 1;
Int m = com [I]. size ();
For (int j = 0; j <m; j ++)
{
If (com [I] [j]. qua> = q)
Cheap = min (cheap, com [I] [j]. price );
}
If (cheap> B)
Return false;
Sum + = cheap;
If (sum> B)
Return false;
}
Return true;
}

Int main ()
{
# Ifndef ONLINE_JUDGE
Freopen ("in.txt", "r", stdin );
# Endif
Int t;
Scanf ("% d", & t );
While (t --)
{
Scanf ("% d", & N, & B );
Char type [30], name [30];
Int price, quaa;
Int maxq = 0;
Cnt = 0;
For (int I = 0; I <N; I ++)
Com [I]. clear ();
For (int I = 0; I <N; I ++)
{
Scanf ("% s % d", type, name, & price, & quaa );
Maxq = max (maxq, quaa );
Com [ID (type)]. push_back (co) {price, quaa });
}
Int L = 0;
Int R = maxq;
While (L <R)
{
Int M = (L + R + 1)/2;
// Cout <L <"<R <" "<M <endl;
If (fun (M ))
{
L = M;
}
Else
R = M-1;
// Cout <L <"<R <" "<M <endl;
// Getchar ();
// If (M = 0)
// Break;
}
Printf ("% d \ n", L );
}
Return 0;

}

We can see that the value is rounded up when I take the value. (Actually written by Liu rujia)

At that time, I wrote M = (L + R)/2; that is, the meaning of downgrading, But I encountered a problem in different submissions.

My TLE.

At that time, I was puzzled. After careful consideration, I found a problem. That is, you are in this program to find the maximum value of a reasonable interval.

For example, if the final value we want to take is 5 and the interval is [], use the rounded down

Then

L R M

0 5 2

2 5 3

3 5 4

4 5 4

4 5 4

...

If you find the problem, it will be in an endless loop.

Let's look at another example. If we want to find the minimum and binary code of a reasonable interval


[Cpp]
While (L <R)
{
M = (L + R + 1)/2;
If (OK (M ))
R = M;
Else
L = M + 1;
}

While (L <R)
{
M = (L + R + 1)/2;
If (OK (M ))
R = M;
Else
L = M + 1;
} We still use the rounded up method. The range is [0, 5], and the last value is 0.

L R M

0 5 3

0 3 2

0 2 1

0 1 1

0 1 1

...

It is in an endless loop, but we can find that it is feasible to take down the whole.

So we can get from the above,

When we use the bipartite method to find the maximum value of a reasonable range, we should pay attention to the extreme values of the two endpoints.

Of course, there is a more secure method.

That is, the above binary method is not used.

Use another variable to record a reasonable value in binary, then L = M + 1 or r = M-1

In this case, it is not stuck in an endless loop.

This is why I think of it. I have not written a blog for a long time. You still need to pick it up.

 

 

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.