题目:
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
题意分析:
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 200000+5;
int a[MAXN];
map<int, __int64> p;
int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
p.clear();
int i;
int minf = INF, maxf = 0;
for(i = 0;i < n;i++)
{
scanf("%d", &a[i]);
minf = min(minf, a[i]);
maxf = max(maxf, a[i]);
p[a[i]]++;
}
int d = maxf-minf;
printf("%d ", d);
__int64 ans = 0;
if(d == 0)
{
__int64 t = p[minf]-1;
ans = (1+t)*t/2;
}
else
{
ans = p[minf]*p[maxf];
}
printf("%I64d\n",ans);
}
return 0;
}
解决B.Pashmak给Parmida挑选最美丽花朵的问题,计算最大美丽差异及选择方式数量。输入包含花园中花朵的数量和每朵花的美丽数值,输出最大美丽差异和可能的选择方式数。
&spm=1001.2101.3001.5002&articleId=38607511&d=1&t=3&u=8663c80e2ff6414d8f40ff1e780b41a6)
1296

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



