zoukankan      html  css  js  c++  java
  • codeforces 798 D. Mike and distribution

    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] andB = [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 Pare 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
    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] andB = [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 Pare 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

    解题思路:

    这道题题意有点难描述,而且不难看懂就不多说了。

    这道题难点就在于有两个数组,难以判断。 所以我们这里先讨论只有一个数组时的情况,如何使取得的数字大于剩下的?其实只要两两之间相互比较,取最大的,当序列长度为奇数时,我们可以先提出

    其中一个,然后将剩余的两两比较,依旧必大于剩余的。

    单个数组的问题讨论完了,然后分析两个数组,这里我们可以将其中一个数组排序用另一个数组记录排序结果(下标)id[],对下一个数组进行排序,这么说有点抽象,我把具体实现情况写一下

    a[0] = 8 ,a[1] = 7,a[2] = 4, a[3] = 8,a[4] = 3;

    b[0] = 4,a[1] = 2,a[2] = 5, a[3] = 3,a[4] = 7;

    id[0] = 0,id[1] = 1,id[2] = 2,id[3] = 3,id[4] = 4;

    先将a数组排序从大到小并用id数组记录结果(下标)

    a数组排序结果 :  a0 a3 a1 a2 a4

    用id数组记录:id[0] = 0,id[1] = 3,id[2] = 1,id[3] = 2,id[4] = 4;

    这里可以用id对b数组进行排序,因为现在id储存的是a从大到小的顺序,按这个顺序排序a必大于剩余的,

    排序过程: 因为是奇数,先取出第一个 b[0] 然后取 max(b[3],b[1]),max(b[2],b[4]),这样就可以得出满足两个数组的下标序列

    实现代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int Max = 100000+3;
    int m,id[Max];
    long long a[Max],b[Max];
    inline bool cmp(const int &x,const int &y)
    {
        return a[x] > a[y];
    }
    int main()
    {
        int i;
        cin>>m;
        for(i=0;i<m;i++) cin>>a[i];
        for(i=0;i<m;i++) cin>>b[i];
        for(i=0;i<m;i++) id[i] = i;
        sort(id,id+m,cmp);
        int n = m/2 + 1;
        cout<<n<<endl;
        cout<<id[0]+1;
        for(i=1;i<m-1;i+=2){
            if(b[id[i]]>b[id[i+1]])
                cout<<" "<<id[i]+1;
            else
                cout<<" "<<id[i+1]+1;
        }
        if(m%2==0)
            cout<<" "<<id[m-1]+1;
        cout<<endl;
        return 0;
    }
  • 相关阅读:
    ubuntu14.04恢复系统默认中文字体
    Mcafee两个Mac版本之间的区别
    关于Windows系统防火墙
    诺顿序列号获取方法
    如何将软件限制策略发挥大作用
    BootCamp支持软件4/5
    Android系统分区理解及分区目录细解
    给Adobe Reader添加书签功能
    PHP面向对象之旅:static变量与方法
    INSERT IGNORE 与 INSERT INTO的区别
  • 原文地址:https://www.cnblogs.com/kls123/p/6834374.html
Copyright © 2011-2022 走看看