文章标题 SPOJ INUM : Interesting Numbers (细节)
来源:互联网 发布:丢火车知乎 编辑:程序博客网 时间:2024/05/21 17:47
Interesting Numbers
Jack and Jill went up the hill. Jack proposed Jill after reaching at the top of the hill. Jill gave Jack ‘N’ numbers and asked him to choose a pair of numbers such that their absolute difference is minimum. She also asked him to choose a pair of numbers such that their absolute difference is maximum. Jill wondered just finding maximum and minimum values is mainstream and it will be a cakewalk for Jack, instead she asked him to find number of pairs which have minimum absolute difference and number of pairs which have maximum absolute difference.
Jill will accept Jack’s proposal only if he can answer her question correctly. Jack does not know programming. Fortunately Jack had his laptop with him, fully charged and with good internet connection. He mailed you the numbers and asked you to mail him the answers back, as you are known to be a good programmer. Now it’s your turn to help him.
Input Format
First line contains a positive integer ‘N’.
Next line contains ‘N’ non-negative integers ‘a[i]’ separated by a single space.
Output Format
Print the number of pairs having minimum and maximum difference separated by a single space.
Constraints
1 ≤ N ≤ 105
0 ≤ a[i] ≤ 1015
Sample Input
5
5 12 8 1 35
Sample Output
1 1
Explanation
It’s optimal to choose the pair (5, 8) for the minimum difference.So only 1 pair.
It’s optimal to choose the pair (1,35) for the maximum difference. Here also only 1 pair.
题意:给你n个数a[n],然后任意找出其中的两个数a[i],a[j],使得其绝对值|a[i]-a[j]|最小和最大,求出绝对值最小和最大的对数。
分析:首先,将n个数排序,然后去重,把重复的数字的次数c[i]记下来,然后有以下几种情况
1、n=1,则maxn和minx都为0
2、n=2,则maxn和minx都为1
3、n>=3时,maxn的值都是c[0]*c[cnt] (cnt为这n个数中不同数字的数目)
而当cnt=n时,说明得通过相邻的两个数相减获得
当cnt
#include<iostream>#include<string>#include<cstdio>#include<cstring>#include<vector>#include<math.h>#include<map>#include<queue> #include<algorithm>using namespace std;const int inf = 0x3f3f3f3f;long long a[100005];int n;long long num[100005];long long c[100005];int main (){ while (scanf ("%d",&n)!=EOF){ int cnt=0; memset (c,0,sizeof (c)); long long minx=0; long long maxn=0; for (int i=0;i<n;i++){ scanf ("%lld",&a[i]); } if (n==1){//n为1 时 printf ("0 0\n"); continue; } sort(a,a+n); num[cnt]=a[0]; c[cnt]++; long long tmp=a[0]; for (int i=1;i<n;i++){ if (a[i]!=tmp){ cnt++; num[cnt]=a[i]; tmp=a[i]; } c[cnt]++; } if (cnt==0){//只有一个数字出现时 printf ("%lld %lld\n",c[0]*(c[0]-1)/2,c[0]*(c[0]-1)/2); continue; } maxn=c[0]*c[cnt];//不管如何,都是头尾数字次数之积 if (cnt+1<n){//有重复的数字出现 for (int i=0;i<=cnt;i++){ minx+=(c[i]*(c[i]-1)/2); } } else if (cnt+1==n){//没有重复的数字出现 long long tmp=a[1]-a[0]; minx=1; for (int i=2;i<n;i++){ if (a[i]-a[i-1]==tmp){ minx++; } else if (a[i]-a[i-1]<tmp){ minx=1; tmp=a[i]-a[i-1]; } } } printf ("%lld %lld\n",minx,maxn); } return 0;}
- 文章标题 SPOJ INUM : Interesting Numbers (细节)
- 文章标题 SPOJ
- 文章标题 SPOJ-COT
- 文章标题 codeforces 706B :Interesting drink( 水)
- SPOJ INUMInteresting Numbers 简单细节题
- 文章标题 SPOJ PHT : Pigeonhole Tower(二分)
- SPOJ PHT【二分】+SPOJ INUM【最小/大值重复】
- 文章标题 HDU 5272:Dylans loves numbers( 水)
- URAL 2070 Interesting Numbers(数学)
- URAL 2070 Interesting Numbers (素数枚举)
- URAL 2070 Interesting Numbers(数论)
- ural 2070 Interesting Numbers(数论基础)
- 蓝桥杯-Interesting Numbers 加强版(java)
- HDU 3195 Interesting Numbers
- ural 2070 - Interesting Numbers
- UVA2070-Interesting Numbers
- ural2070-Interesting Numbers
- URAL 2070 Interesting Numbers
- 点击UITableView的cell展开收缩
- PictureBOX图片保存,添加文字
- 复制文件的操作属于“r-w-x”中的哪个权限操作?
- 关于软件开发,测试什么时候介入项目
- opnet调试之常用指令
- 文章标题 SPOJ INUM : Interesting Numbers (细节)
- 控制反转(Inversion of Control,IoC)简介
- 单例中引用Context导致内存泄漏的问题
- 李诗雨的2016
- 最小堆的实现--topk算法
- DLL Hell(DLL地狱)浅谈
- 关于RecyclerView的一切
- iOS 调取iPhone本地相册和开启相机
- [SPOJ IITWPC4F Gopu and the Grid Problem]线段树区间翻转