2023.1.2

D - Hossam and Combinatorics

题目描述

Hossam woke up bored, so he decided to create an interesting array with his friend Hazem.

Now, they have an array aa of nn positive integers, Hossam will choose a number a_iai and Hazem will choose a number a_jaj .

Count the number of interesting pairs (a_i, a_j)(ai,aj) that meet all the following conditions:

  • 1 \le i, j \le n1≤i,jn ;

  • i \neq ji=j ;

  • The absolute difference |a_i - a_j|∣aiaj∣ must be equal to the maximum absolute difference over all the pairs in the array. More formally, |a_i - a_j| = \max_{1 \le p, q \le n} |a_p - a_q|∣aiaj∣=max1≤p,qnapaq∣ .

输入格式

The input consists of multiple test cases. The first line contains a single integer tt ( 1 \le t \le 1001≤t≤100 ), which denotes the number of test cases. Description of the test cases follows.

The first line of each test case contains an integer nn ( 2 \le n \le 10^52≤n≤105 ).

The second line of each test case contains nn integers a_1, a_2, \dots, a_na1,a2,…,an ( 1 \le a_i \le 10^51≤ai≤105 ).

It is guaranteed that the sum of nn over all test cases does not exceed 10^5105 .

输出格式

For each test case print an integer — the number of interesting pairs (a_i, a_j)(ai,aj) .

题意翻译

给定一个长度为 nn 的序列,求满足以下条件的数对 (i,j)(i,j) 的数量:

  • 1\le i,j\le n1≤i,jn

  • i\ne ji=j

  • |a_i-a_j|=\max_{1\le p,q\le n}|a_p-a_q|∣aiaj∣=max1≤p,qnapaq

输入输出样例

输入

2

5

6 2 3 8 1

6

7 2 8 3 2 10

输出

2

4

说明/提示

In the first example, the two ways are:

  • Hossam chooses the fourth number 88 and Hazem chooses the fifth number 11 .

  • Hossam chooses the fifth number 11 and Hazem chooses the fourth number 88 .

In the second example, the four ways are:

  • Hossam chooses the second number 22 and Hazem chooses the sixth number 1010 .

  • Hossam chooses the sixth number 1010 and Hazem chooses the second number 22 .

  • Hossam chooses the fifth number 22 and Hazem chooses the sixth number 1010 .

  • Hossam chooses the sixth number 1010 and Hazem chooses the fifth number 22 .

  • 思路

  • 找出最大值出现次数和最小值出现次数,不等为cntx*cntn*2,因为是绝对值i和j不同,所以乘以2,

  • 相等则cntx*(cntx-1),i!=j。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        int n;
        scanf("%d", &n);
        int min = 100005, max = 0, cntn = 0, cntx = 0;//最大,最小值出现次数 
        for (int i = 1; i <= n; i++)
        {
            int x;
            scanf("%d", &x);
            if (x > max)
            {
                max = x;
                cntx = 1;
            }
            else if (x == max) cntx++;
            if (x < min)
            {
                min = x;
                cntn = 1;
            }
            else if (x == min) cntn++;
        }
        if (max != min) printf("%lld\n", (long long) cntn * cntx * 2);
        else printf("%lld\n",(long long )cntn * (cntn - 1));
    }
    return 0;
}

P2392 kkksc03考前临时抱佛脚

题目背景

kkksc03 的大学生活非常的颓废,平时根本不学习。但是,临近期末考试,他必须要开始抱佛脚,以求不挂科。

题目描述

这次期末考试,kkksc03 需要考 44 科。因此要开始刷习题集,每科都有一个习题集,分别有 s_1,s_2,s_3,s_4s1,s2,s3,s4 道题目,完成每道题目需要一些时间,可能不等(A_1,A_2,\ldots,A_{s_1}A1,A2,…,As1,B_1,B_2,\ldots,B_{s_2}B1,B2,…,Bs2,C_1,C_2,\ldots,C_{s_3}C1,C2,…,Cs3,D_1,D_2,\ldots,D_{s_4}D1,D2,…,Ds4)。

kkksc03 有一个能力,他的左右两个大脑可以同时计算 22 道不同的题目,但是仅限于同一科。因此,kkksc03 必须一科一科的复习。

由于 kkksc03 还急着去处理洛谷的 bug,因此他希望尽快把事情做完,所以他希望知道能够完成复习的最短时间。

输入格式

本题包含 55 行数据:第 11 行,为四个正整数 s_1,s_2,s_3,s_4s1,s2,s3,s4。

第 22 行,为 A_1,A_2,\ldots,A_{s_1}A1,A2,…,As1 共 s_1s1 个数,表示第一科习题集每道题目所消耗的时间。

第 33 行,为 B_1,B_2,\ldots,B_{s_2}B1,B2,…,Bs2 共 s_2s2 个数。

第 44 行,为 C_1,C_2,\ldots,C_{s_3}C1,C2,…,Cs3 共 s_3s3 个数。

第 55 行,为 D_1,D_2,\ldots,D_{s_4}D1,D2,…,Ds4 共 s_4s4 个数,意思均同上。

输出格式

输出一行,为复习完毕最短时间。

输入输出样例

输入

1 2 1 3

5

4 3

6

2 4 3

输出

20

说明/提示

1\leq s_1,s_2,s_3,s_4\leq 201≤s1,s2,s3,s4≤20。

1\leq A_1,A_2,\ldots,A_{s_1},B_1,B_2,\ldots,B_{s_2},C_1,C_2,\ldots,C_{s_3},D_1,D_2,\ldots,D_{s_4}\leq601≤A1,A2,…,As1,B1,B2,…,Bs2,C1,C2,…,Cs3,D1,D2,…,Ds4≤60。

思路

先对一个题组分析,找出左右脑时间最短时间,记录长的,统计和。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int s[4],t[100][100],min,minn=1000000000,ans=0,l,r;
void fun(int p,int n)
{
    if(p>s[n])
    {
        if(l>r) min=l;
        else min=r;
        if(minn>min) minn=min;
        return;
    }
    l+=t[n][p];
    fun(p+1,n);
    l-=t[n][p];
    r+=t[n][p];
    fun(p+1,n);
    r-=t[n][p];
}
int main()
{
    for(int i=1;i<=4;i++)
    scanf("%d",&s[i]);
    for(int i=1;i<=4;i++)
    {
        for(int j=1;j<=s[i];j++)
        scanf("%d",&t[i][j]);
        l=0;
        r=0;
        fun(1,i);
        ans+=minn;
        minn=1000000000;
    }
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值