C++ pair用法

刷leecode有这么一道题:

给定两个大小相等的数组 A 和 B,A 相对于 B 的优势可以用满足 A[i] > B[i] 的索引 i 的数目来描述。

返回 A 的任意排列,使其相对于 B 的优势最大化。

我第一次想的是枚举法,然后超时了。

class Solution {
public:
    vector<int> advantageCount(vector<int>& A, vector<int>& B) {
        int max = 0;
        vector<int> maxVector;
        maxVector.insert(maxVector.begin(), A.begin(), A.end());
        sort(A.begin(), A.end());
        do
        {
            int count = 0;
            for(int i = 0; i < A.size(); i ++)
            {
                if(A[i] > B[i] ) count++;
            }
            if(count > max)
            {
                max = count;
                maxVector.clear();
                maxVector.insert(maxVector.begin(), A.begin(), A.end());            
            }
        } while(next_permutation(A.begin(), A.end()));
        return maxVector;
    }
};

 后来想了想。换了一种方法做。通过用例了。

1.首先两个数组进行排序,如果数组A[i] > B[j];,用pair存储B数组未排序的索引 可以认为B[j]之前的索引数组都是小于等于A[i].

2. 则对数组A遍历,找到满足A[i]>B[j]的值,取代结果数组B中原先索引值。

代码如下:

class Solution {
public:
    vector<int> advantageCount(vector<int>& A, vector<int>& B) {
        vector<pair<int, int> > b_matrix;
        vector<int> result;  //存储结果索引值
        result.resize(B.size());
        int flag = -1;
        for(int i = 0; i < B.size(); i++)
        {
            b_matrix.push_back(make_pair(B[i],i));
        }
        sort(A.begin(), A.end());
        sort(b_matrix.begin(), b_matrix.end());
        int j = A.size()-1;
        int k = A.size()-1;
        int i = 0;
        while(j>=0)
        {
            if(A[k] > b_matrix[j].first) {
                result[b_matrix[j].second] = A[k];
                k--;
                j--;
            } else {
                result[b_matrix[j].second] = A[i];
                i++;
                j--;
            }
            
        }
         
        return result;
    }
};
/*********example ******

A[k]:          1 2  2 4 6
b_matrix[j]:0 2 2 4 5
6取代5原先的索引,  
4取代2原先的索引,
2取代0原来的索引值。
其他位置则随意
****************/

 

转载于:https://www.cnblogs.com/Shinered/p/9313954.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PairC++ STL中的一个模板类,用于将两个值(可以是不同类型)组合成一个单元。Pair 类型的对象可以在需要一组有序数据时使用,例如将两个数据项存储在一个数据结构中。 Pair 通常用于以下场景: 1. 返回两个值的函数 2. 处理两个值的函数 3. 存储两个值的数据结构 4. 对于需要将键值对映射到一个值的场景 Pair 的语法如下: ``` template <class T1, class T2> struct pair; ``` 其中,T1 和 T2 是要组合成 Pair 类型的两个值的类型。使用 Pair 类型的对象需要引入头文件 <utility>。 例如,以下代码演示了 Pair 的基本用法: ```c++ #include <iostream> #include <utility> using namespace std; int main() { pair<int, string> p1(1, "one"); pair<int, string> p2 = make_pair(2, "two"); cout << "p1: " << p1.first << " " << p1.second << endl; cout << "p2: " << p2.first << " " << p2.second << endl; p1.first = 3; p1.second = "three"; cout << "p1 after update: " << p1.first << " " << p1.second << endl; return 0; } ``` 输出结果为: ``` p1: 1 one p2: 2 two p1 after update: 3 three ``` 在这个例子中,我们创建了两个 Pair 类型的对象 p1 和 p2,它们分别包含一个整数和一个字符串。我们还演示了如何使用 make_pair 函数来创建一个 Pair 类型的对象,make_pair 函数会自动推导出两个值的类型。我们还修改了 p1 对象的值,并输出了更新后的值。 总之,Pair 类型提供了一种方便的方法,可以将两个值组合成一个单元,可以方便地处理和存储这些值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值