[CSP-S Simulation Test]: E (greedy)

Topic Portal (internal title 48)


Input Format

A first line integer $ n $.
N-$ $ next two lines each integers $ x_i, y_i $.


Output Format

A row of integer answer.


Sample

Sample input $ 1 $:

2
3 7
2 5

Sample output of $ 1 $:

2

Sample input $ 2 $:

5
5 15
11 16
16 34
2 14
9 17

$ 2 $ sample output:

96


Data range and tips

Sample $ 1 $ explanation:

The first group is the right-ball $ 3 $ stained red, the right value of $ 7 $ stained blue ball.
The first group is the right-ball $ 2 $ stained red, with a weight of 5 $ $ stained blue ball.
$ (R_ {max} -R_ { min}) \ times (B_ {max} -B_ {min}) = (3-2) \ times (7-5) = 2 $

data range:

For data before $ 10 \% $ a: $ n \ leqslant 20 $
for the data before $ 20 \% $ a: $ n \ leqslant 50 $
data for the first $ 40 \% $ a: $ n \ leqslant 200 $
for the first $ 40 \% data $: $ n \ leqslant 2,000 $
for all data:
$. 1 \ leqslant. 5 ^ {10} $
$. 1 \ leqslant x_i, y_i \ leqslant. 9 $ ^ {10}


answer

Consider greedy.

Because all of the maximum and minimum number will certainly contribute to answer, so the two situations:

  $ \ Alpha. $ Maxima and minima is not a color, this time we just need each group $ x_i, y_i $ election into a smaller blue, red can be selected larger.

  $\beta.$最大值和最小值是一种颜色(设为红色),这时候我们要最小化蓝色的极差,枚举蓝色球的最小值,二分求出最大值即可。

对于这道题的数据,只用考虑情况$\alpha$即可,因为不确定我的代码的情况$\beta$的正确性,所以下面代码只考虑了情况$\alpha$。

时间复杂度:$\Theta(n\log n)$(情况$\alpha$为$\Theta(n)$)。

期望得分:$100$分。

实际得分:$100$分。


代码时刻

#include<bits/stdc++.h>
using namespace std;
int n;
int minr=1<<30,minb=1<<30,maxr,maxb;
int main()
{
	scanf("%d",&n);
	while(n--)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		if(x<y)x^=y^=x^=y;
		minr=min(minr,x);
		maxr=max(maxr,x);
		minb=min(minb,y);
		maxb=max(maxb,y);
	}
	cout<<1LL*(maxr-minr)*(maxb-minb);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11557395.html