在C ++中对数组进行排序并保存旧索引 [英] Sorting array and saving the old index in C++

查看:55
本文介绍了在C ++中对数组进行排序并保存旧索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行在线挑战,必须执行以下操作.

I am doing online challenge which has to do the following.

在一个村庄里有一场比赛.因此,在第一行中输入两个数字N(即参加比赛的人数)和K(其中有多少人可以进入阶段2).

There is a contest going in a village. So in the first line input two numbers N (which is how many people will join the contest) and K (how many of them can go to stage 2).

在那之后,我们在两个阶段中为每个候选人输入N倍的选票.

After that, we input N times the votes for each candidate in both stages.

示例输入:

5 3
9 2
3 10
5 6
8 4
6 5

如您所见,我们输入 N = 5 K = 3 ,这意味着有5个候选者,因此还有5行和其中3行进入了阶段2

As you can see, we input N=5, K=3, which means 5 candidates, so 5 additional lines and 3 of them which go to stage 2.

对数组进行排序后,得票最多的候选人是6、8和9.因此,他们将进入第2阶段.获胜者是其中第2阶段得票最多的候选人.在这种情况下,6票具有5票,这是最高的(8票具有4票,9票具有2票),因此我们输出6票的索引为5.

After we sort the array the candidates with most votes are the ones with 6, 8 and 9. So they're going to stage 2. The winner is the one who has most votes in the stage 2 of them. In this case, 6 has 5 votes which is the most (8 has 4 and 9 has 2) and therefore we output the index of the 6 which is 5.

我到目前为止所得到的:

What I got so far:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int arr[50],nizabackup[50],n,k,niza2[50],saveindex[50],indexp=0;
    cin >> n >> k;
    for(int i=0;i<n;i++)
    {
        cin >> arr[i] >> niza2[i];
        nizabackup[i] = arr[i];
    }
    sort(arr,arr+n);
    for(int j=n;j>=k;j--)
    {
        for(int k=0;k<n;k++)
        {
            if(arr[j]==nizabackup[k])
            {
                saveindex[0]=k;
                indexp++;
            }
        }
    }
    sort(saveindex,saveindex+indexp);
    cout << saveindex[indexp];
    return 0;
}

我需要一个提示该怎么做以及其他问题-为什么我的调试器不读取第二个for循环?

I need a hint what to do and also additional question -- why my debugger doesn't read the second for loop?

推荐答案

好的,替代实现.还有更多设置,但请先阅读 main ,然后看看实际逻辑要简单得多.

OK, alternative implementation. There's more set-up, but read main first and see how much simpler the actual logic is.

#include <vector>
#include <iostream>
#include <algorithm>

struct Contestant {
    int index;
    int firstVote;
    int secondVote;
    Contestant(int i, int v1, int v2) : index(i), firstVote(v1), secondVote(v2)
    {}
};
// return true if l should come before r in sorted result
bool byFirstVote(Contestant const &l, Contestant const &r) {
    return l.firstVote > r.firstVote; // sort by first vote
}
bool bySecondVote(Contestant const &l, Contestant const &r) {
    return l.secondVote > r.secondVote; // sort by second vote
}

int main()
{
    int n, k;
    std::cin >> n >> k;
    // populate a single vector of {index, firstVote, secondVote}
    std::vector<Contestant> contestants;
    for(int i=0; i<n; i++) {
        int v1, v2;
        std::cin >> v1 >> v2;
        contestants.push_back(Contestant(i+1, v1, v2));
    }
    // sort all by firstVote, then sort first k by secondVote
    std::sort(contestants.begin(), contestants.end(), byFirstVote);
    std::sort(contestants.begin(), contestants.begin()+k, bySecondVote);
    std::cout << contestants.front().index << std::endl;
}

我正在将索引(按照您的示例,从1开始,不为零)和两个投票一起存储在一个结构中.

I'm storing the index (starting from 1, not zero, as per your example), and both votes, all together in a single structure.

然后,我只是更改要对哪个字段进行排序.

Then, I just change which field I sort on.

这篇关于在C ++中对数组进行排序并保存旧索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