C++ Vector的排序实践

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/twentyonepilots/article/details/82807620

Argus

A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, financial tickers, on-line auctions, and transaction logs such as Web usage logs and telephone call records. Likewise, queries over streams run continuously over a period of time and incrementally return new results as new data arrives. For example, a temperature detection system of a factory warehouse may run queries like the following.
Query-1: “Every five minutes, retrieve the maximum temperature over the past five minutes.”
Query-2: “Return the average temperature measured on each floor over the past 10 minutes.”
We have developed a Data Stream Management System called Argus, which processes the queries over the data streams. Users can register queries to the Argus. Argus will keep the queries running over the changing data and return the results to the corresponding user with the desired frequency.
For the Argus, we use the following instruction to register a query:
Register Q_num Period
Q_num (0 < Q_num <= 3000) is query ID-number, and Period (0 < Period <= 3000) is the interval between two consecutive returns of the result. After Period seconds of register, the result will be returned for the first time, and after that, the result will be returned every Period seconds.
Here we have several different queries registered in Argus at once. It is confirmed that all the queries have different Q_num. Your task is to tell the first K queries to return the results. If two or more queries are to return the results at the same time, they will return the results one by one in the ascending order of Q_num.

Input

The first part of the input are the register instructions to Argus, one instruction per line. You can assume the number of the instructions will not exceed 1000, and all these instructions are executed at the same time. This part is ended with a line of “#”.
The second part is your task. This part contains only one line, which is one positive integer K (<= 10000).

Output

You should output the Q_num of the first K queries to return the results, one number per line.

Sample Input

Register 2004 200
Register 2005 300
#
5

Sample Output

2004
2005
2004
2004
2005

本题的关键在于对vector数据类型排序,并且vector包含三个参数,根据其中一个参数排序,其他参数也跟着走但不变。一开始尝试了使用三个并列的vector,程序如下:
(注意自定义的sort函数参数必须是vector的地址,否则形参排序之后无法传递给实参,也就是外面的vector根本没有排序,和数组不一样(是不是因为数组名本身就是地址?待讨论),参见C++(笔记)容器(vector)作为函数参数如何传参

(该程序暂未考虑题目中的If two or more queries are to return the results at the same time的情况)

#include <iostream>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

void sort(vector<int>a, vector<int>b,vector<int>c,int left, int right)
{
    cout<<left<<endl;
    cout<<right<<endl;
    if(left >= right)
    {
        return ;
    }
    int i = left;
    int j = right;
    int key = a[left];
    int k=b[left];
    int kc=c[left];

    while(i < j)
    {
        while(i < j && key <= a[j])
        {
            j--;
        }
        a[i] = a[j];
        b[i]=b[j];
         c[i]=c[j];
        while(i < j && key >= a[i])
        {
            i++;
        }

        a[j] = a[i];
        b[j]=b[i];
         c[j]=c[i];
    }

    a[i] = key;
    b[i]=k;
    c[i]=kc;
    sort(a, b,c,left, j - 1);
    sort(a,b, c,i + 1, right);
}

int main()
{
    char str[19];
    vector<int>q;
    vector<int>p;
    int m,n;
    for(int i=0;gets(str)&&str[0] != '#';i++){
        sscanf(str,"Register %d %d",&m,&n);
        q.push_back(m);
        p.push_back(n);
    }
    vector<int>p_c(p);
    int j;
    cin>>j;
    for(;j>0;j--){
        sort(p,q,p_c,0,q.size());
        cout<<q[0]<<endl;
        p[0]+=p_c[0];
        for(int k=0;k<q.size();k++){
            cout<<p[k]<<endl;
        }
    }


    return 0;
}

提交,结果是超时。看来sort函数和三个vector的组合比较复杂。这时了解到vector本身就自带std::sort()排序函数,并且可以传入参数使得vector结构体按照某一元素排序,参考C++中,结构体vector使用sort排序
对于If two or more queries are to return the results at the same time的情况,本来想根据另一个参数对相同时间的元素进行二次排序,但是需要定义迭代器,太复杂。遂直接寻找相同时间的元素的编号最小的元素,输出。
最终代码如下:

#include <iostream>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include <algorithm>

using namespace std;

struct Reg{
    int q;
    int p;
    int q_c;
};

bool SortByQ (Reg a,Reg b) { return (a.q<b.q); }

int main()
{
    char str[19];
    vector<Reg>vt;
    int m,n;
    for(int i=0;gets(str)&&str[0] != '#';i++){
       sscanf(str,"Register %d %d",&m,&n);
        vt.push_back({n,m,n});
    }
    int j;
    cin>>j;
    for(;j>0;j--){
        sort(vt.begin(),vt.end(),SortByQ);

        int mi=vt[0].p;
        int mini=0;
        for(int i=1;vt[i].q==vt[0].q;i++){
                if(vt[i].p<mi){
                    mi=vt[i].p;
                    mini=i;
                }
            }
        cout<<mi<<endl;
        vt[mini].q+=vt[mini].q_c;

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/twentyonepilots/article/details/82807620