Mike and distribution CodeForces - 798D ——二维贪心

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 integers A?=?[a1,?a2,?…,?an] and B?=?[b1,?b2,?…,?bn] of length n 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 select k numbers P?=?[p1,?p2,?…,?pk] such that 1?≤?pi?≤?n for 1?≤?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 subset P “unfair” if and only if the following conditions are satisfied: 2·(ap1?+?…?+?apk) is greater than the sum of all elements from sequence A, and 2·(bp1?+?…?+?bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to Mike and distribution CodeForces - 798D ——二维贪心 - yftianyizhicheng - 天翼之城的博客 because it will be to easy to find sequence P 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 integers a1,?…,?an (1?≤?ai?≤?109) — elements of sequence A.

On the third line there are also n space-separated integers b1,?…,?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 Mike and distribution CodeForces - 798D ——二维贪心 - yftianyizhicheng - 天翼之城的博客.

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 sequence Pshould be distinct.

Example
Input
5
8 7 4 8 3
4 2 5 3 7
Output
3
1 4 5
这道题以前类似的做过,但是当时没有记下来,现在正好记下来。
就是先按a排序,再取出第一个,为什么要取第一个?因为他的数的个数最大可以取n/2+1,然后万一a的最大的数比其他的和还大,就会有问题,所以这两个就可以推出来是1,然后一个一个比较b的大小,只要保证每两个数的b都是取到比较大的哪一个,就一定是大于一半的。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
     int a,b,xb;
     bool operator< (const node& x)const 
     {
          if(a==x.a)
              return b>x.b;
          return a>x.a;
     } 
}e[100005];
int main()
{
     int n;
     scanf("%d",&n);
     for(int i=1;i<=n;i++)
     {
          scanf("%d",&e[i].a);
          e[i].xb=i;
     }
     for(int i=1;i<=n;i++)
         scanf("%d",&e[i].b);
     sort(e+1,e+n+1);
     printf("%d\n",n/2+1);
     printf("%d ",e[1].xb);
     for(int i=2;i<=n;i+=2)
         printf("%d ",e[i].b>e[i+1].b?e[i].xb:e[i+1].xb);
     printf("\n");
     return 0; 
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/82047881