• poj2454


    我不会告诉你我对我自己的想法笑了一下午的。。

    #include <iostream>
    #include <algorithm>
    #include <time.h>
    #include <string.h>

    using namespace std;

    typedef struct select
    {
        int id;
        int num;
    }sel;

    bool comp(sel a, sel b)
    {
        return a.num < b.num;
    }

    int main()
    {
        int k;
        while (cin >> k)
        {
            sel cow[200];
            for (int i = 0; i < k * 3; i++)
            {
                cin >> cow[i].num;
                cow[i].id = i + 1;
            }
            sort(cow, cow + k * 3, comp);
            srand((unsigned) time(NULL));
            while (1)
            {
                int i = rand() % (k) + k;
                int j = rand() % (k) + 2 * k;
                //cout << i << ' ' << j << endl;
                swap(cow[i], cow[j]);
                int temp1 = 0;
                int temp2 = 0;
                for (int a = k; a < 2 * k; a++) 
                {
                    temp1 += cow[a].num;
                }
                for (int a = 2 * k; a < 3 * k; a++)
                {
                    temp2 += cow[a].num;
                }
                if ((temp1 > 500 * k) && (temp2 > 500 * k))
                {
                    for (int a = 0; a < k * 3; a++)
                    {
                        cout << cow[a].id << endl;
                    }
                    break;
                }
            }
        }
    }

    貌似升序更快

    #include <iostream>
    #include <algorithm>
    #include <time.h>
    #include <string.h>

    using namespace std;

    typedef struct select
    {
        int id;
        int num;
    }sel;

    bool comp(sel a, sel b)
    {
        return a.num > b.num;
    }

    int main()
    {
        int k;
        while (cin >> k)
        {
            sel cow[200];
            for (int i = 0; i < k * 3; i++)
            {
                cin >> cow[i].num;
                cow[i].id = i + 1;
            }
            sort(cow, cow + k * 3, comp);
            srand((unsigned) time(NULL));
            while (1)
            {
                int i = rand() % (k);
                int j = rand() % (k) + k;
                //cout << i << ' ' << j << endl;
                swap(cow[i], cow[j]);
                int temp1 = 0;
                int temp2 = 0;
                for (int a = 0; a < k; a++)
                {
                    temp1 += cow[a].num;
                }
                for (int a = k; a < 2 * k; a++)
                {
                    temp2 += cow[a].num;
                }
                if ((temp1 > 500 * k) && (temp2 > 500 * k))
                {
                    for (int a = 0; a < k * 3; a++)
                    {
                        cout << cow[a].id << endl;
                    }
                    break;
                }
            }
        }
    }


  • 相关阅读:
    20190425-快速排序
    ExtJS合同管理信息系统源码
    2013年12月12日16Aspx源码发布详细
    大型商业手机进销存管理系统源码
    大管家固定资产管理系统源码
    地平线开源网站源码Deepsoon v1.2.3
    发票管理系统(Extjs)源码
    国讯通用OA协同办公系统源码
    物流管理系统源码
    2013年10月16Aspx源码发布详情
  • 原文地址:https://www.cnblogs.com/james1207/p/3362332.html
Copyright © 2020-2023  润新知