PTA Birthday Gift (sort + hypothesis to reduce variables)

7-12 Birthday Gift (20分)

On Derrick’s birthday, Derrick received a gift from his girlfriend Sandy. The gift consists n tiny balls, and each of them has two characteristics a and b. As Derrick is boring, he just wants to play with these balls.

Each time Derrick chooses two balls randomly. For each pair of balls x, y, Derrick defines their beautiful value as min(X​a + Yb, Xb + Ya).

Given the properties a and b of all the n tiny balls, Derrick will select exactly two of them and maximum their beautiful value. Now he needs your help, please help him.

Input Specification:

Each input file only contains one test case.

The first line contains an integer n (2 ≤ n ≤ 2×10
​5​​ ), indicating the total number of the tiny balls.

The second line contains n integers a
​1​​ ,a​2​​ ,…,a​n​​ (1 ≤ a​i​ ≤ 10​9​​ ), indicating the characteristic a of each ball.

The third line contains n integers b​1​​ ,b​2​​ ,…,b​n​​ (1 ≤ b​i​​ ≤ 10​9​ ), indicating the characteristic b of each ball.

Output Specification:

For each test case, output a single integer in a line indicating the maximum beautiful value of the given tiny balls.

Sample Input:

5
1 3 5 4 2
2 4 3 5 3

Sample Output:

8

Problem solving

Thank you for your thoughts: https://blog.csdn.net/weixin_45750972/article/details/106547534

  1. First analyze the meaning of the question. If you use a violent solution, each ball must be compared with the other balls. Now change the question to find the first ball (x ball) and the other n-1 balls (y ball) to get the most Merit. Obviously, the value formed by him and each ball is min(xa + yb, xb + ya), that is, there are two possibilities, that is, the x ball chooses the a property, or the x ball chooses the b property, then do The thing is to convert two choices into one choice, so I use the ideas of the big guys.
  2. For min(xa + yb, xb + ya), now suppose that two balls of x and y are selected to compare, and the first ball with a property + the other ball with a small property b (the result of both taking min), we The first ball is always called the x ball, and the second ball is called the y ball, so the following assumptions are made:
    for any x, y ball satisfies xa + yb <= xb + ya (if not satisfied, take X , Y ball exchange to make it satisfy, here just means that this assumption can be established);
    then by (xa + yb <= xb + ya) => (xa-xb <= ya-yb), now the ball is in this form Sort, that is, each ball in the front is satisfied with the ball in the back, the ball in front is the ball of X and the ball behind is the ball of Y, that is,
    xa + yb <= xb + ya, the constant is established
    and then Traverse from front to back, each time the ball (X ball) is traversed, the largest Yb behind the ball is calculated as the optimal solution of the ball and the back, and then the maximum value is the optimal solution for all balls .
  3. Use the rk[i] array to represent the largest Yb starting from i

Code

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int maxn = 2e5 + 2;
// int a[maxn], b[maxn];
int rk[maxn];

struct node {
    
    
    int a, b;
} ball[maxn];

bool cmp(const node &x, const node &y) {
    
     return x.a - x.b < y.a - y.b; }

int main() {
    
    
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
    
    
        scanf("%d", &ball[i].a);
    }
    for (int i = 1; i <= n; i++) {
    
    
        scanf("%d", &ball[i].b);
    }
    sort(ball + 1, ball + n + 1, cmp);
    // for (int i = 1; i <= n; i++) {
    
    
    //     cout << ball[i].a << " " << ball[i].b << endl;
    // }

    for (int i = n; i >= 1; i--) {
    
    
        rk[i] = max(rk[i + 1], ball[i].b);  // rk[i], 从下标i开始后面最大的y.b
    }

    // 相当于把每对小球之间的比较有两种情况,全部转换为一种情况了
    int ans = 0;
    for (int i = 1; i < n; i++) {
    
    
        ans = max(ans, rk[i + 1] + ball[i].a);
    }
    cout << ans << endl;

    system("pause");
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45349225/article/details/109488357