链接:
https://codeforces.com/problemset/problem/459/B
题意:
在数组中找出差值最大,看看有多少对能组成这个值
解:
找出最大值最小值,记录数量(遍历一遍即可)
最大值=最小值时,说明全数组只有一个数,结果为C2 n(n为数组大小)=n×(n-1)/2
实际代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<limits.h>
#define csh(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long int ll;
const int N=2E5+10;
int sz[N];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>sz[i];
}
ll Min=INT_MAX;
ll summin=0;
ll summax=0;
ll Max=INT_MIN;
for(int i=1;i<=n;i++)
{
if(sz[i]<Min)
{
Min=sz[i];
summin=1;
}
else if(sz[i]==Min)
{
summin++;
}
if(sz[i]>Max)
{
Max=sz[i];
summax=1;
}
else if(sz[i]==Max)
{
summax++;
}
}
//cout<<summax<<" "<<summax<<endl;
if(Min!=Max)
{
cout<<Max-Min<<" "<<summin*summax<<endl;
}
else
{
cout<<Max-Min<<" "<<(summin*(summin-1))/2<<endl;
}
}
限制:
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output