题意:求中位数,以及能成为中位数的数的个数,以及选择不同中位数中间的可能性。

也就是说当数组个数为奇数时,中位数就只有一个,中间那个以及中位数相等的数都能成为中位数,选择的中位数就只有一种可能;如果为偶数,中位数又两个,同样和那两个相等的数都能成为中位数,在[第一个中位数,第二个中位数]这一区间中拥有的整数个数就是所求的第三个数。

分类讨论,模拟即可。

代码:

 /*
* Author: illuz <iilluzen@gmail.com>
* Blog: http://blog.csdn.net/hcbbt
* File: uva10057.cpp
* Lauguage: C/C++
* Create Date: 2013-08-25 11:30:04
* Descripton: UVA 10057 A mid-summer night's dream, simulation
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <utility>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repu(i, a, b) for (int i = (a); i < (b); i++)
#define repf(i, a, b) for (int i = (a); i <= (b); i++)
#define repd(i, a, b) for (int i = (a); i >= (b); i--)
#define swap(a, b) {int t = a; a = b; b = t;}
#define mc(a) memset(a, 0, sizeof(a))
#define ms(a, i) memset(a, i, sizeof(a))
#define sqr(x) ((x) * (x))
#define FI(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
typedef long long LL;
typedef unsigned long long ULL; /****** TEMPLATE ENDS ******/ const int MAXN = 1000000;
int n, num[MAXN], n1, n2; int main() {
while (scanf("%d", &n) != EOF) {
rep(i, n) {
scanf("%d", &num[i]);
}
sort (num, num + n);
int k = (n - 1) / 2;
n1 = 0; n2 = 1;
if (n % 2) {
for (int i = k; i >= 0 && num[k] == num[i]; i--)
n1++;
for (int i = k + 1; i < n && num[k] == num[i]; i++)
n1++;
}
else {
for (int i = k; i >= 0 && num[k] == num[i]; i--)
n1++;
for (int i = k + 1; i < n && num[k + 1] == num[i]; i++)
n1++;
n2 = num[k + 1] - num[k] + 1;
}
printf("%d %d %d\n", num[k], n1, n2);
}
return 0;
}

UVA 10057 A mid-summer night's dream. 仲夏夜之梦 求中位数的更多相关文章

  1. UVA - 10057 A mid-summer night&#39;s dream.

    偶数时,中位数之间的数都是能够的(包含中位数) 奇数时,一定是中位数 推导请找初中老师 #include<iostream> #include<cstdio> #include ...

  2. UVa 10057 - A mid-summer night's dream

    题目大意:给n个数,找一个数A使得A与这n个数的差的绝对值最小.输出A最小的可能值,n个数中满足A的性质的数的个数以及满足A性质的不同的数的个数(不必从这n个数中挑选). 看见绝对值就想到了数轴上点之 ...

  3. UVa 11437:Triangle Fun(计算几何综合应用,求直线交点,向量运算,求三角形面积)

    Problem ATriangle Fun Input: Standard Input Output: Standard Output In the picture below you can see ...

  4. UVA 11174 Stand in a Line (组合+除法的求模)

    题意:村子里有n个人,给出父亲和儿子的关系,有多少种方式可以把他们排成一列,使得没人会排在他父亲的前面 思路:设f[i]表示以i为根的子树有f[i]种排法,节点i的各个子树的根节点,即它的儿子为c1, ...

  5. UVA 796 Critical Links(模板题)(无向图求桥)

    <题目链接> 题目大意: 无向连通图求桥,并将桥按顺序输出. 解题分析: 无向图求桥的模板题,下面用了kuangbin的模板. #include <cstdio> #inclu ...

  6. UVA 10522 Height to Area(知三角形三高求面积)

    思路:海伦公式, AC代码: #include<bits/stdc++.h> using namespace std; int main() { int n; scanf("%d ...

  7. uva 11300 分金币(利用绝对值加和进行求出最小值)

    //qq 767039957 welcome #include<cstdio> #include<algorithm> #include<vector> #incl ...

  8. UVA - 12295 最短路(迪杰斯特拉)——求按对称路线最短路条数

    题意: 给你一个n,然后给你一个n*n的正方形w[i][j],你需要找到一个从(1,1)点走到(n,n)点的最短路径数量.而且这个路径必须按照y=x对称 题解: 我们把左上角的点当作(0,0)点,右下 ...

  9. 【LeetCode】480. 滑动窗口中位数 Sliding Window Median(C++)

    作者: 负雪明烛 id: fuxuemingzhu 公众号: 每日算法题 本文关键词:LeetCode,力扣,算法,算法题,滑动窗口,中位数,multiset,刷题群 目录 题目描述 题目大意 解题方 ...

随机推荐

  1. UVa1225 Digit Counting

    #include <stdio.h>#include <string.h> int main(){    int T, N, i, j;    int a[10];    sc ...

  2. JavaSE学习总结第27天_反射 & 设计模式 & JDK5、7、8新特性

      27.01  反射_类的加载概述和加载时机 类的加载:当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 加载:就是指将class文件读 ...

  3. C++对象模型--C++对象模型

    何为C++对象模型? 部分: 1       语言中直接支持面向对象程序设计的部分 2       对于各种支持的底层实现机制 语言中直接支持面向对象程序设计的部分,如构造函数.析构函数.虚函数.继承 ...

  4. C#操作Office.word(一)

    该文章主要是讲述如何使用VS2010创建word文档,因为在项目中我们可能需要点击一个按钮把数据库中的项目表单或图片显示到word文档中,因此该文章主要分析如何使用VS2010创建word文档并填写相 ...

  5. What I Have Lived For(我为什么而活着-罗素)

    What I Have Lived For by Bertrand Russell Three passions, simple but overwhelmingly strong, have gov ...

  6. CDOJ 1259 昊昊爱运动 II bitset+线段树

    题目链接 昊昊喜欢运动 他N天内会参加M种运动(每种运动用一个[1,m]的整数表示) 现在有Q个操作,操作描述如下 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) 问昊昊第l天到第r天参加了 ...

  7. python实现进度条

    先说一下文本系统的控制符: \r: 将光标移动到当前行的首位而不换行: \n: 将光标移动到下一行,并不移动到首位: \r\n: 将光标移动到下一行首位. 环境: root@ubuntu16:/ale ...

  8. [转]IOS Segment页面之间view的切换

    有三个view,分别为view1.view2.view3,通过UISegmentedControl进行三个view的切换. @interface UIViewDemoViewController :  ...

  9. Protel 99SE铺铜问题总结

    一.PCB电路板放置铺铜有什么作用? 散热: 屏蔽 抗干扰 pcb板子带有寄生电容: 提高板子强度: 美观: 增加被抄板的难度,尤其是覆铜+黑油.   二.PROTEL不规则铺铜的方法: 1.先要知道 ...

  10. PHP第一章学习——了解PHP(下)

    继续昨天的部分! —————————————————————————————— 首先Ubuntu下安装Apache软件: ubuntu更新源有问题,又要解决半天! 我现在很冷静! 安装Apache教程 ...