UVa 10107 What is the Median?
2013-10-03 10:39
211 查看
10107 - What is the Median?
Time limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1048
The Problem
Median plays an important role in the world of statistics. By definition, it is a value which divides an array into two equal parts. In this problem you are to determine the current median of some long integers.Suppose, we have five numbers {1,3,6,2,7}. In this case, 3 is the median as it has exactly two numbers on its each side. {1,2} and {6,7}.
If there are even number of values like {1,3,6,2,7,8}, only one value cannot split this array into equal two parts, so we consider the average of the middle values {3,6}. Thus, the median will be (3+6)/2 = 4.5. In this
problem, you have to print only the integer part, not the fractional. As a result, according to this problem, the median will be 4!
Input
The input file consists of series of integers X ( 0 <= X < 2^31 ) and total number of integers N is less than 10000. The numbers may have leading or trailing spaces.Output
For each input print the current value of the median.Sample Input
1 3 4 60 70 50 2
Sample Output
1 2 3 3 4 27 4
完整代码:
/*0.035s*/ #include<cstdio> int a[10010]; int main() { int i, x, cnt = 0, m; while (~scanf("%d", &x)) { for (i = cnt; i; i--) { if (a[i] > x) a[i + 1] = a[i]; else break; } a[i + 1] = x; ++cnt; m = cnt >> 1; printf("%d\n", (cnt & 1 ? a[m + 1] : (a[m] + a[m + 1]) >> 1)); } return 0; }
PS:使用 Treap 复杂度更低。
相关文章推荐
- UVa 10107 - What is the Median?
- uva 10107 What is the Median?
- uva 10107 - What is the Median?
- Uva 10107 - What is the Median?
- UVA - 10056 What is the Probability ? 概率UVA - 10056 What is the Probability ?
- UVA 10056 What is the Probability ?
- uva 10056 What is the Probability?
- UVa10107 What is the Median?
- UVA10107 What is the Median?
- uva 10056 - What is the Probability ?(概率)
- UVA 10056 - What is the Probability ?(概率)
- UVA10056 - What is the Probability ?(概率)
- uva 10056 - What is the Probability ?(概率)
- UVa 10646 - What is the Card?
- UVA - 10056 What is the Probability ?(概率)
- What is the Probability - UVa 10056 概率
- uva 10056 What is the Probability ?(简单概率)
- uva 10056 What is the Probability ? 概率是多少
- UVa 10056 What is the Probability ? (概率&有一个陷阱)
- UVA 10056 What is the Probability ?