题目链接:http://codeforces.com/problemset/problem/459/B
题目意思:有 n 朵 flowers,每朵flower有相应的 beauty,求出最大的beauty 差 和 要达到这个最大的差 的取法有多少种。
一下子wa,是因为没考虑到整个序列都是相同的beauty 时的情况,以为取法是一种= =。注意,beauty 差为0都是合法的。还有注意这句话: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。 就是说,两种flower 只要有一种(当然两种也可以)和之前的取法不相同,就是一个新的取法。
傻了,相同beauty > 2 时,答案应该是 n * (n-1) / 2!!!竟然写成 n * (n-1)了,这样会有重复取法数啦!!!!排列组合都还给老师了- -
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 8 typedef long long LL; 9 const int maxn = 2e5 + 5; 10 int b[maxn]; 11 12 int main() 13 { 14 int n; 15 while (scanf("%d", &n) != EOF) 16 { 17 for (int i = 0; i < n; i++) 18 scanf("%d", &b[i]); 19 sort(b, b+n); 20 int minn = b[0]; 21 int maxx = b[n-1]; 22 if (minn == maxx) 23 { 24 if (n == 2) 25 printf("0 1 "); 26 else 27 printf("0 %lld ", (LL)n * (LL)(n-1) / 2); 28 } 29 else 30 { 31 int cnt1 = 1; 32 int cnt2 = 1; 33 for (int i = 1; i < n; i++) 34 { 35 if (minn == b[i]) 36 cnt1++; 37 else 38 break; 39 } 40 for (int i = n-2; i >= 0; i--) 41 { 42 if (maxx == b[i]) 43 cnt2++; 44 else 45 break; 46 } 47 printf("%d %lld ", maxx-minn, (LL)cnt1*cnt2); 48 } 49 } 50 return 0; 51 }