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;
                }
            }
        }
    }


  • 相关阅读:
    C#事件和委托的区别
    已知有个rand7()的函数,返回1到7随机自然数,让利用这个rand7()构造rand10()随机1~10
    如何搭建github+hexo博客-转
    ngRouter和ui-router区别
    JS数组追加数组采用push.apply的坑(转)
    vue中关于computed的一点理解
    simplify the life ECMAScript 5(ES5)中bind方法简介
    github使用-知乎的某小姐的一篇文章
    Jade 模板引擎使用
    玩转Nodejs日志管理log4js(转)
  • 原文地址:https://www.cnblogs.com/james1207/p/3362332.html
Copyright © 2011-2022 走看看