2019杭州师范大学校赛问题 H: Little Sub and Counting

问题 H: Little Sub and Counting
时间限制: 1 Sec 内存限制: 256 MB
提交: 351 解决: 100
[提交] [状态] [命题人:admin]
题目描述
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 AiAj > AjAi .

输入
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

思路;推断一下,从Ai=2开始,Aj=3,4,5,6…,然后Ai=3,Aj=4,5,6…,多推断几次就会发现,除2以外(2从5开始),其他数从比他自身大1的数开始,就一直满足AiAj >AjAi,可以根据y=ax,的函数性质推断一下,(a+1)b,和a(b+1),他们的增长速度是相差特别大的,特别是当b很大的时候,还需要注意的是1不满足这个规律,还需要特判3,因为23 < 32,

#include<iostream>
#include<cstring>
#include<string.h>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    int ddd = 0;
    scanf("%d", &n);
    int a[100005], b[100005];
    int flag = 0, flag2 = 0;
    for (int i = 0; i < n; i++)
    {
        int t;
        scanf("%d", &t); a[i] = t; b[i] = t;
        if (t == 1)flag++;
        if (t == 2)flag2++;
    }
    sort(b, b + n);
    for (int i = 0; i < n; i++)
    {
        if (a[i] == 1)
        {
            if (ddd == 0)
            {
                ddd++;
                printf("0");
            }
            else
            printf(" 0");
        }
        else{
            int t;
            if (a[i] == 2){
                t = upper_bound(b + 0, b + n, 4) - b;
            }
            else{
                t = upper_bound(b + 0, b + n, a[i]) - b;
            }
            if (a[i] == 3){
                if (ddd == 0)
                {
                    cout << n-t+flag+flag2;
                    ddd++;
                }
                else
                    cout << " " << n - t + flag + flag2;
            }
            else
            {
                if (ddd == 0)
                {
                    cout << n - t + flag;
                    ddd++;
                }
                else
                    cout << " " << n - t + flag;
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值