题目链接:http://codeforces.com/problemset/problem/459/B
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 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:
代码如下:
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <cstdlib> #include <climits> #include <ctype.h> #include <queue> #include <stack> #include <vector> #include <utility> #include <deque> #include <set> #include <map> #include <iostream> #include <algorithm> using namespace std; const double eps = 1e-9; #define INF 1e18 //typedef long long LL; typedef __int64 LL; #define MAXN 200017 int main() { LL n; LL a[MAXN], b[MAXN]; while(~scanf("%I64d",&n)) { LL i; LL maxx = 0, minn = 1000000000; for(i = 0; i < n; i++) { scanf("%I64d",&a[i]); if(a[i] > maxx) maxx = a[i]; if(a[i] < minn) minn = a[i]; } LL cont1 = 0, cont2 = 0; if(maxx == minn) { printf("0 %I64d\n",n*(n-1)/2); continue; } for(i = 0; i < n; i++) { if(a[i] == maxx) cont1++; else if(a[i] == minn) cont2++; } LL d = maxx-minn; printf("%I64d %I64d\n",d,cont1*cont2); } return 0; }
Codeforces Round #261 (Div. 2) 459B. Pashmak and Flowers(数学题,组合),布布扣,bubuko.com
Codeforces Round #261 (Div. 2) 459B. Pashmak and Flowers(数学题,组合)
原文:http://blog.csdn.net/u012860063/article/details/38617151