459B - Pashmak and Flowers

该博客介绍了一道编程题目,要求在给定数组中找出差值最大的两个数并计算这样的数对有多少。解决方案包括找出数组的最大值和最小值,然后根据最大值和最小值的关系计算组合数。代码实现中涉及到了遍历、比较和计数操作,最后根据最大值和最小值是否相等来输出结果。
摘要由CSDN通过智能技术生成

链接:

https://codeforces.com/problemset/problem/459/B

题意:

在数组中找出差值最大,看看有多少对能组成这个值

解:

找出最大值最小值,记录数量(遍历一遍即可)

最大值=最小值时,说明全数组只有一个数,结果为C2 n(n为数组大小)=n×(n-1)/2

实际代码:

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<limits.h>
#define csh(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long int ll;
const int N=2E5+10;
int sz[N];
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>sz[i];
	}
	ll Min=INT_MAX;
	ll summin=0;
	ll summax=0;
	ll Max=INT_MIN;
	for(int i=1;i<=n;i++)
	{
		if(sz[i]<Min)
		{
			Min=sz[i];
			summin=1;
		}
		else if(sz[i]==Min)
		{
			summin++;
		}
		if(sz[i]>Max)
		{
			Max=sz[i];
			summax=1;
		}
		else if(sz[i]==Max)
		{
			summax++;
		}
	}
	//cout<<summax<<" "<<summax<<endl;
	if(Min!=Max)
	{
		cout<<Max-Min<<" "<<summin*summax<<endl;
	}
	else
	{
		cout<<Max-Min<<" "<<(summin*(summin-1))/2<<endl;
	}
}

限制:

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值