【TYVJ】1463

http://tyvj.cn/Problem_Show.aspx?id=1463

二分的话是水题啊。。

为了学分块还是来写这题吧。。

二分:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=1000005;
int n, a[N], cnt;
int main() {
	read(n);
	for1(i, 1, n) read(a[i]);
	sort(a+1, a+1+n);
	int x;
	while(~scanf("%d", &x)) printf("%d
", (int)(lower_bound(a+1, a+1+n, x)-a) );
	return 0;
}

分块:分块就是将有序的数组分成一个一个的大小为sqrt(n)的块,然后验证每个块最后的一个元素(注意处理好边界就行了)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=1000005;
int n, a[N], siz, lst[N];
int ifind(const int &x) {
	if(x<a[1]) return 1;
	for1(i, 1, siz+1) if(lst[i]>=x) {
		int ret=(i-1)*siz, j=0;
		while(a[ret+j]<x) ++j;
		return ret+j;
	}
	return n+1;
}
int main() {
	read(n);
	for1(i, 1, n) read(a[i]);
	sort(a+1, a+1+n);
	siz=sqrt(n);
	for1(i, 1, siz) lst[i]=a[i*siz];
	lst[siz+1]=a[n];
	int x;
	while(~scanf("%d", &x)) printf("%d
", ifind(x));
	return 0;
}

背景 Background

各种数据结构帝~
各种小姊妹帝~
各种一遍AC帝~ 来吧!

描述 Description

某个同学又有很多小姊妹了
他喜欢聪明的小姊妹 所以经常用神奇的函数来估算小姊妹的智商
他得出了自己所有小姊妹的智商
小姊妹的智商都是非负整数
但是这个同学看到别的同学的小姊妹
也喜欢用神奇的函数估算一下
然后看看这个小姊妹在自己的小姊妹群体中排在第几位...
(这么邪恶的兴趣...)

输入格式 InputFormat

第一行一个整数N 代表小姊妹的个数
第二行N个整数 代表这位同学N个小姊妹的智商
接下来若干行 每行一个整数
代表这位同学看中的别人的小姊妹的智商
0<=智商<=2^31-1
0<=N<=1000000

输出格式 OutputFormat

输出若干行
每行一个整数 回答新的小姊妹
在原来小姊妹中智商的排名

样例输入 SampleInput [复制数据]

5
1 2 3 4 5
1
2
3
4
5

样例输出 SampleOutput [复制数据]

1
2
3
4
5

数据范围和注释 Hint

数据量很大
C语言用scanf输入输出!
另外 这个同学的小姊妹群体在这个题中是不会变的~
他会在看完所有别的同学的小姊妹之后...
大家听说过 苏格拉底和麦田的故事的吧...

来源 Source

Bob HAN

原文地址:https://www.cnblogs.com/iwtwiioi/p/3945043.html