CodeForces 166C
来源:互联网 发布:数据质量盘点标准 编辑:程序博客网 时间:2024/05/22 17:18
A median in an array with the length of n is an element which occupies position number after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17.
We define an expression as the integer part of dividing number a by number b.
One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x.
Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array.
While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need.
The first input line contains two space-separated integers n and x (1 ≤ n ≤ 500, 1 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different.
Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x.
3 1010 20 30
1
3 41 2 3
4
In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position , that is, 10.
In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4.
题意:给定一个数字x,和已知的几个数,问需要添加多少个数字使得所给的数字x成为中位数,这里有一个不同的是当最终形成的数组的元素个数是偶数个时候,中位数是第2/n个数字。
思路:思路题,想好每一种情况就可以了。中位数左边的数的个数l和右边的数的个数r相等,或者l == r-1的时候也可以。按照这个思路来进行情况的判断。这样弄出来以后还不行,因为第一组样例不过,后来想到了,如果里面有y个x,情况就又不一样,于是情况的判断变成了,有0个x的时候,1个x的时候,多个x的时候。附上代码
#include<bits/stdc++.h>using namespace std;int num[1000];int main(){ int n,x; scanf("%d%d",&n,&x); int l = 0,r = 0,sam = 0; for(int i = 1;i <= n;i++) { int m;scanf("%d",&m); if(m == x) sam++; else if(m > x) r++; else l++; } int ans = 0; if(sam == 0) { if(abs(r - l)<=1&&r>l) printf("1\n"); else if(r - l>1) printf("%d\n",r - l); else { printf("%d\n",l-r+1); } } else if(sam == 1) { if(abs(r - l)<=1&&r > l) printf("0\n"); else if(r - l>1) printf("%d\n",r - l - 1); else printf("%d\n",l - r); } else { if(l + sam < r) printf("%d\n",r-l-sam); else if(l+sam >= r&&l < sam + r) printf("0\n"); else printf("%d\n",l - r- sam+1); }}
- CodeForces 166C
- CodeForces 166C Median
- CodeForces 166C--stl
- CodeForces 166C - Median
- codeforces 166C Median 中位数
- Codeforces Round #166 (Div. 2) c. Secret
- codeforces 166C Median - from lanshui_Yang
- Codeforces 166C Median 暴力模拟
- Codeforces-340-C(c++)
- Codeforces-507-C(c++)
- CodeForces 731C C
- CodeForces-612C C
- CODEFORCES 265C CODEFORCES, 265C
- codeforces #78 div2 C
- Codeforces 117C Cycle
- CodeForces 209C
- CodeForces 156C Cipher
- codeforces 148 div2 C
- tomcat内存溢出
- adb logcat 命令行用法
- oracle基本函数
- 展开拉伸动画---属性动画
- Android 操作SQLite基本用法
- CodeForces 166C
- 自定义导航栏(附带利用单例传值)
- Intent传递Map
- spring+mvc+mybatis+maven(上传+拦截+json出现下载问题)
- 11.Java 基础 - 泛型
- 习题3 数字和数学计算
- 面试出Android开发者的真正水平
- 11_Power数值的整数次方
- 仿淘宝的Flexible进行H5页面进行适配mobile