Question A: Just Long Neckties ----------------------------- Thinking

题目描述
Have you ever heard of Just Odd Inventions, Ltd.? This company is known for their “just odd inventions.” We call it JOI, Ltd. in this problem.
JOI, Ltd. has invented its newest product “Just Long Neckties”. There are N + 1 types of neckties, numbered 1 to N + 1. The length of the i-th necktie (1 ≤ i ≤ N + 1) is Ai.
The company gathered their employees to hold a try-on party. N employees participate in the party, and the j-th employee (1 ≤ j ≤ N) initially wears a necktie of length Bj.
The try-on party is held following this procedure:

  1. CEO of JOI, Ltd. chooses a necktie, which is not used at the party.
  2. Then, each employee chooses one of the remaining neckties to try on. No two employees choose the same necktie.
  3. Finally, each employee takes off the necktie which (s)he initially wears and puts the selected necktie on.
    If an employee initially wearing a necktie of length b tries a necktie of length a, (s)he feels strangeness of max { a − b , 0 } . The oddity of the try-on party is defined as the maximum strangeness among the employees.
    We also define Ck as the minimum oddity of the try-on party if CEO of JOI, Ltd. chooses the k-th necktie.
    Write a program which, given the lengths of the neckties used at the party and the neckties each employee initially wears, calculates the values of C1 , C2 , . . . , CN + 1.
    输入
    Read the following data from the standard input. Given values are all integers.
    N
    A1 . . . AN + 1
    B1 . . . BN
    Constraints
    • 1 ≤ N ≤ 200,000.
    • 1 ≤ Ai ≤ 1,000,000,000 (1 ≤ i ≤ N + 1).
    • 1 ≤ Bj ≤ 1,000,000,000 (1 ≤ j ≤ N).

输出
Write one line to the standard output. The output should contain the values of C1 , C2 , . . . , CN + 1, separated by a space.
样例输入 Copy
【样例1】

3
4 3 7 6
2 6 4
【样例2】
5
4 7 9 10 11 12
3 5 7 9 11
样例输出 Copy
【样例1】

2 2 1 1
【样例2】
4 4 3 2 2 2
提示
样例1解释

Here is an example of a try-on party:
• CEO of JOI, Ltd. chooses the 4th necktie. This necktie is not used in the party.
• The employee 1 chooses the 1st necktie, the employee 2 chooses the 2nd necktie, the employee 3 chooses the 3rd necktie.
• Each employee tries the necktie they choose on.
In this case, strangeness of each employee is 2, 0, 3 in order. Therefore, the oddity of the party is 3.
It is possible to decrease the oddity to 1 if the employees choose di ff erent neckties. One of the example is:
• CEO of JOI, Ltd. chooses the 4th necktie. This necktie is not used in the party.
• The employee 1 chooses the 2nd necktie, the employee 2 chooses the 3rd necktie, the employee 3 chooses the 1st necktie.
• Each employee tries the necktie they choose on.
In this case, strangeness of each employee is 1, 1, 0 in order. Therefore, the oddity of the party is 1.
This is the minimum possible oddity when CEO of JOI, Ltd. chooses the 4th necktie, so C4 = 1.

Analysis:
sort first, then maintain the maximum value of the prefix and the maximum value of the suffix, then
enumerate i in turn

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1000;
int c[N],b[N];
int lb[N];
int la[N],ra[N];
int n;
struct node
{
	int x,pos;
}a[N];
bool cmp(const node &a,const node &b)
{
	return a.x<b.x;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n+1;i++)
	{
		scanf("%d",&a[i].x);
		a[i].pos=i;
	}
	sort(a+1,a+1+n+1,cmp);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&b[i]);
	}
	sort(b+1,b+1+n);
	for(int i=1;i<=n;i++)
	{
		la[i]=max(la[i-1],a[i].x-b[i]);

	}
	for(int i=n+1;i>1;i--)
	{
		ra[i]=max(ra[i+1],a[i].x-b[i-1]);
	}

	for(int i=1;i<=n+1;i++)
	{
		c[a[i].pos]=max(la[i-1],ra[i+1]);
	} 
	for(int i=1;i<=n+1;i++)
	{
		cout<<c[i]<<" ";
	}
	cout<<endl;
}
Published 572 original articles · praised 14 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_43690454/article/details/105271226