文章标题 SPOJ INUM : Interesting Numbers (细节)

原创 2017年01月03日 23:58:58

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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

文章标题 codeforces 706B :Interesting drink( 水)

Interesting drinkDescriptionVasiliy likes to rest after a hard work, so you may often meet him in so...

文章标题 HDU 5272:Dylans loves numbers( 水)

Dylans loves numbersDescriptionWho is Dylans?You can find his ID in UOJ and Codeforces. His anothe...

[SPOJ IGAME Interesting Game]Nim 博弈+数位DP

[SPOJ IGAME Interesting Game]Nim 博弈+数位DP分类:数学 逆元 水题1. 题目链接[SPOJ IGAME Interesting Game]2. 题意描述Alice和...

Ural 2070 Interesting Numbers

DescriptionNikolay and Asya investigate integers together in their spare time. Nikolay thinks an int...

算法提高 Problem S4: Interesting Numbers 加强版 解题报告

Problem Description   We call a number interesting, if and only if:   1. Its digits consists of on...

URAL 2070 Interesting Numbers (素数枚举)

大体题意: 两个人,一个人认为一个数如果是素数的话,那么是符合要求的,另外一个人认为这个数的因子个数是素数的话,那么才是符合要求的! 给你一个区间 [L,R],求出该区间内 同时符合两个人要求 或...

蓝桥杯-Interesting Numbers 加强版(java)

算法提高 Problem S4: Interesting Numbers 加强版 时间限制:1.0s 内存限制:1.0GB ...

URAL 2070 Interesting Numbers

条件1:质数。条件2:不一定是质数,但因子个数(包括1和本身)是质数的数。求 [ L , R ] 内同时满足或同时不满足两个条件的数的个数。

SPOJ Balanced Numbers(数位dp,三进制状压)

题目链接SPOJ Balanced Numbers题意定义一个数如果满足所有位上偶数数字出现奇数次,奇数数字出现偶数次那么就称为Balanced Number。给一个区间[L,R][L,R],求Bal...

SPOJ10606---Balanced Numbers(三进制数位dp)

Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)