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

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

分类讨论,模拟即可。

代码:

 /*
* 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. spring MVC中文乱码相关总结

    总结几种方式,都使用的话能解决大多数乱码的情况 1.所有页面使用 <%@page language="java" pageEncoding="UTF-8" ...

  2. 求新的集合 A=AUB(顺序表)

    #include<stdio.h> typedef int A; const int LIST_INIT_SIZE=100; const int LISTINCREMENT=10; typ ...

  3. UVa 10806 Dijkstra,Dijkstra(最小费用最大流)

    裸的费用流.往返就相当于从起点走两条路到终点. 按题意建图,将距离设为费用,流量设为1.然后增加2个点,一个连向节点1,流量=2,费用=0;结点n连一条同样的弧,然后求解最小费用最大流.当且仅当最大流 ...

  4. [C#编程参考]把图像转换为数组的两种实现

    当一个程序和一个图片放在一起,无非有两种操作: 第一种,就是传输这个图片,在传输图片之前要首先把这个图片变成byte类型的数组.所以这时候我们用到的是图片的存储的数据,也就是图片属性中的大小.我们并不 ...

  5. python3.4 使用pymssql 乱码

    问题:sqlserver数据库编码为gbk,使用python3.4+pymssql 查询,中文乱码 解决办法: conn = pymssql.connect(host="192.168.12 ...

  6. [LeetCode]题解(python):132-Palindrome Partitioning II

    题目来源: https://leetcode.com/problems/palindrome-partitioning-ii/ 题意分析: 给定一个s,可以将s拆成若干个回文子字符串之和,如果拆成了m ...

  7. poj 3358

    /** 大意: 给定小数(p/q),求其循环节的大小和循环节开始的位置 解法: 若出现循环 ai*2^m= aj%p; 即 2^m %p =1 若2与p 互素,则可由欧拉函数的, 不互素,需将其转化为 ...

  8. Linux第三方源

    由于版权等各方面原因,很多时候在基础安装完Linux后,满多软件并不包含在yum(对于Ubuntu可能是apt-get)源中. 因此可以去下载第三方源,安装下载第三方软件.当然,如果习惯了源代码编译安 ...

  9. 读书笔记:js设计模式

    面向过程编程,面向对象编程和函数式编程> 定义一个类方法1:function Anim(){ } Anim.prototype.start = function(){ .. };Anim.pro ...

  10. 在Livemedia的基础上开发自己的流媒体客户端

    一.背景 二.Livemedia框架介绍 1.总体框架 2.客户端框架 2.1 客户端openRTSP流程 2.2增加一种新的媒体 2.2.1增加媒体的format 2.2.2 新媒体需要考虑的问题 ...