1 条题解

  • 0
    @ 2025-11-23 21:53:41

    C++ :

    #include <bits/stdc++.h>
    #define endl '\n'  //宏定义换行
    using namespace std;
    
    const int N = 2e5+9;  //根据题目要求定义大小
    int n, a[N], b[N], c[N];
    /*
     * a:小 B 的出价
     * b:小 C 的出价
     * c:小 C 来买对答案的贡献
     */
    long long ans; //千万别忘记 long long
    
    int main() {
    	ios::sync_with_stdio(false);//提高cin的速度
    	cin.tie(0);
    	cin >> n;
    	for (int i = 1; i <= n * 2; i++)
    		cin >> a[i];
    	for (int i = 1; i <= n * 2; i++)
    		cin >> b[i];
    	for (int i = 1; i <= n * 2; i++) {
    		// 默认小 B 进行购买
    		ans += a[i];
    		// 同时计算此时小 C 购买的贡献
    		c[i] = b[i] - a[i];
    	}
    	// 最大化贡献 使答案更优
    	sort(c + 1, c + 1 + n * 2);
    	// 选最后 n 个就是最大的答案了
    	for (int i = n * 2; i >= n + 1; i--)
    		ans += c[i];
    	cout << ans << endl;
    	return 0;
    }
    
    • 1

    信息

    ID
    5645
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者