On Neighbor Exchange Order

@

concept

Neighbor exchange sorting is a common greedy algorithm. It generalizes the decision to determine the adjacent two elements to the entire sequence to obtain the optimal solution.

Ⅰ: P1080 king game

Title Link
Title Description
Coincides with the National Day of H, the king invited n ministers to play a prize game. First, he asked each minister to write an integer on the left and right hands, and the king himself also wrote an integer on the left and right hands. Then, let the n ministers line up, and the king stands at the front of the line. After lining up, all ministers will receive a number of gold coins rewarded by the king. The number of gold coins received by each minister is: the product of the number on the left hand of everyone in front of the minister divided by the number on his own right hand, Then round down the result.

The king does not want a certain minister to get particularly many rewards, so he wants you to help him rearrange the order of the team so that the minister who receives the most rewards receives as few rewards as possible. Note that the position of the king is always at the front of the team.

Input format:

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

The second line contains two integers a and b, separated by a space to represent the integers on the left and right hands of the king.

In the next n lines, each line contains two integers a and b, separated by a space to represent the integers on the left and right hands of each minister.

Output format:

An integer representing the number of gold coins received by the most awarded minister in the rearranged team.

Input sample # 1:
3
1 1
2 3
7 4
4 6
Output sample # 1:
2

【data range】

For 20% of the data, 1≤ n≤ 10,0 <a, b <81≤n≤10,0 <a, b <8;

For 40% of the data, 1≤n≤20,0 <a, b <81≤n≤20,0 <a, b <8;

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

For 60% of the data, ensure that the answer does not exceed 10 ^ 9109;

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

Ideas
Let i and j be two adjacent ministers, and let the product of the numbers on the left hand of all the ministers in front of them be k , then
① If i is in front of j , then the greater value of the two ministers ’rewards is

\[max(\frac{k}{b~i},\frac{k*a~i}{b~j}) \]

② If i is behind j , the greater value of the two ministers ’rewards is

\[max(\frac{k}{b~j},\frac{k*a~j}{b~i}) \]

Because the problem requires that the maximum value is the smallest, we set i to be better in front of j , then we need to meet

\[max(\frac{k}{b~i},\frac{k*a~i}{b~j}) \leqslant max(\frac{k}{b~j},\frac{k*a~j}{b~i}) \]

Again

\[\frac{k}{b~i} \leqslant \frac{k*a~j}{b~i},\frac{k}{b~j} \leqslant \frac{k*a~i}{b~j} \]

Then the above formula is equivalent to

\[\frac{k*a~i}{b~j} \leqslant \frac{k*a~j}{b~i},即a~i*b~i \leqslant a~j*b~j \]

Then generalize this formula (decision) to the entire sequence, that is, according to this priority order, then the final solution must be the optimal solution.

Some may wonder why may be extended to the whole sequence? What if i and j are not adjacent?

For the entire sequence, all the neighbors of the sequence need to meet the above decision, which is equivalent to sorting according to this decision

If we do n’t understand it, let ’s simulate it. Above, we have proved that any two adjacent elements need to satisfy the above formula to ensure the optimal solution of these two elements. We first arrange the order according to the above decision, then for any two Non-adjacent elements i and j (assuming that j is behind i ), swapping elements at two positions, it will no longer be satisfied

\[a~i*b~i \leqslant a~j*b~j \]

Also no longer satisfied

