题目描述
牛牛和 牛可乐 面前有 n 个物品,这些物品编号为 1,2,…,n1,2,\dots,n1,2,…,n ,每个物品有两个属性 ai,bia_i, b_iai,bi 。
牛牛与 牛可乐会轮流从剩下物品中任意拿走一个, 牛牛先选取。
设 牛牛选取的物品编号集合为 H,牛可乐选取的物品编号的集合为 T,取完之后,牛牛 得分为 ∑i∈Hai\sum_{i\in H} a_i∑i∈Hai;而 牛可乐得分为 ∑i∈Tbi\sum_{i\in T} b_i∑i∈Tbi。
牛牛和 牛可乐都希望自己的得分尽量比对方大(即最大化自己与对方得分的差)。
你需要求出两人都使用最优策略的情况下,最终分别会选择哪些物品,若有多种答案或输出顺序,输出任意一种。
输入描述:
第一行,一个正整数 n,表示物品个数。
第二行,n 个整数 a1,a2,…,ana_1,a_2,\dots,a_na1,a2,…,an,表示 n 个物品的 A 属性。
第三行,n 个整数 b1,b2,…,bnb_1,b_2,\dots,b_nb1,b2,…,bn,表示 n 个物品的 B 属性。
保证 2≤n≤2×1052\leq n\leq 2\times 10^52≤n≤2×105,0≤ai,bi≤1090\leq a_i,b_i\leq 10^90≤ai,bi≤109。
输出描述:
输出两行,分别表示在最优策略下 牛牛和 牛可乐各选择了哪些物品,输出物品编号。
示例1
输入
复制
3
8 7 6
5 4 2
输出
复制
1 3
2
说明
3 1
2
也会被判定为正确
本以为a, b属性的差值是关键, 每次选择差的绝对值最大的物品就行, 后来发现是错的。。。
#include <algorithm>
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
const int maxn = 2e5 + 10;
struct node{
int id, x, y;
}a[maxn];
int a1[maxn], a2[maxn];
int n, t1, t2;
struct cmp{
bool operator() (node x, node y) {
return x.x + x.y < y.x + y.y;
}
};
int main() {
cin >> n;
for(int i = 0; i < n; i++)
cin >> a[i].x;
for(int i = 0; i < n; i++)
cin >> a[i].y, a[i].id = i + 1;
priority_queue<node, vector<node>, cmp>que;
for(int i = 0; i < n; i++)
que.push(a[i]);
for(int i = 1; i <= n; i++) {
node tem = que.top();
if(i & 1)
a1[t1++] = tem.id;
else
a2[t2++] = tem.id;
que.pop();
}
for(int i = 0; i < t1; i++)
cout << a1[i] << " ";
cout << endl;
for(int i = 0; i < t2; i++)
cout << a2[i] << " ";
return 0;
}