题目描述
Little Sub likes Math. Now he has a simple counting problem for you.
Given an positive integer sequence A, for each Ai , Please calculate how many elements Aj in the sequence satisfi ed
输入
The first line contains one positive integer n(1 ≤ n≤100000).
The following line contains n positive integers Ai(1≤Ai≤231-1).
输出
Please output n integer in one line and separate them by a single space. The ith integer indicates the answer concerning Ai .
样例输入
复制样例数据
3 1 2 5
样例输出
0 2 1
思路:
1、 令x=ai,y=aj,所以x^y>y^x
2、两边同时取对数,y*(lnx)>x*(lny);
3、将相同变量转化到一边,(lnx)/x>(lny)/y;
所以可以求出函数f(x)=(lnx)/x的单调性。即在(0,e]上单调增,(e,+无穷)单调减;
你把图画出来会发现,f(1)=0,f(2)=f(4)=(ln2)/2,f(3)=(ln3)/3,算不出来比不了大小的话,直接带入到第一步中,就可以知道他的大小了。
所以排一遍序,找到每个比f(ai)要小的值。
AC代码
#include<iostream>
#include<algorithm>
using namespace std;
int a[110000],b[110000];
int main()
{
int n,num1=0,num2=0,num3=0,num4=0,num=0;
//num记录比4大的数,num1记录数组中等于1的数的个数,num2,num3,num4同理。
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
if(a[i]==1)
num1++;
else if(a[i]==2)
num2++;
else if(a[i]==3)
num3++;
else if(a[i]==4)
num4++;
else
num++;
}
sort(b,b+n);
for(int i=0;i<n;i++)
{
if(a[i]==1)
cout<<0;
else if(a[i]==2)
cout<<num1+num;
else if(a[i]==3)
cout<<num1+num+num2+num4;
else if(a[i]==4)
cout<<num1+num;
else
{
cout<<num1+n-(upper_bound(b,b+n,a[i])-b);
}
if(i!=n-1)
cout<<" ";
else
cout<<endl;
}
return 0;
}