zoukankan      html  css  js  c++  java
  • [UCF HSPT 2021] Sharon’s Sausages | 思维 暴力

    Description

    Sharon is a food-loving individual, and recently he has discovered that he loves the taste of different types of sausages!

    When he was invited to the HSPT potluck, he decided that he would contribute 4 sausages for the cause in order to spread his love for sausages. Sharon also loves symmetry, so he chose the 4 sausages in such a way that the first and fourth sausages have the same length, and the second and third sausages have the same length.

    While on his way to the potluck, Sharon came across a sausage field, which consisted of many sausages lined up in a row (how glorious!). To his horror, though, he dropped his 4 sausages into the field and now they’re lost! Luckily he explained the situation to the owner of the sausage field and he has allowed Sharon to choose 4 sausages from the field. Sharon doesn’t remember the lengths of the original sausages that he chose, but he remembers that he dropped the first sausage to the left of the second, the second to the left of the third, and the third to the left of the fourth. Sharon now wants to know how many candidates there are for choosing 4 sausages from the field such that his conditions are met. Can you help him?

    Given n sausage lengths, output the number of ways to choose 4 of them with positions a,b,c,d such that 1 ≤ a < b < c < d ≤ n 1≤a<b<c<d≤n 1a<b<c<dn, while also making sure that l e n g t h a = l e n g t h d lengtha=lengthd lengtha=lengthd and l e n g t h b = l e n g t h c lengthb=lengthc lengthb=lengthc.

    Input

    The first line of the input contains a single, positive integer, p, representing the number of potlucks in which Sharon is invited. Each potluck is then defined on two lines. The first line of each potluck contains a single integer, n ( 4 ≤ n ≤ 1 0 5 4≤n≤10^5 4n105 ), representing the number of sausages in the sausage field. The next line contains n space-separated integers, lengthi ( 1 ≤ l e n g t h i ≤ 100 1≤length_i≤100 1lengthi100), representing the length of the i th sausage, respectively.

    Output
    Output the number of ways to select 4 sausages that meet Sharon’s requirement.

    Samples
    Input Copy
    2
    8
    2 1 3 4 2 1 2 3
    8
    1 3 3 7 1 3 3 7
    Output
    2
    3

    题意:
    给出一个长度为 n n n的序列,问有多少种方案数使得挑选四个位置满足ABBA这种情况
    1 ≤ a < b < c < d ≤ n 1 leq a < b < c < d leq n 1a<b<c<dn,且满足
    n u m [ a ] = n u m [ d ]    n u m [ b ] = n u m [ c ] num[a] = num[d] num[b] = num[c] num[a]=num[d]  num[b]=num[c]
    n u m [ a ] ≠ n u m [ b ] num[a] eq num[b] num[a]=num[b]

    对于ABBA这种,因为不能选同一个位置,我们暂且把他们看作是不同的,即A1 B1 B2 A2
    然后我们暴力枚举B2
    然后对于每一个A 属于 [1,100],此时的贡献都应该 + 前面<A1,B2> 的个数 * A2的个数,(A1 = A2),这里的贡献通过乘法原理可以得出
    为了算出这个贡献,我们要开三个数组
    preCnt[x] -> 用来记录前面有多少个x
    bakCnt[x] -> 用来记录后面有多少个x
    pairCnt[a][b] -> 用来记录前面有多少对<a,b>

    具体细节
    ① 因为我们是从前向后遍历数组元素,所以说要提前预处理出bakCnt[],在输入的时候顺便记录一下即可
    ② 因为bakCnt[]记录的是后面的个数,所以不包括当前位置,所以在遍历到这个数的时候,bakCnt[a[i]] - 1
    ③ 在计算完贡献之后,一定要加上此时这个位置 i i ipairCnt[][] 的贡献
    ④ 最后对preCnt[a[i]] + 1 意思是,遍历完这个元素之后,对于后面的每一个位置,这个数的个数都增加了1

    时间复杂 sum(n) * 100
    ac_code:

    #define Clear(x,val) memset(x,val,sizeof x)
    int n;
    int a[maxn];
    int preCnt[101];
    int bakCnt[101];
    ll pairCnt[101][101];
    int main() {
        int _ = read;
        while(_ --) {
            Clear(preCnt, 0);
            Clear(bakCnt, 0);
            Clear(pairCnt, 0);
            n = read;
            for(int i = 1; i <= n; i++) {
                a[i] = read;
                bakCnt[a[i]] ++;
            }
            ll ans = 0LL;
            for(int i = 1; i <= n; i++) { /// A1 B1 B2 A2  -> B2
                int B2 = a[i];
                bakCnt[B2] --;
                for(int A1 = 1; A1 <= 100; A1 ++) {
                    int A2 = A1;
                    ans += 1LL * pairCnt[A1][B2] * 1LL * bakCnt[A2] * 1LL;///
                }
                for(int A1 = 1; A1 <= 100; A1 ++) {
                    pairCnt[A1][B2] += preCnt[A1];
                }
                preCnt[B2] ++;
            }
            printf("%lld
    ", ans);
        }
        return 0;
    }
    /**
    2
    8
    2 1 3 4 2 1 2 3
    8
    1 3 3 7 1 3 3 7
    
    **/
    
  • 相关阅读:
    20200925--矩阵加法(奥赛一本通P93 6 多维数组)
    20200924--图像相似度(奥赛一本通P92 5多维数组)
    20200923--计算鞍点(奥赛一本通P91 4)
    20200922--计算矩阵边缘元素之和(奥赛一本通P91 3二维数组)
    20200921--同行列对角线的格(奥赛一本通P89 2 二维数组)
    磨人的.net core 3.1(二) DataReader的问题
    磨人的.net core 3.1(一) CORS的问题
    Vue SSR问题:返回的js打包文件为HTML文件
    axios与.net core API实现文件下载
    .Net Core API中基于System.Threading.Timer的定时任务
  • 原文地址:https://www.cnblogs.com/PushyTao/p/15459791.html
Copyright © 2011-2022 走看看