Problem Link:点击打开链接
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!
Your task is to write a program which calculates two things:
- The maximum beauty difference of flowers that Pashmak can give to Parmida.
- The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way.
The first line of the input contains n (2 ≤ n ≤ 2·105). In the next line there are n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ 109).
The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively.
2 1 2
1 1
3 1 4 5
4 1
5 3 1 2 3 1
2 4
In the third sample the maximum beauty difference is 2 and there are 4 ways to do this:
- choosing the first and the second flowers;
- choosing the first and the fifth flowers;
- choosing the fourth and the second flowers;
- choosing the fourth and the fifth flowers.
code:
#include<iostream>
#include<algorithm>
using namespace std;
__int64 n,a[200002],i,j,d,m,n1,n2;
int main()
{
while(scanf("%I64d",&n)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%I64d",&a[i]);
sort(a+1,a+n+1);
d=a[n]-a[1];
if(d==0)
{
m=n*(n-1)/2;
printf("%I64d %I64d\n",d,m);
}
else
{
n1=1;
for(i=2;i<=n;i++)
{
if(a[i]==a[1])
n1++;
else
break;
}
n2=1;
for(i=n-1;i>=1;i--)
{
if(a[i]==a[n])
n2++;
else
break;
}
m=n1*n2;
printf("%I64d %I64d\n",d,m);
}
}
return 0;
}

本文深入探讨了算法和数据结构的关键优化策略,包括排序算法、动态规划、哈希算法等,旨在提升程序效率和解决复杂问题。通过实例分析,展示了如何在实际应用中灵活运用这些技术,以实现高效的数据处理和解决方案。
830

被折叠的 条评论
为什么被折叠?



