【题目】
Little Sub and Counting
Time Limit: 1 s Memory Limit: 256 MB
Submission:1587 AC:150 Score:100.00
Description
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 satisfied Ai^Aj > Aj^Ai.
Input
The first line contains one positive integer n(1 ≤ n ≤ 100000).
The following line contains n positive integers Ai(1 ≤ Ai ≤ 2^31 − 1).
Output
Please output n integer in one line and separate them by a single space. The ith integer indicates the answer concerning Ai.
Author
【题解】
我们可以发现,1没有满足条件的可能,1对任意其他数都满足;2只有在aj大于4的时候满足,2只对aj=3满足;2和大于3的数对3满足;大于n的数对n满足。所以我们只需要排序二分计算符合要求的个数即可。
【代码】
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;
int main()
{
ll a[100005],b[100005];
int c[5]={0};
int n; scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%lld",&a[i]);
b[i]=a[i];
if(a[i]==1) c[1]++;
else if(a[i]==2) c[2]++;
}
sort(b,b+n);
for(int i=0;i<n;i++){
if(a[i]==1) printf("0");
else if(a[i]==2) printf("%d",c[1]+n-(lower_bound(b,b+n,5)-b));
else if(a[i]==3) printf("%d",c[1]+c[2]+n-(lower_bound(b,b+n,a[i]+1)-b));
else printf("%d",c[1]+n-(lower_bound(b,b+n,a[i]+1)-b));
printf(i==n-1?" ":" ");
}
return 0;
}