uva 10474 Where is the Marble? 计数排序
来源:互联网 发布:mac能玩单机游戏 编辑:程序博客网 时间:2024/04/28 13:51
题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个。
数据范围为10000,不大,完全可以暴力,sort不会超时。
但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力被hack了。之后就学会了计数排序了。
这题也用计数排序做,挺快的,代码也不长。
代码:
#include <cstdio>#include <cstring>const int maxn = 10001;int num[maxn], s[maxn];int main() {int n, q, tmp, m = 0, cnt = 0;while (scanf("%d%d", &n, &q) && (n || q)) {printf("CASE# %d:\n", ++cnt);memset(num, 0, sizeof(num));//initmemset(s, 0, sizeof(s));for (int i = 1; i <= n; i++) {//inputscanf("%d", &tmp);if (m < tmp)m = tmp;num[tmp]++;}int sum = 0;for (int i = 1; i <= m; i++)//calc the sums[i] = s[i - 1] + num[i];for (int i = 1; i <= q; i++) {//outputscanf("%d", &tmp);if (num[tmp])printf("%d found at %d\n", tmp, s[tmp - 1] + 1);elseprintf("%d not found\n", tmp);}}return 0;}
- Where is the Marble? uva 计数排序
- uva 10474 Where is the Marble?(计数排序)
- uva 10474 Where is the Marble? 计数排序
- uva 10474 - Where is the Marble?(排序,二分搜索)
- UVA 10474 (暑假-排序、检索 -C - Where is the Marble?)
- uva 10474 Where is the Marble?(排序)
- UVa 10474 - Where is the Marble?【排序和检索】
- Uva 10474 Where is the Marble?(排序与检索)
- UVA.10474 Where is the Marble ( 排序 二分查找 )
- UVa 10474 Where is the Marble?
- Uva 10474 Where is the Marble?
- uva 10474 Where is the Marble?
- UVA 10474 Where is the Marble?
- UVA 10474 - Where is the Marble
- uva-10474 - Where is the Marble?
- uva 10474 - Where is the Marble?
- UVA 10474 - Where is the Marble?
- UVa 10474 - Where is the Marble?
- 推荐给开发者的20款响应式jQuery插件(收藏)
- skew-symmetric matrix
- ThreadPool
- 总结合理发布外链的小方法!
- JRUBY_HOME会造成hbase shell起动错误
- uva 10474 Where is the Marble? 计数排序
- 团购网站两向发展互联网应创新
- 快速过滤掉List集合中重复项
- 在Linux系统上动态修改MySQL最大进程数
- 交叉编译工具安装
- 死机最新资料大全
- The specified child already has a parent. You must call removeView() on the child's parent first.
- 程序员技术练级攻略
- 面对绿萝算法如今网站优化法宝因素:互动性