\[\left\{\begin{matrix}\tag{1} a_{i}*b_{i} \leqslant a_{i+1}*b_{i+1} \\ a_{i}*b_{i} \leqslant a_{i+2}*b_{i+2} \\ ... \\ a_{i}*b_{i} \leqslant a_{j}*b_{j} \end{matrix}\right.\]

with

\[\left\{\begin{matrix}\tag{2} a_{i}*b_{i} \leqslant a_{j}*b_{j} \\ a_{i+1}*b_{i+1} \leqslant a_{j}*b_{j} \\ ... \\ a_{j-1}*b_{j-1} \leqslant a_{j}*b_{j} \end{matrix}\right.\]

For equation (1): the elements of the inequality in the first line are adjacent, we exchange their elements, so that the first line is satisfied, and the elements in the second line are adjacent, we exchange. . Until the original element on i has reached the position of j , and the original elements from i to j are moved forward by one. At this time, the exchange order of neighbors completely satisfies (1); the
same is true for (2) Equation: The elements of the inequality in the last line are adjacent, we exchange their elements, and then the elements in the penultimate line are adjacent, and then exchange ... until the original element on j reaches the i position, then use the neighbor exchange The sorting completely satisfies the formula (2).
From this: there is no neighbor order in the entire sequence that does not satisfy our decision.

The above questions seem to have explained the essence of neighbor exchange: launching decision , then look at

Ⅱ: P2123 Queen Game

Topic Link
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
meaning of the questions, we set minister i and j adjacent to a minister of the previous two ministers bonus is C, and the number of the previous two ministers all ministers left for the sum, then
① if i in j front, Then the greater value of the two ministers ’bonuses is $$ max (max (C, sum + a_ {i}) + b_ {i}, sum + a_ {i} + a_ {j}) + b_ {j} , Which is $$

\[max(max(C,sum+a_{i})+b_{i}+b_{j},sum+a_{i}+a_{j}+b_{j})\tag{1} \]

② If i is behind j , the greater value of the two ministers ’rewards is $$ max (max (C, sum + a_ {j}) + b_ {j}, sum + a_ {i} + a_ {j }) + b_ {i}, which is $$

\[max(max(C,sum+a_{j})+b_{j}+b_{i},sum+a_{i}+a_{j}+b_{i})\tag{2} \]

The problem is that the maximum value is the smallest, we set i to be better in front, then there is

\[max(max(C,sum+a_{i})+b_{i}+b_{j},sum+a_{i}+a_{j}+b_{j}) \leqslant max(max(C,sum+a_{j})+b_{j}+b_{i},sum+a_{i}+a_{j}+b_{i}) \]

Take apart the max inside, ie

\[max(C+b_{i}+b_{j},sum+a_{i}+b_{i}+b_{j},sum+a_{i}+a_{j}+b_{j}) \leqslant max(C+b_{j}+b_{i},sum+a_{j}+b_{j}+b_{i},sum+a_{i}+a_{j}+b_{i}) \]

Of which

\[C+b_{i}+b_{j} \]

Eliminate (explained later why it can be eliminated), and then become

\[max(sum+a_{i}+b_{i}+b_{j},sum+a_{i}+a_{j}+b_{j}) \leqslant max(sum+a_{j}+b_{j}+b_{i},sum+a_{i}+a_{j}+b_{i}) \]

Eliminate sum

\[max(a_{i}+b_{i}+b_{j},a_{i}+a_{j}+b_{j}) \leqslant max(a_{j}+b_{j}+b_{i},a_{i}+a_{j}+b_{i}) \]

Simplify, get

\[max(b_{i},a_{j})+a_{i}+b_{j} \leqslant max(b_{j},a_{i})+a_{j}+b_{i} \]

Shift item

\[max(b_{i},a_{j})-a_{j}-b_{i} \leqslant max(b_{j},a_{i})-a_{i}-b_{j} \]

Observe that the larger numbers are eliminated on both the left and right sides, leaving the opposite of the smaller numbers.

\[-min(b_{i},a_{j})\leqslant -min(b_{j},a_{i}) \]

Erasure

\ [min (b_ {i}, a_ {j}) \ geqslant min (b_ {j}, a_ {i}) \]

It seems that we have found the final simplest decision, and then extended it to the entire sequence as well to obtain the optimal solution.

Why can we eliminate the same term, we can simplify the above formula to

\[max(a1,b1,c) \leqslant max(a2,b2,c) \]

So if c is the largest of all numbers, then the left and right sides are equal. It is the same whether you change or not.
If c is not the largest of all numbers, you do n’t care about the existence of c, because it is only determined that this inequality is true. Elements larger than c.
So, you can eliminate c

However, this decision is wrong. But so much is wrong with incomplete describe better,
the standard explanation is that because this decision does not meet the strict weak ordering is not comparable transitive . We directly explain with a set of data

\[\left\{\begin{matrix}a_{1}=7 , b_{1}=3 \\ a_{2}=1 , b_{2}=1 \\ a_{3}=1 , b_{3}=6 \end{matrix}\right.\]

\ [Explicit, min (a_ {1}, b_ {2}) = min (b_ {1}, a_ {2}), min (a_ {2}, b_ {3}) = min (b_ {2} , a_ {a}) \]

\ [However, min (a_ {1}, b_ {3})! = Min (b_ {1}, a_ {3}) \]

This explains what simple transitive not comparable , i.e. \ (a <= b, b <= c \) can not Release \ (A <= \ C) , this case can not be directly generalized to the entire sequence the need to add something to satisfy not comparable transitive job.

We know that the conflict is caused by \ (min (b_ {i}, a_ {j}) = min (b_ {j}, a_ {i}) \) Whether the neighbors should be exchanged and ambiguous, and then analyze the questions given The formula
Insert picture description here
can find that the prefix of a has a certain influence on the answer, so when they are equal, putting a small in front will be a better choice; similarly, b must also have an effect on the answer, and each b will be calculated Once, when equal, it is better to put b in front of it. Then two solutions (ie two correct decisions) can now be drawn:

===========================================================
#include<bits/stdc++.h>
using namespace std;

#define  I inline
#define rg register
#define ll long long

I ll rd()
{
	ll x=0,f=0; char c=getchar();
	while(!isdigit(c)){f|=c=='-';c=getchar();}
	while( isdigit(c)){x=(x<<3)+(x<<1)+(c-48);c=getchar();}
	return f?-x:x;
}

const int N = 2E4+10;

struct node{
	ll a,b;
	bool operator < (const node& x)const{
	   return min(a,x.b)==min(x.a,b)?a<x.a:min(a,x.b)<min(x.a,b);  //决策 1
	   return min(a,x.b)==min(x.a,b)?b>x.b:min(a,x.b)<min(x.a,b);  //决策 2
	}
}e[N];

int n;

int main()
{
	int T=rd();
	while(T--)
	{
		n=rd();
		for(int i=1;i<=n;i++) e[i].a=rd(),e[i].b=rd();
		sort(e+1,e+n+1);
		ll sum=e[1].a+e[1].b,pre=e[1].a;
		for(int i=2;i<=n;i++)
			pre+=e[i].a,sum=max(sum,pre)+e[i].b;
		printf("%lld\n",sum);
	}
}

For each group \ ((a_ {i}, b_ {j}) \) the relationship between the two elements also affects the answer, we try to establish

\[d=\left\{\begin{matrix}-1 ,a_{i}<b_{i} \\ 0 ,a_{i}=b_{i} \\ 1 ,a_{i}>b_{i} \end{matrix}\right.\]

Here, all elements are divided into three categories, according to \ (min (b_ {i}, a_ {j}) \ leqslant min (b_ {j}, a_ {i}) \) to judge:
\ (① d_ {i} = d_ {j} = -1, arrange in ascending order of a; \)
\ (② d_ {i} = d_ {j} = 0, arrange randomly; \)
\ (③ d_ {i} = d_ {j} = 1, in descending order of b; \) The
remaining \ (d_ {i}! = D_ {j} \) can be satisfied as long as \ (d_ {i} <d_ {j} \) is satisfied \ (min (b_ {j}, a_ {i}) \ leqslant min (b_ {i}, a_ {j}) \) , in summary, the new decision not only fully satisfies the constraints of the old decision, but also satisfies the transmission of not comparable (not difficult to find, after the classification of elements, each type having a transfer between the element and also having transfer resistance between classes), this way is obtained a third solution.

===========================================================
#include<bits/stdc++.h>
using namespace std;

#define  I inline
#define rg register
#define ll long long

I ll rd()
{
	ll x=0,f=0; char c=getchar();
	while(!isdigit(c)){f|=c=='-';c=getchar();}
	while( isdigit(c)){x=(x<<3)+(x<<1)+(c-48);c=getchar();}
	return f?-x:x;
}

const int N = 2E4+10;

struct node{
	ll a,b,d;
	bool operator < (const node& x)const{
	   if(d==x.d)
	   {
	       if(d<=0) return a<x.a;
	       else return b>x.b;
	   }
	   return d<x.d;
	}
}e[N];

int n;

int main()
{
	int T=rd();
	while(T--)
	{
		n=rd();
		for(int i=1;i<=n;i++){
			e[i].a=rd(),e[i].b=rd();
			if(e[i].a<e[i].b)  e[i].d=-1;
			if(e[i].a==e[i].b) e[i].d=0;
			if(e[i].a>e[i].b)  e[i].d=1;
		}
		sort(e+1,e+n+1);
		ll sum=e[1].a+e[1].b,pre=e[1].a;
		for(int i=2;i<=n;i++)
			pre+=e[i].a,sum=max(sum,pre)+e[i].b;
		printf("%lld\n",sum);
	}
}

Ⅲ: P1248 processing production scheduling

Title link
$ Title description $
Insert picture description here

Analysis: This is a typical pipeline scheduling problem. With the idea of ​​neighbor exchange, we first assume that there are only two products, and the processing time is \ ((a_ {i}, b_ {i}) and (a_ {j }, b_ {j}) \) , then
①If i is in front of j , the required time is \ (a_i + max (a_j, b_i) + b_j \) ;
②If i is behind j , the required time is \ ( a_j + max (a_i, b_j) + b_i \) ;
Now we assume that the former scheme is better, and there should be

\[a_i+max(a_j,b_i)+b_j \leqslant a_j+max(a_i,b_j)+b_i \]

Shift item

\[max(a_j,b_i)-a_j-b_i \leqslant max(a_i,b_j)-a_i-b_j \]

Both the left and right sides are equivalent to leaving a small opposite number, ie

\[-min(a_j,b_i) \leqslant -min(a_i,b_j) \]

Eliminate the minus sign eventually

\[min(a_i,b_j) \leqslant min(a_j,b_i) \]

Similarly, this decision certainly does not meet the passing of not comparable , and the solution as a problem, not repeat them.

===========================================================
#include<bits/stdc++.h>
using namespace std;

#define  I inline
#define rg register
#define ll long long

I ll rd()
{
	ll x=0,f=0; char c=getchar();
	while(!isdigit(c)){f|=c=='-';c=getchar();}
	while( isdigit(c)){x=(x<<3)+(x<<1)+(c-48);c=getchar();}
	return f?-x:x;
}

const int N = 2E4+10;

struct node{
	ll a,b,d,id;
	bool operator < (const node& x)const{
	   if(d==x.d)
	   {
	       if(d<=0) return a<x.a;
	       else return b>x.b;
	   }
	   return d<x.d;
	}
}e[N];

int n;

int main()
{
	n=rd();
	for(int i=1;i<=n;i++) e[i].a=rd();
	for(int i=1;i<=n;i++) e[i].b=rd();
	for(int i=1;i<=n;i++){
		e[i].id=i;
		if(e[i].a<e[i].b)  e[i].d=-1;
		if(e[i].a==e[i].b) e[i].d=0;
		if(e[i].a>e[i].b)  e[i].d=1;
	}
	sort(e+1,e+n+1);
	ll sum=e[1].a;
	for(int i=2;i<=n;i++)
	    if(e[i].a>=e[i-1].b) sum+=e[i].a;
		else e[i+1].a-=(e[i-1].b-e[i].a),sum+=e[i-1].b; 

	sum+=e[n].b;
	printf("%lld\n",sum);
	for(int i=1;i<=n;i++) printf("%d%c",e[i].id,i==n?'\n':' '); 
}

Guess you like

Origin www.cnblogs.com/17134h/p/12706408.html