Codeforces 798D 構造

Mike and distribution
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 integers A = [a1, a2, ..., an] and B = [b1, b2, ..., bn] of length neach 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) isgreater 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 P should be distinct.

Example
input
5
8 7 4 8 3
4 2 5 3 7
output
3
1 4 5

題意:給你兩個序列  從中選出n/2+1個相同下標的數  使得兩個序列的和  大於各自序列和的一半  題目保證有解


題解:先按A從大到小排序

如果n是奇數  就先把第一個選進去  然後兩個兩個的for  對於相鄰的兩個  選取B大的那個

然後就能保證a的和大於序列和的一半  因爲a1>max(a2,a3)  min(a2,a3)>max(a4,a5)...

b選的也是大的  所以也能保證


如果n是偶數  先這樣做一次  然後把1或者2再扔進去即可


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
struct node{
	int a,b,lab;
	bool operator <(const node& c)const{
		if(a==c.a)return b>c.b;
		return a>c.a;
	}
}e[100005];
vector<int>sp;
int main(){
    int n,i,j;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
    	scanf("%d",&e[i].a);
    	e[i].lab=i;
    }
    for(i=1;i<=n;i++)scanf("%d",&e[i].b);
    sort(e+1,e+1+n);
    if(n%2){
    	sp.push_back(e[1].lab);
    	i=2;
    }
    else i=1;
	for(;i<=n;i+=2){
    	if(e[i].b<e[i+1].b)sp.push_back(e[i+1].lab);
    	else sp.push_back(e[i].lab);
    }
    if(n%2==0){
    	if(sp[0]!=e[1].lab)sp.push_back(e[1].lab);
    	else sp.push_back(e[2].lab);
    }
    printf("%d\n",sp.size());
    for(i=0;i<sp.size();i++)printf("%d ",sp[i]);
    printf("\n");
    return 0;
}


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