• Mike and distribution CodeForces


    题目链接

    TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料。

    题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1~n的不重复数字,

    要求这k个数字为下标的对应a和b中的数的和乘以2的值  分别大于a和b 的数组总和。

    思路:首先对a进行降序排序,然后输出最大值的下标,随后进行幅度为2的枚举,对排序后的a2~an进行选择性输出下标,(注意,排序的时候用一个新数组开两个变量,一个index,一个v进行排序,可以用结构体,也可以用pair)

    那么怎么进行选择性排序呢,由于要求的是这k的数的和*2大于总和,那么如果每两个数选取了一个大的,那么选出来的n/2+1个数的和*2一定大于原数组总和,至于为什么可以自行思考。

    时间复杂度为O ( n*log n  )  ,,  非常好的一个贪心题目,思想可以抽象为把n个数分为 n/2的小集合,从每一个小子集中选大的那个数,那么总和*2一定大于全集的和。

    附上代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb std::ios::sync_with_stdio(false)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define gg(x) getInt(&x)
    using namespace std;
    typedef long long ll;
    inline void getInt(int* p);
    const int maxn=1000010;
    const int inf=0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    int n;
    ll a[maxn];
    ll b[maxn];
    struct  node
    {
        int id;
        ll v;
    }c[maxn];
    bool cmp(node a,node b)
    {
        return a.v>b.v;
    }
    int main()
    {
        scanf("%d",&n);
        repd(i,1,n)
        {
            scanf("%lld",&a[i]);
            c[i].v=a[i];
            c[i].id=i;
        }
        repd(i,1,n)
        {
            scanf("%lld",&b[i]);
        }
        sort(c+1,c+1+n,cmp);
        printf("%d
    ",n/2+1);
        printf("%d ",c[1].id );
        for(int i=2;i<=n;i+=2)
        {
            if(i<n)
            {
                if(b[(c[i].id)]>b[c[i+1].id])
                {
                    printf("%d ", c[i].id);
                }else
                {
                    printf("%d ",c[i+1].id);
                }
            }else
            {
                printf("%d",c[i].id );
            }
        }
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }

    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    jsp 特殊标签
    poj 1753 Flip Game 高斯消元 异或方程组 求最值
    zoj 3155 Street Lamp 高斯消元 异或方程组 求方案数
    poj1222 EXTENDED LIGHTS OUT 高斯消元解异或方程组 模板
    zoj 3930 Dice Notation 模拟
    zoj 3157 Weapon 线段树求逆序对数
    hdu 1242 Rescue BFS+优先队列
    hdu 3466 Proud Merchants 贪心+01背包
    zoj 3689 Digging 贪心+01背包
    hdu 2602 Bone Collector 01背包模板
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10231202.html
Copyright © 2020-2023  润新知