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

二分的话是水题啊。。

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

二分:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <string>
  5. #include <iostream>
  6. #include <algorithm>
  7. using namespace std;
  8. #define rep(i, n) for(int i=0; i<(n); ++i)
  9. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  10. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  11. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  12. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  13. #define CC(i,a) memset(i,a,sizeof(i))
  14. #define read(a) a=getint()
  15. #define print(a) printf("%d", a)
  16. #define dbg(x) cout << #x << " = " << x << endl
  17. #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
  18. 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; }
  19. inline const int max(const int &a, const int &b) { return a>b?a:b; }
  20. inline const int min(const int &a, const int &b) { return a<b?a:b; }
  21.  
  22. const int N=1000005;
  23. int n, a[N], cnt;
  24. int main() {
  25. read(n);
  26. for1(i, 1, n) read(a[i]);
  27. sort(a+1, a+1+n);
  28. int x;
  29. while(~scanf("%d", &x)) printf("%d\n", (int)(lower_bound(a+1, a+1+n, x)-a) );
  30. return 0;
  31. }

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

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <string>
  5. #include <iostream>
  6. #include <algorithm>
  7. using namespace std;
  8. #define rep(i, n) for(int i=0; i<(n); ++i)
  9. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  10. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  11. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  12. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  13. #define CC(i,a) memset(i,a,sizeof(i))
  14. #define read(a) a=getint()
  15. #define print(a) printf("%d", a)
  16. #define dbg(x) cout << #x << " = " << x << endl
  17. #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
  18. 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; }
  19. inline const int max(const int &a, const int &b) { return a>b?a:b; }
  20. inline const int min(const int &a, const int &b) { return a<b?a:b; }
  21.  
  22. const int N=1000005;
  23. int n, a[N], siz, lst[N];
  24. int ifind(const int &x) {
  25. if(x<a[1]) return 1;
  26. for1(i, 1, siz+1) if(lst[i]>=x) {
  27. int ret=(i-1)*siz, j=0;
  28. while(a[ret+j]<x) ++j;
  29. return ret+j;
  30. }
  31. return n+1;
  32. }
  33. int main() {
  34. read(n);
  35. for1(i, 1, n) read(a[i]);
  36. sort(a+1, a+1+n);
  37. siz=sqrt(n);
  38. for1(i, 1, siz) lst[i]=a[i*siz];
  39. lst[siz+1]=a[n];
  40. int x;
  41. while(~scanf("%d", &x)) printf("%d\n", ifind(x));
  42. return 0;
  43. }

背景 Background

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

描述 Description

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

输入格式 InputFormat

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

输出格式 OutputFormat

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

样例输入 SampleInput [复制数据]

  1. 5
    1 2 3 4 5
    1
    2
    3
    4
    5

样例输出 SampleOutput [复制数据]

  1. 1
    2
    3
    4
    5

数据范围和注释 Hint

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

来源 Source

Bob HAN

【TYVJ】1463 - 智商问题(二分/分块)的更多相关文章

  1. TYVJ 1463 智商问题 分块

    TYVJ 1463 智商问题 Time Limit: 1.5 Sec  Memory Limit: 512 MB 题目连接 http://www.tyvj.cn/p/1463 背景 各种数据结构帝~ ...

  2. Tyvj 题目1463 智商问题(分块)

    P1463 智商问题 时间: 1500ms / 空间: 131072KiB / Java类名: Main 背景 各种数据结构帝~各种小姊妹帝~各种一遍AC帝~ 来吧! 描述 某个同学又有很多小姊妹了他 ...

  3. 【TYVJ 1463】智商问题 (闲得无聊)

    老老实实写二分. #include<cstdio> #include<algorithm> using namespace std; int n, a[1000001], x; ...

  4. Tyvj P1463 智商问题 分块

    P1463 智商问题 时间: 1500ms / 空间: 131072KiB / Java类名: Main 背景 各种数据结构帝~各种小姊妹帝~各种一遍AC帝~ 来吧! 描述 某个同学又有很多小姊妹了他 ...

  5. [luoguP2801] 教主的魔法(二分 + 分块)

    传送门 以为对于这类问题线段树都能解决,分块比线段树菜,结果培训完才知道线段树是一种特殊的分块方法,有的分块的题线段树不能做,看来分块还是有必要学的. 对于这个题,先分块,然后另开一个数组对于每个块内 ...

  6. 【BZOJ】2453: 维护队列【BZOJ】2120: 数颜色 二分+分块(暴力能A)

    先说正解:把所有相同的数相成一个链在每一个区间里的种数就是不同链的链头,那么记录每个数的上个相同数所在位置,那么只要找出l到r之间前驱值在l之前的数的个数就可以了 本人打的暴力,有一个小技巧,用cha ...

  7. 【二分答案】【分块答案】【字符串哈希】【set】bzoj2946 [Poi2000]公共串

    我们二分/分块枚举答案x,暴力把除了最短的字符串以外的其他字符串的x长度子串哈希搞出来,分别扔到set里. 然后暴力枚举最短的字符串的x长度字串,查看是否在全部的set里出现过. #include&l ...

  8. acdream 1738 世风日下的哗啦啦族I 分块

    世风日下的哗啦啦族I Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acdream.info/problem?pid=1738 Descri ...

  9. 【分块答案】【最小生成树】【kruscal】bzoj1196 [HNOI2006]公路修建问题

    二分(分块)枚举 边权上限.用kruscal判可行性. #include<cstdio> #include<algorithm> #include<cstring> ...

随机推荐

  1. poj2993 翻转2996

    Emag eht htiw Em Pleh Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2944   Accepted:  ...

  2. 15 day 1代碼

    第一题 用堆维护. #include <cstdio> #include <algorithm> #include <queue> int n,i,f[400000 ...

  3. 【Spring】Spring系列1之Spring概述

    概述

  4. iterator与const_iterator及const iterator区别

    如果你传递过来一个const类型的容器,那么只能用const_iterator来遍历.  C++ Code  12345   void Method(const vector<int> v ...

  5. 父页面刷新 保持iframe页面url不变

    思路:点击父页面时写cookies-->刷新时从cookies中奖内容读取出来. 本文转自:http://blog.163.com/sdolove@126/blog/static/1146378 ...

  6. 【转】javax.net.ssl.SSLHandshakeException(Cas导入证书)

    本文转自:http://my.oschina.net/laiwanshan/blog/159057 一.报错: javax.net.ssl.SSLHandshakeException 二.原因分析:C ...

  7. codeforces B. Xenia and Ringroad 解题报告

    题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...

  8. iphone越狱还原

    在Cydia 里安装Impactor 就行了 .在操作时需要全程联网: .请至少保证 % 的电量以防止在恢复过程出现断电的情况(建议将设备连接至电源): .设备将恢复至出厂状态,所有用户数据都将被清除 ...

  9. Session的使用(登录例案+其它页面访问)

    本程序功能是使用Session将用户输入的用户名保存在Session中(登录成功情况下,登录失败不会有Session值),其它页面想访问时会先判断是否有之前存的Session值. 登录Login.ht ...

  10. Hibernate常见问题

    问题1,hql条件查询报错 执行Query session.createQuery(hql) 报错误直接跳到finally 解决方案 加入 <prop key="hibernate.q ...