转载注明出处:http://www.cnblogs.com/ligun123/archive/2013/03/27/2984740.html
题目来源:https://www.hackerrank.com/challenges/median
The median of M numbers is defined as the middle number after sorting them in order if M is odd or the average number of the middle 2 numbers (again after sorting) if M is even. You have an empty number list at first. Then you can add or remove some number from the list. For each add or remove operation, output the median of numbers in the list.
Example :
For a set of m = 5 numbers, { 9, 2, 8, 4, 1 } the median is the third number in sorted set { 1, 2, 4, 8, 9 } which is 4. Similarly for set of m = 4, { 5, 2, 10, 4 }, the median is the average of second and the third element in the sorted set { 2, 4, 5, 10 } which is (4+5)/2 = 4.5
Input:
The first line is an integer n indicates the number of operations. Each of the next n lines is either “a x” or “r x” which indicates the operation is add or remove.
Output:
For each operation: If the operation is add output the median after adding x in a single line. If the operation is remove and the number x is not in the list, output “Wrong!” in a single line. If the operation is remove and the number x is in the list, output the median after deleting x in a single line. (if the result is an integer DO NOT output decimal point. And if the result is a double number , DO NOT output trailing 0s.)
Constraints:
0 < n <= 100,000
For each “a x” or “r x”, x will always be an integer which will fit in 32 bit signed integer.
Sample Input:
7
r 1
a 1
a 2
a 1
r 1
r 2
r 1
Sample Output:
Wrong!
1
1.5
1
1.5
1
Wrong!
Note: As evident from the last line of the input, if after remove operation the list becomes empty you have to print “Wrong!” ( quotes are for clarity ).
解题思路:
还是很简单的,新开一个数组result存放执行a、r命令后的数据,然后a我用插入排序,r的时候用二分查找,找到了就移除result中的这个数,还要注意调整此数后面的数据的index,没找到继续执行在string中的下一个命令。然后要注意最后输出的数据格式。
#include <iostream>
#include <fstream>
#include <iomanip>
usingnamespacestd;
//insert sort
void insertionSort(int ar_size, int * ar) {
/* 从外围循环控制对有序数组插入
if (ar_size > 1) {
insertionSort(ar_size-1, ar);
}
*/
if (ar_size == 1) {
return;
}
int key = ar[ar_size-1];
for (int i = ar_size -2; i >= 0; i --) {
if (key < ar[i]) {
ar[i +1] = ar[i];
} else {
ar[i +1] = key;
break;
}
if (i ==0) {
ar[i] = key;
}
}
}
int findValue(int value, int *arr, int size)
{//arr升序,返回value的index,如果没找到返回size
int begin = 0;
int end = size -1;
while (begin <= end) {
int mid = (begin + end) /2;
if (value < arr[mid]) {
end = mid -1;
} else if (value > arr[mid]) {
begin = mid +1;
} else return mid;
}
return size;
}
int main(int argc, const char * argv[])
{
int N = 0;
cin >> N;
char s[N];
int x[N];
int *result = (int *)calloc(N, sizeof(int));
int pResult = 0; //result数组里面数据个数
for(int i = 0; i < N; i++){
cin >> s[i] >> x[i];
}
for(int i = 0; i < N; i++){ //i标示指令执行顺序
char order = s[i];
if (order == 'a') {
//add
result[pResult] = x[i];
pResult ++;
insertionSort(pResult, result);
} else if (order == 'r') {
//remove
if (pResult < 1) {
cout<<"Wrong!"<<endl;
continue;
} else {
int index = findValue(x[i], result, pResult);
if (index == pResult) {
cout<<"Wrong!"<<endl;
continue;
} else {
for (int j =index; j < pResult -1; j ++) {
result[j] = result[j +1];
}
pResult --;
}
}
} else { //error order
break;
}
/*/
printf("\n*******************************");
for (int j =0; j < N; j ++) {
printf("%d \n", result[j]);
}
//*/
if (pResult < 1) {
cout<<"Wrong!"<<endl;
}
else if (pResult == 1) {
cout<<result[pResult -1]<<endl;
// printf("%d\n",result[pResult -1]);
} else {
int plus = result[(pResult-1) /2] + result[(pResult-1) /2 + (pResult-1) %2];
double r = plus / 2.0;
if (plus % 2) {//奇数,所得的r则是整数
// printf("%.1f\n", r);
cout.setf(ios::fixed);
cout.unsetf(ios::showpoint);
cout<<r<<endl;
} else {
// printf("%d\n", (int)r);
cout<<(int)r<<endl;
}
}
}
free(result);
return 0;
}