Mike and distribution

Source: Internet
Author: User
Tags arrays

Description:

Mike had always been thinking about the harshness of social inequality. He's so obsessed with it, sometimes it even affects him while solving problems. At the moment, Mike had a sequences of positive integers A = [A1, a2, ..., an] and B = [B1, B2, ..., bn] of length n EAC H which he uses to ask people some quite peculiar questions.

To test what good is spotting inequality in life, he wants you to find an  "unfair"  subset of th e original sequence. To is more precise, he wants you to select k numbers p = [P1, p2, ..., Pk] such THAT 1≤PI≤N&N Bsp;for 1≤i≤k and elements in p are Distinct. Sequence p will represent indices of elements so you'll select from both sequences. He calls such a subset p  "unfair"  if and only if the following conditions is 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  because it'll be-to-find sequence p if he a Llowed to select too many elements!

Mike guarantees you a solution would always exist given the conditions described above and so please help him satisfy he curiosity! Input

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

On the second line there is n space-separated integers a1, ..., an (1≤ai≤109)-elements of sequence A.

On the third line there is 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 is less or equal to.

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 is distinct. Example Input

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

equation variants:

2*x > X+y ===> x>y (X is the sum of the numbers corresponding to the selected subscript in the array, and Y is the sum of the corresponding numbers in the array for the unselected subscript)


Thinking of solving problems:

Essentially is to find out the number of n/2+1, and for this selected set of numbers, in the non-selected number can be found in the corresponding to a number less than it (so the corresponding to two numbers for a group, a number from the selected set, a number from the unchecked collection), This ensures that the selected number is greater than the remaining unselected number, because a>b------> a*2 > a+b = SUM (A is a combination of the selected set elements, and B is the sum of the unchecked set elements) to meet the requirements of the topic. Example: 1,4,9,6,5,2, check 9,4,5, unchecked is 1,6,2, there is 9>6,4>1,5>2, so the number chosen to meet the requirements. (It can be seen here that each group of case solutions may have multiple)


Problem Solving steps:

To sort any of these arrays, select the A array here. Primitive, the elements of A and B arrays are one by one corresponding to the array of a, the corresponding B-array elements should be moved along with the elements of a array.


Here order is an array of structs


1. After ordering, Order[0] is added to the selected set.

2. Then the order array two elements as a division, namely 12,34, 56, 7.

3. Select an element in each partition and choose the rule: Select the element with the big B value. Thus, the selected order subscript is 0,2,3,6,7. Then we can find the corresponding original subscript.


Explanation:

For the B array, it is clear that the 3rd step will ensure that we find the idea of a>b, because there is a corresponding smaller number when the large number is chosen in each of the two element divisions.

For a array, we look for each pair of a>b in the same partition as the B array, but across the partition, as shown in the figure. Because the a array is ordered, the comparison of such cross-partitioning is certain to satisfy the a>b. (√ indicates selected, _ indicates not selected).



Two arrays are satisfied, then the solution is satisfied.




Code:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL Long Long
#define PI ACOs ( -1.0)
using namespace Std;

struct node
{
  int a,b,f;
} arr[100005];

BOOL CMP (node x, node Y)
{
  return x.a>y.a;
}

int main ()
{
  int n;

  while (~SCANF ("%d", &n))
    {for

      (int i=0; i<n; i++)
        {
          scanf ("%d", &arr[i].a);
          arr[i].f=i+1;
        }

      for (int i=0; i<n; i++)
        scanf ("%d", &arr[i].b);

      Sort (arr,arr+n,cmp);

      printf ("%d\n%d", n/2+1,arr[0].f);

      arr[n].b=-1;//Note that n is an odd even difference for

      (int i=1; i<n; i+=2)
        {
          if (arr[i].b>arr[i+1].b)
            printf ("%d", ARR[I].F);

          else
            printf ("%d", arr[i+1].f);
        }

      printf ("\ n");
    }
  return 0;
} From CJZ


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.