UVA - 10057 A mid-summer night's dream.

偶数时,中位数之间的数都是可以的(包括中位数)

奇数时,一定是中位数

推导请找初中老师

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int box[1000000];
int main()
{
    //freopen("in","r",stdin);
    //freopen("out","w",stdout);
    int i,n,a,b,c;
    int num[65546];
    while(cin>>n)
    {
        memset(num,0,sizeof(num));
        for(i=0;i<n;i++)
        {
            cin>>box[i];
            num[box[i]]++;
        }
        sort(box,box+n);
        if(n%2||box[n/2-1]==box[n/2])
        {
            i=n/2;
            a=box[i];
            b=num[a];
            c=1;
        }
        else
        {
            i=n/2-1;
            a=box[i];
            b=num[box[i]]+num[box[i+1]];
            c=box[i+1]-box[i]+1;
        }
        cout<<a<<" "<<b<<" "<<c<<endl;
    }
    return 0;
}

UVA - 10057

A mid-summer night‘s dream.

Crawling failed

Description

Problem C

A mid-summer night’s dream

Input: standard input

Output: standard output

This is year 2200AD. Science has progressed a lot in two hundred years. Two hundred years is mentioned here because this problem is being sent back to 2000AD with the help of time machine. Now it is possible to establish direct connection between man and
computer CPU. People can watch other people’s dream on 3D displayer (That is the monitor today) as if they were watching a movie. One problem in this century is that people have become so dependent on computers that their analytical ability is approaching
zero. Computers can now read problems and solve them automatically. But they can solve only difficult problems. There are no easy problems now. Our chief scientist is in great trouble as he has forgotten the number of his combination lock. For security reasons
computers today cannot solve combination lock related problems. In a mid-summer night the scientist has a dream where he sees a lot of unsigned integer numbers flying around. He records them with the help of his computer, Then he has a clue that if the numbers
are (X1, X2,   …  , Xn) he will have to find an integer number A (This
A is the combination lock code) such that

(|X1-A| + |X2-A| + … … + |Xn-A|) is minimum.

Input

Input will contain several blocks. Each block will start with a number n (0<n<=1000000) indicating how many numbers he saw in the dream. Next there will be n numbers. All the numbers will be less than 65536. The input will be terminated by end of file.

Output

For each set of input there will be one line of output. That line will contain the minimum possible value for A. Next it will contain how many numbers are there in the input that satisfy the property of A (The summation of absolute deviation from A is minimum).
And finally you have to print how many possible different integer values are there for A (these values need not be present in the input). These numbers will be separated by single space.

 

Sample Input:

2

10

10

4

1

2

2

4

Sample Output:

10 2 1

2 2 1

______________________________________________________________________________________________
Shahriar Manzoor

16-12-2000

Source

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 4. Algorithm Design

Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Data Structures and Libraries :: Linear Data Structures with Built-in Libraries ::
C++ STL algorithm (Java Collections)

UVA - 10057 A mid-summer night's dream.

时间: 2024-09-28 17:06:17

UVA - 10057 A mid-summer night's dream.的相关文章

uva 10057 A mid-summer night’s dream(中位数)

uva 10057 A mid-summer night's dream This is year 2200AD. Science has progressed a lot in two hundred years. Two hundred years is mentioned here because this problem is being sent back to 2000AD with the help of time machine. Now it is possible to es

UVa 10969 (圆与圆之间的覆盖问题) Sweet Dream

题意: 有n个按先后顺序放置的不同大小不同位置的圆,求所有可见圆弧的长度. 分析: 这道题应该是大白书上例题 LA 2572 (求可见圆盘的数量) Kanazawa 的加强版,整体框架都差不多. 对于每个圆求出与其他圆相交的交点所对应的幅角(转化到[0, 2π]中),排个序,然后枚举每段弧的终点,如果不被后面放置的圆所覆盖则可见. 注意: 原本以为WA是精度问题,后来调大调小都一直WA,这里精度eps从1e-11到1e-13都没问题. 但是在判断弧的终点是否被圆所覆盖的时候要加上等号.也就是第6

UVA - 10969 Sweet Dream

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34782 Problem 从楼上往下丢N个圆,告诉圆的半径和落点,求最终图形的可见弧长之和.N<=100 Solution 实际上可以转换为圆求交.逆着来,i : n->1,j : i+1->n,求出 i 与 j 的交点(同时考虑无交点和完全覆盖的情况),把交点按极角排序,然后就变成了区间覆盖问题了,给若干个区间,统计未被覆盖的长度(在此题实际上是角度)之和,然后就知

POJ2411(Mondriaan&#39;s Dream)

题目链接:传送门 题目大意:用1*2大小的砖块去铺满n*m大小的地面,有多少种方案 题目思路:因为1<=n,m<=11,并且砖块是1*2,故可以用二进制思想,也就是状态压缩DP,其中矩阵中为0的元素表示当前位置竖着放一块砖,而连着 两个1表示横着放一块砖(状态压缩真的很奇妙) #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <

uva 507 Jill Rides Again (分治)

uva 507 Jill Rides Again Jill likes to ride her bicycle, but since the pretty city of Greenhills where she lives has grown, Jill often uses the excellent public bus system for part of her journey. She has a folding bicycle which she carries with her

Uva 10815-Andy&#39;s First Dictionary(串)

Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all

uva 10026 Problem C: Edit Step Ladders

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=970 通过对每一个字符串,每一个位置进行枚举三个操作,然后二分查找操作后的字符串是否存在,dp记录. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define N 25

uva 11107 - Life Forms(后缀数组)

题目链接:uva 11107 - Life Forms 题目大意:给定n个字符串,求一个最长的字符串,为n/2个字符串的子串. 解题思路:后缀数组,处理除后缀数组后,二分长度,每次遍历height数组,当长度不足时就分段,如果存在一段中包含n/2个起点,则为可行长度. #include <cstdio> #include <cstring> #include <set> #include <algorithm> using namespace std; co

uva 12206 - Stammering Aliens(哈希)

题目链接:uva 12206 - Stammering Aliens 题目大意:给出一个字符串,找出至少出现m次的最长子串. 解题思路:哈希算法,将每个后缀数组建立一个哈希值,每次二分长度判断,每次判断时将哈希值排序,计数即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef unsigned long long ll; const int ma