Codeforces1541 B. Pleasant Pairs(暴力,复杂度分析)

Posted live4m

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces1541 B. Pleasant Pairs(暴力,复杂度分析)相关的知识,希望对你有一定的参考价值。

题意:

在这里插入图片描述

解法:

将数从小到大排序,然后暴力枚举数对匹配即可,当a[i]*a[j]>4e5break.

因为数两两不同,下表和i+j<=4e5,
对于a[i]=1,最多匹配4e5/1,
对于a[i]=2,最多匹配4e5/2,
...
对于a[i]=k,最多匹配4e5/k次.

最坏复杂度也只有O(n*log).

code:

#include<bits/stdc++.h>
#define int long long
#define PI pair<int,int>
using namespace std;
const int maxm=2e5+5;
PI a[maxm];
int n;
void solve(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].first;
        a[i].second=i;
    }
    sort(a+1,a+1+n);
    int ans=0;
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            int x=a[i].first*a[j].first;
            if(x>4e5)break;
            int y=a[i].second+a[j].second;
            if(x==y)ans++;
        }
    }
    cout<<ans<<endl;
}
signed main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int T;cin>>T;while(T--)
    solve();
    return 0;
}

以上是关于Codeforces1541 B. Pleasant Pairs(暴力,复杂度分析)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces1541 D. Tree Array(期望,概率dp,lca)

位 (bit)字节 (Byte) 字符bpsBps的辨析

[codeforces 260]B. Ancient Prophesy

codeforces 655B B. Mischievous Mess Makers(贪心)

codeforces 653B B. Bear and Compressing(dfs)

Codeforces Round #352 (Div. 2) B. Different is Good