P1080 king game, p1080 king game

Source: Internet
Author: User

P1080 king game, p1080 king game
Description

On the National Day of the country H, the king invited n ministers to play a prize-winning game. First, he asked each minister to write an integer respectively on the left and right hands, and the king himself wrote an integer on the left and right hands. Then, let the n ministers in a row, and the king stood at the forefront of the team. After queuing, all ministers will receive several gold coins rewarded by the King. Each minister will receive the following gold coins: the product of the number on the left hand of all people in front of the minister divided by the number on his right hand, and then rounded down to get the result.

The king does not want a minister to receive a special reward. Therefore, he wants to ask you to help him arrange the order of his team so that the minister with the most rewards can receive as little as possible. Note that the king is always at the forefront of the team.

Input/Output Format

Input Format:

 

The first line contains an integer n, indicating the number of ministers.

The second line contains two integers, a and B, separated by a space, representing the integers on the left and right hand of the king respectively.

In the next n rows, each row contains two integers, a and B, separated by a space, representing the integers on the left and right hands of each minister respectively.

 

Output Format:

 

The output contains only one row and an integer, indicating the number of gold coins received by the minister who received the most awards in the rearranged team.

 

Input and Output sample input sample #1:
3 1 1 2 3 7 4 4 6 
Output sample #1:
2
Description

[Input and output sample description]

The ranking of Ministers 1, 2, and 3 is as follows. The number of gold coins received by ministers who receive the most prizes is 2;

Sort the team by 1, 3, and 2. The number of gold coins received by the minister who received the most prizes is 2;

Sort the teams in the order of 2, 1, and 3. The number of gold coins received by the minister who received the most prizes is 2;

Sort the teams in the order of 2, 3, and 1. The number of gold coins received by the minister who received the most prizes is 9;

Sort the teams by 3, 1, and 2. The number of gold coins received by the minister who received the most prizes is 2;

In the order of 3, 2, and 1, the number of gold coins received by the minister who received the most prizes was 9.

Therefore, the minister with the most prizes should receive at least two gold coins, and the answer should be 2.

[Data Scope]

For 20% of the data, there are 1 ≤ n ≤ 10, 0 <a, B <8;

For 40% of the data, there are 1 ≤ n ≤ 20, 0 <a, B <8;

For 60% of data, 1 ≤ n ≤ 100;

For 60% of the data, ensure that the answer is no more than 10 ^ 9;

For 100% of data, there are 1 ≤ n ≤ 1,000, 0 <a, B <10000.

NOIP 2012 raise group first day Second question

 

 

Do not want to write .jpg.

The idea is simple, that is, to sort a * B from small to large.

1. proof:

1) Know that if two adjacent people exchange positions, they will only affect the values of the two people, but will not affect others.

2) Assume that two adjacent people I, I + 1. Set A [I]B [I] <= A [I + 1]B [I + 1], the product of the left hand of all people before I is S.

Then, ans1 = max {S/B [I], S * A [I]/B [I + 1]}

If

Then, ans2 = max {S/B [I + 1], S * A [I + 1]/B [I]}

Because, A [I]B [I] <= A [I + 1]B [I + 1]

So, SA [I]/B [I + 1] <= SA [I + 1]/B [I]

Because, S/B [I + 1] <= S * A [I]/B [I + 1]

Therefore, ans2 = S * A [I + 1]/B [I]

Ans1 = max {S/B [I], S * A [I]/B [I + 1]}

Therefore, ans1 <= ans2

2. proof:

Convert log to addition.

In fact, it is only related to the order of two adjacent persons.
They cannot influence people.
Let's take a look. Suppose there are two people, I, j.
The answer before j is:
Max {−b I, a I −b j}
I. The answer after j is:
Max {−b j, a j −b I}

Apparently −b j <a I −b j, −b I <a j −b I.
Therefore, a I −b j <a j −b I.
Therefore, a I + B I <a j + B j

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<queue> 7 #define lli long long int  8 using namespace std; 9 const lli MAXN=1001;10 inline void read(lli &n)11 {12     char c='+';lli x=0;bool flag=0;13     while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}14     while(c>='0'&&c<='9'){x=x*10+(c-48);c=getchar();}15     flag==1?n=-x:n=x;16 }17 struct node 18 {19     lli a,b;20     node(){a=0;b=0;}21 }pep[MAXN];22 lli comp(const node &a,const node &b)23 {24     return (a.a*a.b<b.a*b.b);25 }26 lli now=1;27 lli ans=0;28 int main()29 {30     lli n;31     read(n);32     read(pep[1].a);read(pep[1].b);33     for(lli i=2;i<=n+1;i++){read(pep[i].a);read(pep[i].b);}34     sort(pep+2,pep+n+2,comp);35     for(lli i=1;i<=n+1;i++)36     {37         ans=max(ans,now/(pep[i].b));38         now*=pep[i].a;39     }40     //printf("%lld",ans);41     cout<<ans;42     return 0;43 }

 

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.