uva 10057 A mid-summer night's dream.(检索)

题目连接:10057 A mid-summer night's dream.


题目大意:找到使得给出算式最小的值,如果有多个,输出最小的,然后在输出所给的数组A中有多少个数值可以满足算式最小(相等的要分开计算),随后输出有多少个整数满足算式值虽小(没有在数组A中出现也要计算)。


解题思路:本体主要题目转换后就是找数组A的中位数,如果给出的n为奇数, 所要找的就是中间的那个值,然后遍历A统计相等的个数就可以了, ans就是1.

如果n为偶数,所要找的就是中间两个,而且由A[a] 到A[b]之间所有整数都满足。


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int N = 1000005;
int n, num[N];
int count(int cur) {
    int cnt = 0;
    for (int i = 0; i < n; i++) {
	if (num[i] == num[cur])
	    cnt++;
	else if (num[i] > num[cur])
	    break;
    }
    return cnt;
}
int main() {
    int cur, cnt, sum;
    while (scanf("%d", &n) == 1) {
	cur = n / 2;
	for (int i = 0; i < n; i++)
	    scanf("%d", &num[i]);
	sort(num, num + n);
	if (n % 2) {
	    sum = count(cur);
	    cnt = 1;
	}
	else {
	    sum = count(--cur);
	    if (num[cur] == num[cur + 1])
		cnt = 1;
	    else {
		cnt = num[cur + 1] - num[cur] + 1;
		sum += count(cur + 1);
	    }
	}
	printf("%d %d %d\n", num[cur], sum, cnt); 
    }
    return 0;
}


你可能感兴趣的:(uva 10057 A mid-summer night's dream.(检索))