Mike and distribution

Description:

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  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 .

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

公式變形:

2*X > X+Y    ===>    X>Y  (X爲選中下標在數組中對應的數之和,Y爲未選中下標在數組中對應的數之和)


解題思想:

本質上就是要找出n/2+1個數,並且對於這個選中集合裏面的數,在非選中的數中都能分別找到一個與之對應的小於它的數(所以這樣的對應爲兩個數爲一組,一個數來自選中集合,一個數來自非選中集合),這樣就一定能保證選出數大於剩下沒有選中的數,因爲a>b ------> a*2 > a+b = sum(a爲選中集合元素之和,b爲非選中集合元素之和), 滿足題目要求。例:1,4,9,6,5,2,選中9,4,5,非選中就是1,6,2,有9>6,4>1,5>2,所以這樣選出的數就滿足要求。(這裏能看出來每組case解法可能有多種)


解題步驟:

對其中任意一個數組排序,這裏就選A數組。原始的時候,A、B數組的元素是一一對應的,在對A數組排序的時候,對應的B數組的元素也要跟着A數組的元素一起移動。


這裏order是個結構體數組


1.排完序之後,先把order[0]加進選中集合。

2.然後對order數組兩個元素作爲一個劃分,即12,34, 56,7。

3.每一個劃分裏面選一個元素出來,選擇規則爲:選擇B值大的那個元素。如此,選中的order下標就是0,2,3,6,7。然後找到對應的原始下標就可以了。


解釋:

對於B數組,很顯然第3步就能保證滿足我們找到 a>b對 的思想,因爲在每個二元素劃分裏面選出大的那個數的時候,就已經有了一個對應的比它小的數。

對於A數組,我們找每一對a>b就不像B數組一樣在同一個劃分裏面找,而是跨劃分找,如圖所示。因爲A數組是排了序的,所以這樣跨劃分的比較是一定滿足a>b的。(√表示選中,_表示不選)。



兩個數組都滿足了,那麼解也就滿足了。




代碼:

#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;//注意n爲奇偶數的差異

      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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章