UPC Contest 1748 Little Sub and Counting

题目描述

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;
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值