跳到正文

题解

题解:P12218 [蓝桥杯 2023 国 Java B] 玩具

__lalala__约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
获赞
1
题目
P12218

版本

  1. v1当前

当我们将最小的数乘上最大的数时,就能让权重和最小,下面给出证明。

设 a≤b≤c≤d ,则有 {a×b+c×da×c+b×da×d+b×c三种情况(a×b+c×d)−(a×c+b×d)=a⋅(b−c)+d⋅(c−b)=(a−b)⋅(b−c)又因为 a≤b≤c≤d,所以a×b+c×d≥a×c+b×d同理可得 a×c+b×d≥a×d+b×c所以在 a≤b≤c≤d时,a×d+b×c为最优策略证毕\text{设 } a \le b \le c \le d \text{ ,则有 } \begin{cases} a\times b + c\times d \\ a\times c + b\times d \\ a\times d + b\times c \end{cases} \text{三种情况} \\ \begin{aligned} (a\times b + c\times d) - (a\times c + b\times d) &= a \cdot (b - c) + d \cdot (c - b) \\ &= (a - b) \cdot (b - c) \end{aligned} \\ \text{又因为 } a \le b \le c \le d \text{,所以} a\times b + c\times d \ge a\times c + b\times d \\ \text{同理可得 } a\times c + b\times d \ge a\times d + b\times c \\ \text{所以在 } a \le b \le c \le d \text{时,} a\times d + b\times c \text{为最优策略} \\ \text{证毕}

code

C++
#include<bits/stdc++.h>
#define int long long//不开long long见祖宗
using namespace std;
int a[1000010];
signed main(void)
{
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
	cin >> n;
	for(int i = 1;i <= 2 * n;i++)
	{
		cin >> a[i];
	}
	sort(a + 1, a + 1 + 2 * n);//从小到大排序
	int ans = 0;
	for(int i = 1;i <= n;i++)
	{
		ans += a[i] * a[2 * n - i + 1];//第i小乘上第i大
	}
	cout << ans;
    return 0;
}
原文