C. Number of Pairs

C. Number of Pairs
You are given an array a of n integers. Find the number of pairs (i,j) (1≤i<j≤n) where the sum of ai+aj is greater than or equal to l and less than or equal to r (that is, l≤ai+aj≤r).

For example, if n=3, a=[5,1,2], l=4 and r=7, then two pairs are suitable:

i=1 and j=2 (4≤5+1≤7);
i=1 and j=3 (4≤5+2≤7).
Input
The first line contains an integer t (1≤t≤104). Then t test cases follow.

The first line of each test case contains three integers n,l,r (1≤n≤2⋅105, 1≤l≤r≤109) — the length of the array and the limits on the sum in the pair.

The second line contains n integers a1,a2,…,an (1≤ai≤109).

It is guaranteed that the sum of n overall test cases does not exceed 2⋅105.

Output
For each test case, output a single integer — the number of index pairs (i,j) (i<j), such that l≤ai+aj≤r.

Example
inputCopy
4
3 4 7
5 1 2
5 5 8
5 1 2 4 3
4 100 1000
1 1 1 1
5 9 13
2 5 5 1 1
outputCopy
2
7
0
1
之前没用过lower_bounder和upper_bounder 做题的时候没有想到。二分又懒得写。。。。。。
所以直接掉大分

#include <iostream>
#include <algorithm>
using namespace std;
#define int long long
int ch[205001];
signed main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, l, r;
        cin >> n >> l >> r;
        for (int i = 0; i < n; i++)
        {
            cin >> ch[i];
        }
        sort(ch, ch + n);
        int sum = 0;
        for (int i = 0; i < n; i++)
        {
            int a = lower_bound(ch + i + 1, ch + n, l - ch[i]) - ch;
            int b = upper_bound(ch + i + 1, ch + n, r - ch[i]) - ch;
            int c = b - a;
            sum += c;
        }
        cout << sum << endl;
    }
}

哭了。。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个用C++实现的解题代码: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; // 检查一个数是否为质数 bool isPrime(int num) { if (num < 2) return false; for (int i = 2; i * i <= num; i++) { if (num % i == 0) return false; } return true; } // 找到最大可能的质数 vector<int> findMaxPrimality(int n) { vector<int> permutation(n); for (int i = 0; i < n; i++) { permutation[i] = i + 1; } // 如果n为1或2,则返回原始序列 if (n == 1 || n == 2) return permutation; // 如果n为偶数,将最后两个数交换位置 if (n % 2 == 0) { swap(permutation[n-1], permutation[n-2]); } // 将质数放在前面,非质数放在后面 for (int i = 2; i <= n; i++) { if (isPrime(i)) { swap(permutation[i-1], permutation[n-1]); break; } } return permutation; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> permutation = findMaxPrimality(n); for (int i = 0; i < n; i++) { cout << permutation[i] << " "; } cout << endl; } return 0; } ``` 解题思路: 1. 首先,我们考虑特殊情况,当n为1或2时,最大可能的质数就是原始序列。 2. 对于n为偶数的情况,我们可以将最后两个数交换位置,这样可以保证最后一个数不会是质数,因为偶数除2外都不是质数。 3. 对于n为奇数的情况,我们可以将最后一个质数放在序列的最后,其他非质数放在其他位置。我们可以通过遍历从2到n的数字,找到第一个质数,并将其与最后一个数字交换位置。 4. 最后输出得到的排列即为最大可能的质数排列之一。 希望这个解题思路对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值