Cattle off network - Exercises

Time limit: 2 seconds; space constraints: 65536k

Title Description
In order to find satisfying jobs, beef collect the difficulty and rewards of each job. Standard Taurus selected to work in the case of difficulty does not exceed the value of their own capacity, Taurus choose the highest paying jobs. After their work is selected beef, beef beef little friends came to help work the election, beef still use their own criteria to help small partners. Taurus too much of a small partner, so he had to put this task to you.

Input Description:
Each input comprises a test.
The first line of each test case contains two positive integers, respectively, the number of working N (N <= 100000) and small partner M (M <= 100000).
The following N lines contains two positive integers, respectively, of the difficulty of the work Di (Di <= 1000000000) and compensation Pi (Pi <= 1000000000).
The next line contains M a positive integer, the ability to represent the value of M small partner Ai (Ai <= 1000000000).
There is no guarantee the two tasks of the same reward.

Output Description:
For each small partner, in a separate line output a positive integer representing the highest reward he can get. A job can be selected more than one person.

Example 1
Input
. 3. 3
1 100
10 1000
1000000000 1001
9101000000000

Output
100
1000
1001

1. Thoughts

  • 2s time limit is really discouraging to me ...... ...... almost not a word search using violent means, otherwise every minute timeout warning, complexity is O (MN);
  • The next track of thinking:
    (1) defines two classes, Work and Friends, are used to store information work and small partners, which Work diff class and money are used to store and difficulty of the work with respect to remuneration , Friends class ai, mo, and the ability of small partners are used to index storage, and the order of the maximum compensation input can be obtained; and
    (2) the ability of small and difficult to work partners ascending sort order, complexities are O (MlogM) and O (NlogN);
    (. 3) since the compensation is not necessarily proportional to the difficulty of the work and the work, i.e., a small but it is possible to work more difficult paid work, so the calculation corresponding to the difficulty of the work can be achieved the highest remuneration;
    (4) again from the lowest partners the ability to start small and then work a little small partner after partner has made previous start looking for higher returns within its ability, complexity is O (M + N);
    (5) Finally, the small partner sorted from the input, and then outputs each corresponding to the maximum compensation can be obtained.

2. To achieve
running time: 523 ms
take up memory: 4204K

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Work{
public:
    int diff;
    int money;
};

class Friends{
public:
    int ai;
    int mo;
    int index;
};

static bool cmp(Work &a, Work &b){
    return (a.diff < b.diff);
}

static bool cmpa(Friends &a, Friends &b){
    return (a.ai < b.ai);
}

static bool cmpi(Friends &a, Friends &b){
    return (a.index < b.index);
}

int main(){
    int M, N;

    while (cin >> N >> M){
        vector<Work> w;
        for (int i = 0; i < N; i++){
            Work c;
            cin >> c.diff >> c.money;
            w.push_back(c);
        }
        vector<Friends> frd;
        for (int i = 0; i < M; i++){
            Friends c;
            cin >> c.ai;
            c.index = i;
            frd.push_back(c);
        }

        //小伙伴按照能力要求从大到小排序
        sort(frd.begin(), frd.end(), cmpa);
        //sort(frd.begin(), frd.end(), [&](Friends &a, Friends &b){return (a.ai < b.ai); });
        
        //工作按照能力要求从小到大排序
        sort(w.begin(), w.end(), cmp);
        //sort(w.begin(), w.end(), [&](Work &a, Work &b){return (a.diff < b.diff); });

        //由于工作的报酬和要求不一定成正比
        //因此高要求能达到的报酬是比它低要求中所有报酬的最大值
        int mx = 0;
        for (int i = 0; i < N; i++){
            mx = max(mx, w[i].money);
            w[i].money = mx;
        }

        //从能力最低的小伙伴开始,之后的小伙伴在前一位小伙伴取得的工作之后开始找能力范围内报酬更高的
        int m, j, k = 0, t;
        for (int i = 0; i < M; i++){
            m = 0;
            t = 0;
            j = k;
            while (j <= N){
                if (j == N || frd[i].ai < w[j].diff){
                    t = j;//第j个工作是第一个超过第i位小伙伴能力的工作
                    if (j>0)
                        m = w[j - 1].money;//将第j-1个工作给第i个小伙伴
                    break;
                }
                else{
                    j++;
                }
            }
            k = t;
            frd[i].mo = m;
        }

        //小伙伴按照输入顺序从大到小排序
        sort(frd.begin(), frd.end(), cmpi);
        //sort(frd.begin(), frd.end(), [&](Friends &a, Friends &b){return (a.index < b.index); });

        for (int i = 0; i < M; i++){
            cout << frd[i].mo << endl;
        }
    }

    return 0;
}

Guess you like

Origin www.cnblogs.com/xuyy-isee/p/11329617.html