Codeforces1566D1

本文介绍了一种使用 multiset 对元素进行排序并计算特定条件下元素对数量的方法。通过迭代器遍历已排序集合,找出小于当前元素的个数,以此解决计数对问题。

问题描述

  • 这个题目要求有多少对i<ji < ji<j满足ai<aja_i < a_jai<aj

思路分析

  • 我们直接在读入的时候就用multiset对之前读入的数排序好,再用迭代器找到比当前读入数小的数的个数即可

代码如下

#include <bits/stdc++.h>
using namespace std;
const int maxn = 310;
int a[maxn];
multiset<int> s;
int main()
{
        int t;
        cin >> t;
        while (t--)
        {
                int n, m;
                cin >> n >> m;
                int ans = 0;
                s.clear();
                for (int i = 1; i <= m; i++)
                {
                        cin >> a[i];
                        int tp = 0;
                        for (auto it = s.begin(); it != s.end(); it++)
                        {
                                if (*it < a[i])
                                {
                                        tp++;
                                }
                                else
                                        break;
                        }
                        s.insert(a[i]);
                        ans += tp;
                }
                cout << ans << endl;
        }
        return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值