Codeforces Round #261 (Div. 2) B. Pashmak and Flowers

题目链接

题解:找出序列中的最小值和最大值之差即为第一个输出,遍历一遍序列,分别找出最小值和最大值出现的次数,相乘即为总共的方法数量。不过,要注意几点:

1. 如果二者出现次数相等,即序列中的值全相同,则方法数量为C(n, 2),即n*(n-1) / 2;

2. n应定义为long long 或者(long long)n*(n-1) / 2LL,因为没开long long所以重判的时候WA掉了,可惜了

//Pashmak and Flowers.cpp
#include <bits/stdc++.h>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
	freopen("D:\\Documents\\Programs\\Acm\\input.txt", "r", stdin);
#endif
	int n; cin >> n;
	int beauty[200005];
	for(int i = 0; i < n; i++) {
		scanf("%d", beauty + i);
	}
	sort(beauty, beauty + n);
	cout << beauty[n-1] - beauty[0] << ' ';
	LL cnt_low = 0, cnt_high = 0;
	for(int i = 0; i < n; i++) {
		if (beauty[i] == beauty[0]) cnt_low++;
		if (beauty[i] == beauty[n-1]) cnt_high++;
	}
	if (cnt_low == cnt_high && cnt_low == n) {
		cout << (LL)n*(n-1) / 2 << endl;
	}
	else cout << cnt_low * cnt_high << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值