P1463 智商问题
时间: 1500ms / 空间: 131072KiB / Java类名: Main
背景
各种数据结构帝~
各种小姊妹帝~
各种一遍AC帝~ 来吧!
描述
某个同学又有很多小姊妹了
他喜欢聪明的小姊妹 所以经常用神奇的函数来估算小姊妹的智商
他得出了自己所有小姊妹的智商
小姊妹的智商都是非负整数
但是这个同学看到别的同学的小姊妹
也喜欢用神奇的函数估算一下
然后看看这个小姊妹在自己的小姊妹群体中排在第几位...
(这么邪恶的兴趣...)
输入格式
第一行一个整数N 代表小姊妹的个数
第二行N个整数 代表这位同学N个小姊妹的智商
接下来若干行 每行一个整数
代表这位同学看中的别人的小姊妹的智商
0<=智商<=2^31-1
0<=N<=1000000
输出格式
输出若干行
每行一个整数 回答新的小姊妹
在原来小姊妹中智商的排名
测试样例1
输入
5
1 2 3 4 5
1
2
3
4
5
输出
1
2
3
4
5
备注
数据量很大
C语言用scanf输入输出!
另外 这个同学的小姊妹群体在这个题中是不会变的~
他会在看完所有别的同学的小姊妹之后...
大家听说过 苏格拉底和麦田的故事的吧...
Bob HAN
如果分数相同 排在相同的分数第一位
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
typedef long long LL;
int last[N], k, a[N];
int solve(int x)
{
for(int i=1;i<=k+1;i++)
{
if(x>last[i-1]&&x<=last[i])
{
int pos=(i-1)*k;
for(int j=1;j<=k;j++)
{
if(x<=a[pos+j]) return pos+j;
}
}
}
}
int main()
{
int n, x;
scanf("%d", &n);
for(int i=1;i<=n;i++) scanf("%d", &a[i]);
sort(a+1,a+n+1);
k=(int)sqrt(n*1.0);
for(int i=1;i<=k;i++) last[i]=a[i*k];
last[k+1]=a[n],last[0]=-1;
while(scanf("%d", &x)!=EOF)
{
if(x<a[1]) cout<<1<<endl;
else if(x>a[n]) cout<<(n+1)<<endl;
else printf("%d\n",solve(x));
}
return 0;
}