POJ 3269 中位数】的更多相关文章

题意: 思路: 这道题坑也不少.. 你准备好脑洞了么? 首先 要认真审题 题目中有说:"没有两头牛的吃草位置是相邻的" 这句话让我们省了很多的事儿 (Discuss里有的大神就入了这个坑了) 然后呢 自然想到了中位数 (不要问我怎么想到的) 但是如果n为偶数怎么办呢 就取两个中间位置的数那段区间呗~ 本以为随便搞搞 n的取值 干啥 n为奇数 找到中间点 n为偶数 找到矩形区间 就像酱紫,就完了呢-. 然而 我想简单了-. 还有几步没有想到 1. 中间的那个点被牛占了怎么办 2. 矩形区…
最近在看一些中位数的东西,然后顺便也看了些题目.poj 1723不仅要求到水平位置的最短距离和,还要求水平都相邻的排成一排的最短距离和,即士兵都站成一列. 到y轴的距离好办,按y轴坐标排序,求中位数,然后求所有到中位数的距离和. 但是在x上怎么样才能最短呢?百思不得其解啊,最后看了这篇之后,豁然开朗. x轴方向,先把x[]排好序,要想移动的距离最短,那么这时的相对位置肯定不变.那么假设a是这个队列的最左边的x坐标,那么它们的关系就有就有 x[0] -> a x[1]  -> a + 1 x[2…
题意:对列数X计算∣Xi – Xj∣组成新数列的中位数. 思路:双重二分搜索 对x排序 如果某数大于 mid+xi 说明在mid后面,这些数的个数小于 n/2 的话说明这个中位数 mid 太大 反之太小 对x 搜索 ,直接用lower_bound实现 解决问题的代码: #include <iostream> #include <stdio.h> #include <algorithm> #include <math.h> using namespace st…
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int maxn=1e4+9; int x[maxn],y[maxn]; struct D { int x,y; bool operator <(const D &xx)const { if(…
题目链接:http://poj.org/problem?id=2388 题目大意: 奇数个数排序求中位数 解题思路:看代码吧! AC Code: #include<stdio.h> #include<algorithm> using namespace std; int main() { int n; while(scanf("%d",&n)!=EOF) { ]; ; i<n; i++) scanf("%d",&na[i…
题目大意: 平面上有N(N<=10000)个点,求这些点变成一条水平线的最小移动步数. 算法讨论: 表示自己太弱弱了,打算从今天开始提高一下智商. 我们考虑,既然是要成一条水平线,那么这条直线的y坐标肯定是所有y的中位数了.这是不用置疑的.所以我们只要求出中位数,然后对y坐标的距离差求下和就可以了. 对于x坐标,我们这样考虑,既然题目要求是最小步数,那么也就是说,这些博士兵在水平位置上的相对位置不变,换个意思说就是 原来三个士兵的x坐标是 -1 5 6,那么在他们移动之后,假设移动成一条直线之后…
题目 http://poj.org/problem?id=1723 题解 带权中位数类型的题目~ 可以先考虑降维,最后集合的y坐标,明显是y坐标的中位数的位置,容易求出y方向的贡献res_y.比较麻烦的是在x坐标上最后是要列成一排,而不是单独的一个点,我们可以假设最后集合的最左边的点是x,采用贪心的策略列出公式:res_x=sum(abs(xi-x-i))(i belongs to [0,n-1]).令zi=xi-i,就转化为res_x=sum(abs(zi-x)),这相当于求zi数列的中位数(…
Description For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (middle value) of the elements received so far. Input The first line of input contains…
题目链接:http://poj.org/problem?id=3784 题目大意:依次输入n个数,每当输入奇数个数的时候,求出当前序列的中位数(排好序的中位数). 此题可用各种方法求解. 排序二叉树方法,每个结点保存以其为根的左右子树中数的个数.如果数据出的够严格,这种方法会被卡的,除非是通过动态调整维持树的高度较小. 排序二叉树的代码如下: #include <cstdio> using namespace std; #define N 20000 struct Node { int v;…
Description For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After each odd-indexed value is read, output the median (middle value) of the elements received so far. Input The first line of input contains…