UVA 10057 A mid-summer night's dream. 仲夏夜之梦 求中位数
题意:求中位数,以及能成为中位数的数的个数,以及选择不同中位数中间的可能性。
也就是说当数组个数为奇数时,中位数就只有一个,中间那个以及中位数相等的数都能成为中位数,选择的中位数就只有一种可能;如果为偶数,中位数又两个,同样和那两个相等的数都能成为中位数,在[第一个中位数,第二个中位数]这一区间中拥有的整数个数就是所求的第三个数。
分类讨论,模拟即可。
代码:
/*
* 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. 仲夏夜之梦 求中位数的更多相关文章
- UVA - 10057 A mid-summer night's dream.
偶数时,中位数之间的数都是能够的(包含中位数) 奇数时,一定是中位数 推导请找初中老师 #include<iostream> #include<cstdio> #include ...
- UVa 10057 - A mid-summer night's dream
题目大意:给n个数,找一个数A使得A与这n个数的差的绝对值最小.输出A最小的可能值,n个数中满足A的性质的数的个数以及满足A性质的不同的数的个数(不必从这n个数中挑选). 看见绝对值就想到了数轴上点之 ...
- UVa 11437:Triangle Fun(计算几何综合应用,求直线交点,向量运算,求三角形面积)
Problem ATriangle Fun Input: Standard Input Output: Standard Output In the picture below you can see ...
- UVA 11174 Stand in a Line (组合+除法的求模)
题意:村子里有n个人,给出父亲和儿子的关系,有多少种方式可以把他们排成一列,使得没人会排在他父亲的前面 思路:设f[i]表示以i为根的子树有f[i]种排法,节点i的各个子树的根节点,即它的儿子为c1, ...
- UVA 796 Critical Links(模板题)(无向图求桥)
<题目链接> 题目大意: 无向连通图求桥,并将桥按顺序输出. 解题分析: 无向图求桥的模板题,下面用了kuangbin的模板. #include <cstdio> #inclu ...
- UVA 10522 Height to Area(知三角形三高求面积)
思路:海伦公式, AC代码: #include<bits/stdc++.h> using namespace std; int main() { int n; scanf("%d ...
- uva 11300 分金币(利用绝对值加和进行求出最小值)
//qq 767039957 welcome #include<cstdio> #include<algorithm> #include<vector> #incl ...
- UVA - 12295 最短路(迪杰斯特拉)——求按对称路线最短路条数
题意: 给你一个n,然后给你一个n*n的正方形w[i][j],你需要找到一个从(1,1)点走到(n,n)点的最短路径数量.而且这个路径必须按照y=x对称 题解: 我们把左上角的点当作(0,0)点,右下 ...
- 【LeetCode】480. 滑动窗口中位数 Sliding Window Median(C++)
作者: 负雪明烛 id: fuxuemingzhu 公众号: 每日算法题 本文关键词:LeetCode,力扣,算法,算法题,滑动窗口,中位数,multiset,刷题群 目录 题目描述 题目大意 解题方 ...
随机推荐
- AngularJS Directive 学习笔记
指令 Directive 指令要点 大漠老师的教学节点 解析最简单的指令 hello: 匹配模式 restrict 解析最简单的指令 hello: template.tempmlateUrl.$tem ...
- 大夏VIEW使用技巧
HDXTextView *remark=[[HDXTextView alloc] initWithFrame:CGRectMake(leftmargin-5,labbackView.top, cell ...
- C++如何将一个整数输出为小数
double a=3; //一定是double不能是int cout.setf(ios::fixed); cout.precision(2); cout< ...
- Android应用开发基础篇(12)-----Socket通信
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/03/2378669.html 一.概述 网络通信无论在手机还是其他设备上都应用得非常广泛,因此掌 ...
- gsoap 超时(timeout)设置
参考:http://www.cs.fsu.edu/~engelen/soapdoc2.html#tth_sEc19.19 gsoap就不用介绍了,是一个c/c++编写的可用于服务端与客户端的连接工具. ...
- [LeetCode]题解(python):007-Reverse Integer
题目来源: https://leetcode.com/problems/reverse-integer/ 题意分析: 这道题目很简单,就是将一个数反转,123变321,-123变321. 题目思路: ...
- Python 安装、循环语句、数据类型(一)
一.关于版本的选择 Should i use Python 2 or Python 3 for my development activity?转载自Python官网 Short version: P ...
- ural1057 Amount of degrees 位数统计
#include <iostream> #include <string> using namespace std; ][]; void init(){ f[][] =; ;i ...
- .net web初级工程师教程
序 这份教程,只针对正在努力找工作的初级.net web工程师,软件这行,刚入门时找工作是个坎,希望教程对各位有帮助. 教程将通过一个实际项目,简单明了地完整呈现,在实际工作中,工程师都做些什么及怎么 ...
- QSlider解决点击不能到该位置问题
方法一:可以继承重写一个QSlider 方法二:1.instaneventfiliter(this);slider安装一个事件过滤器 2.在eventfilter(QObject *,QEvent*) ...