zoukankan      html  css  js  c++  java
  • 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;
                }
            }
        }
    }


  • 相关阅读:
    Visifire正式版(v1.1)发布
    [转]PSP机能强大!已能模拟运行WINDOWS系统?
    在Silverlight+WCF中应用以角色为基础的安全模式(一)基础篇之角色为基础的安全模式简介 Virus
    C#的加密解密算法,包括Silverlight的MD5算法 Virus
    MMORPG programming in Silverlight Tutorial (10)Implement the sprite’s 2D animation (Part IV)
    Game Script: Rescue Bill Gates
    MMORPG programming in Silverlight Tutorial (9)KeyFrame Animation
    MMORPG programming in Silverlight Tutorial (5)Implement the sprite’s 2D animation (Part II)
    MMORPG programming in Silverlight Tutorial (7)Perfect animation
    MMORPG programming in Silverlight Tutorial (3)Animate the object (Part III)
  • 原文地址:https://www.cnblogs.com/james1207/p/3362332.html
Copyright © 2011-2022 走看看