Codechef Racing Horses题解
2014-05-05 08:39
351 查看
找一个数组中两数之间最小的不同值。
思路:
1 排序
2 后前相减,比较得到最小不同值
三个数甚至更多数之间的不同值都是这么求了,时间效率都是O(nlgn) -- 排序使用的时间
原题:
http://www.codechef.com/problems/HORSES
笔者的练习文件很大,所以还是使用类好,可以减少变量名和函数名的冲突。namespace有时候也不好用。
思路:
1 排序
2 后前相减,比较得到最小不同值
三个数甚至更多数之间的不同值都是这么求了,时间效率都是O(nlgn) -- 排序使用的时间
原题:
http://www.codechef.com/problems/HORSES
笔者的练习文件很大,所以还是使用类好,可以减少变量名和函数名的冲突。namespace有时候也不好用。
#include <cstdio> #include <algorithm> #include <assert.h> using std::qsort; class RacingHouse { const static int MAX_INT = -((1<<31)+1); public: int scanInt() { char c = getchar(); while (c < '0' || '9' < c) { c = getchar(); } int num = 0; while ('0' <= c && c <= '9') { num = (num<<3) + (num<<1) + c - '0'; c = getc(stdin); } return num; } void run() { auto cmp = [](const void *a, const void *b) { return *(int *)a - *(int *)b; }; int T = scanInt(); while (T--) { int n = scanInt(); assert(1 < n); int *A = new int ; for (int i = 0; i < n; i++) { A[i] = scanInt(); } qsort(A, n, sizeof(int), cmp); int minDiff = MAX_INT; for (int i = 1; i < n; i++) { if (A[i] - A[i-1] < minDiff) { minDiff = A[i] - A[i-1]; } } printf("%d\n", minDiff); } } }; int racingHouseRun() { RacingHouse rh; rh.run(); return 0; }
相关文章推荐
- Codechef Racing Horses题解
- codechef Prime Palindromes 题解
- codechef Recipe Reconstruction 题解
- 【位运算分析+主席树】CodeChef(PREFIXOR)[Prefix XOR]题解
- codechef The Ball And Cups题解
- codechef Sums in a Triangle题解
- codechef Ciel and Receipt题解
- codechef - Bytelandian gold coins 题解
- codechef Top Batsmen题解
- codechef Little Elephant and Permutations题解
- Codechef July Challenge 2014部分题解
- Codechef Maximum Weight Difference题解
- codechef Chef and The Right Triangles 题解
- code chef - Counting Matrices题解
- codechef Little Elephant and Permutations题解
- codechef Sums in a Triangle题解
- codechef Correctness of Knight Move题解
- [题解]CodeChef APRIL Challenge 17
- codechef February Challenge 2018 简要题解
- codechef Ambiguous Permutations 题解