題目串連: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;}