Codeforces 798D Mike and distribution(二维贪心)

版权声明:转载请注明出处:http://blog.csdn.net/yasola,谢谢 https://blog.csdn.net/Yasola/article/details/70477816

D. 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) 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 P should be distinct.

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

题目大意:

    给你两个数组长度为n的数组a,b,让你从中选择n/2+1个下标,使得对于每个数组,选择的元素之和的二倍大于整个数组的元素之和。


解题思路:

    首先,对于题目要求的选择元素之和两倍大与所有元素之和,我们可以转化为选择元素之和大于剩下的。然后我们可以将下标按照a从大到小排序。然后选择第一个,之后每两个一组,选择b大的一个,如果n是偶数再选择最后一个。

    至于这样写的正确性:首先对于数组b,每一组选择的都是大的,而且还有多选的,所以一定比剩下的大。对于数组a,从第一个开始看,当前选择的,一定比下一组剩下的a大。所以这样贪心就一定正确。

    总结一下就是,对于二维的贪心我们可以先让它变成其中一维有序,这样只需要重点考虑另一维,就会简单很多。


AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <set>
#include <ctime>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))

const int MAXN=100000+3;
int N,id[MAXN];
LL a[MAXN],b[MAXN];

inline bool cmp(const int &i1,const int &i2)
{
    return a[i1]>a[i2];
}

int main()
{
    scanf("%d",&N);
    for(int i=0;i<N;++i)
        scanf("%lld",&a[i]);
    for(int i=0;i<N;++i)
        scanf("%lld",&b[i]);
    for(int i=0;i<N;++i)
        id[i]=i;
    sort(id,id+N,cmp);
    int n=N/2+1;
    printf("%d\n",n);
    printf("%d",id[0]+1);
    for(int i=1;i+1<N;i+=2)
    {
        if(b[id[i]]>b[id[i+1]])
            printf(" %d",id[i]+1);
        else printf(" %d",id[i+1]+1);
    }
    if(!(N&1))
        printf(" %d",id[N-1]+1);
    putchar('\n');
    
    return 0;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页