题解 P4670 【[BalticOI 2011 Day2]Plagiarism】

· · 题解

洛谷传送门:https://www.luogu.com.cn/problem/P4670

LOJ 传送门:https://loj.ac/p/2635

博客传送门:https://oi-master.gitee.io/2020/12/22/BalticOI-2011-Day2-Plagiarism-题解/

这题绝对是道水题。跟我一样用 STL 的同学,请喊:

STL!STL!STL 强!

我们发现 n 只有 10^5 大,意味着我们可以使用 \operatorname{O}\left(n\log n\right) 的方法解决此题。

首先,我们先排序此数列。可以使用 STL 的 sort() 函数排序。

然后,我们发现对于每个数,需要对比的程序在排过序的数列中有单调性:当 a_i 不用比较时,那么 a_{i-1} 也不用比较。

那么可以算出每个数字 90\% 是多少,然后用 STL 的 lower_bound() 函数二分。

所以需要您手写的除了输入输出就没什么了。

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define int long long
signed main(){
    int n;
    cin>>n;
    int a[n+1];
    for(int i=1;i<=n;++i)
        cin>>a[i];
    sort(a+1,a+n+1);
    int sum=0;
    for(int i=1;i<=n;++i){
        int now=ceil(a[i]*90.0/100);
        int j=lower_bound(a+1,a+i,now)-a;
        sum+=i-j;
    }
    cout<<sum<<endl;
}