uva 10474 Where is the Marble? 计数排序
2013-07-23 11:48
501 查看
题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个。
数据范围为10000,不大,完全可以暴力,sort不会超时。
但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力被hack了。之后就学会了计数排序了。
这题也用计数排序做,挺快的,代码也不长。
代码:
数据范围为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)); //init memset(s, 0, sizeof(s)); for (int i = 1; i <= n; i++) { //input scanf("%d", &tmp); if (m < tmp) m = tmp; num[tmp]++; } int sum = 0; for (int i = 1; i <= m; i++) //calc the sum s[i] = s[i - 1] + num[i]; for (int i = 1; i <= q; i++) { //output scanf("%d", &tmp); if (num[tmp]) printf("%d found at %d\n", tmp, s[tmp - 1] + 1); else printf("%d not found\n", tmp); } } return 0; }
相关文章推荐
- 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? (二分查找&equal_range()的使用)
- 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?
- 【STL基础04】UVA-10474-Where is the Marble?
- UVa_10474 - Where is the Marble?
- uva 10474 Where is the Marble?
- uva10474 - Where is the Marble?
- UVa 10474 Where is the Marble?
- UVA 10474 - Where is the Marble?
- UVa10474 Where is the Marble ? 有序数组二分找值,lower_bound / upper_bound
- UVA10474 Where is the Marble?