uva 10057 A mid-summer night’s dream 夏天晚上的梦
2015-08-06 23:20
513 查看
原题:
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 peoples 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
that 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
题目大意:
前面几段都是废话,就是给你n个数,让你找出3个值来。这三个值分别是在给出的这n个数里面找到一个数A,让上面的那个公式(差的绝对值的和最小),第二个值是让你找在给出的n个数里面有多少个数都是同样能求的差的绝对值的和最小,第三个数是在整个数轴上有多少个数能满足差的绝对值的和最小(不重复)
真绕口~~
思路见代码下方
思路:
刚开始看的时候往往平均值上蒙,找出平均值然后找了离平均值最近的那个数。但是自己写了一组数据后返现这个思路是错误的。后来自己写了几个数据测试发现是找出中位数即可,如果是奇数那么答案1就是中间的那个数,答案2就是中间的那个数的个数,答案3就是1.
如果是偶数,答案1就是中间偏左的那个数,答案2就是2,答案3就是中间偏右和中间偏左的数的差。
(好吧,我是看数据蒙的。自己写出几个奇数和偶数的数据在纸上算算就能找出答案)
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 peoples 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
that 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
题目大意:
前面几段都是废话,就是给你n个数,让你找出3个值来。这三个值分别是在给出的这n个数里面找到一个数A,让上面的那个公式(差的绝对值的和最小),第二个值是让你找在给出的n个数里面有多少个数都是同样能求的差的绝对值的和最小,第三个数是在整个数轴上有多少个数能满足差的绝对值的和最小(不重复)
真绕口~~
思路见代码下方
[code]#include<iostream> #include<algorithm> #include<cstdio> #include<vector> #include<cstring> #include<string> #include<cmath> #include <iomanip> #include<queue> #include<cstring> #include<sstream> #include<map> using namespace std; const int N=1000001; int a ; int num[65536]; int main() { ios::sync_with_stdio(false); int n,mid,tem,sum,ans; while(cin>>n) { memset(num,0,sizeof(num)); ans=sum=0; for(int i=1;i<=n;i++) { cin>>a[i]; num[a[i]]++; sum+=a[i]; } sort(a+1,a+1+n); if(n%2) { mid=(1+n)/2; cout<<a[mid]<<" "<<num[a[mid]]<<" "<<1<<endl; continue; } else { mid=n/2; if(a[mid]==a[mid+1]) cout<<a[mid]<<" "<<num[a[mid]]<<" "<<1<<endl; else cout<<a[mid]<<" "<<2<<" "<<a[mid+1]-a[mid]+1<<endl; } } return 0; }
思路:
刚开始看的时候往往平均值上蒙,找出平均值然后找了离平均值最近的那个数。但是自己写了一组数据后返现这个思路是错误的。后来自己写了几个数据测试发现是找出中位数即可,如果是奇数那么答案1就是中间的那个数,答案2就是中间的那个数的个数,答案3就是1.
如果是偶数,答案1就是中间偏左的那个数,答案2就是2,答案3就是中间偏右和中间偏左的数的差。
(好吧,我是看数据蒙的。自己写出几个奇数和偶数的数据在纸上算算就能找出答案)
相关文章推荐
- burp suite使用(一) --- 抓包,截包,改包
- Hash索引和BTree索引
- mybatis中的变量#与$
- 关于JAVA Entity entity = entityClass.getAnnotation(Entity.class);
- 打造一款属于自己的Android滑动器
- A1077 Kuchiguse (20)
- ksoap2连接 web service
- Codeforces Round #313 Gerald is into Art (枚举)
- Scala 深入浅出实战经典 第1讲 Scala开发环境搭建和HelloWorld解析
- 1.Linux下libevent和memcached安装
- 1.Linux下libevent和memcached安装
- CF_37A_Towers
- Android:Toast 弹出信息框
- 实现一边打包一边删除文件
- org-reveal 特效
- 表单前端验证+ajax异步请求
- 1671 Walking Ant【bfs】
- vim 插件 syntastic 头文件找不到
- CF_525C_IlyaAndSticks
- 关于就业指导和职业规划