Codeforces 798D Mike and distribution【思维+贪心】好题!好题!感觉智商受到了一万点伤害= =

时间:2023-02-12 18:27:15

D. Mike and distributiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integersA = [a1, a2, ..., an] andB = [b1, b2, ..., bn] of lengthn each which he uses to ask people some quite peculiar questions.

To test you on how good are you at spotting inequality in life, he wants you to find an"unfair" subset of the original sequence. To be more precise, he wants you to selectk numbers P = [p1, p2, ..., pk] such that1 ≤ pi ≤ n for1 ≤ i ≤ k and elements in P are distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subsetP "unfair" if and only if the following conditions are satisfied:2·(ap1 + ... + apk) isgreater than the sum of all elements from sequenceA, and 2·(bp1 + ... + bpk) is greater than the sum of all elements from the sequenceB. Also, k should be smaller or equal toCodeforces 798D Mike and distribution【思维+贪心】好题!好题!感觉智商受到了一万点伤害= = because it will be to easy to find sequenceP if he allowed you to select too many elements!

Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity!

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the sequences.

On the second line there are n space-separated integersa1, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.

On the third line there are also n space-separated integersb1, ..., bn (1 ≤ bi ≤ 109) — elements of sequence B.

Output

On the first line output an integer k which represents the size of the found subset.k should be less or equal to Codeforces 798D Mike and distribution【思维+贪心】好题!好题!感觉智商受到了一万点伤害= =.

On the next line print k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the elements of sequence P. You can print the numbers in any order you want. Elements in sequenceP should be distinct.

ExampleInput
5
8 7 4 8 3
4 2 5 3 7
Output
3
1 4 5

题目大意:

给你长度为N的两个数组,让你找到n/2+1个数,使得选出的数分别在两个数组的和*2>两个数组各自的和。


思路:


1、首先我们按照A数组从大到小排序。

然后对于要求我们进行贪心。


2、要求为:使得选出的数分别在两个数组的和*2>两个数组各自的和。那么我们转化问题变成:使得选出的数分别在两个数组中的和,大于剩下没有选的数的和。

我们首先贪心的将第一个数纳入答案中。

如果此时n%2==1.那么剩余n-1(偶数个)数,那么我们将其两两相邻的数分成一组.那么我们贪心处理只要拿一组中B值较大的那一个即可。

如果此时n%2==0.那么剩余n-1(奇数个)数,那么我们同理将相邻两个数分成一组,那么会剩余一个数,我们直接选他纳入答案即可,剩余部分每组选出B值较大的那一个。


简单证明:

对于A数组的选择,我们将第一个数贪心的纳入了答案,那么对于后边两个相邻的数中,我们无论选择了哪个A,我们第一个数都能大于这个A.同理往后是一样的。

对于B数组的选择,我们两两相邻的数都选择了较大的那一个数,而且还有多选出来的数,那么选出来的值一定是一半以上的。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int x,y,pos;
}a[150000];
int ans[150000];
int cmp(node a,node b)
{
return a.x>b.x;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
int cnt=1;
for(int i=1;i<=n;i++)scanf("%d",&a[i].x),a[i].pos=i;
for(int i=1;i<=n;i++)scanf("%d",&a[i].y);
sort(a+1,a+1+n,cmp);
ans[cnt++]=a[1].pos;
for(int i=2;i<n;i+=2)
{
if(a[i].y>a[i+1].y)
{
ans[cnt++]=a[i].pos;
}
else ans[cnt++]=a[i+1].pos;
}
if(n%2==0)ans[cnt++]=a[n].pos;
printf("%d\n",n/2+1);
for(int i=1;i<cnt;i++)printf("%d ",ans[i]);
printf("\n");
}
}