Educational Codeforces Round 161 (Rated for Div. 2) -- B -- Forming Triangles --- 题解

目录

B -- Forming Triangles

        题目大意:

思路解析:

 代码实现:


B -- Forming Triangles

        题目大意:

                

思路解析:

         这是一道比较简单的思维题,但是我比赛的时候犯了一个巨大的失误,ai代表的不是长度,2^ai才代表木棍的长度,比赛时没有看到这一点,所以这一题gg了。

        通过上面2^ai代表长度,我们可以知道,当ai,aj,ak各不相同,并且ai < aj <ak时, ai+aj一定小于ak,此时无法构成三角形,由此可知,只能构成等边三角形或者等腰三角形。(这很重要)

        所以考虑哪些棍子数量大于等于2,此时可以构成等腰三角形,为了不造成重复,第三条边选择小于腰长的边。

        考虑哪些棍子大于等于3,此时可以构成等边三角形。

        为了不重复,一定要考虑排列的情况。(因为排列组合的情况会导致算重)

 代码实现:

        

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        int t = input.nextInt();
        for (int i = 0; i < t; i++) {
            int n = input.nextInt();
            int[] arr = new int[n];
            int[] sum = new int[n + 1];
            int[] s = new int[n + 1];
            for (int j = 0; j < n; j++) {
                arr[j] = input.nextInt();
                sum[arr[j]]++;
            }
            s[0] = sum[0];
            for (int j = 1; j <= n; j++) {
                s[j] = s[j - 1] + sum[j];
            }
            long res = 0;
            for (int j = 1; j <= n; j++) {
                s[j] -= sum[j];
                if (sum[j] >= 3){
                    res += (long) sum[j] * (sum[j] - 1) * (sum[j] - 2) / 6;
                }
                if (sum[j] >= 2){
                    res += (long) sum[j] * (sum[j] - 1) * s[j] / 2;
                }
            }
            System.out.println(res);
        }
        out.close();
    }


    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static Input input = new Input(System.in);
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    static class Input {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public Input(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public String nextLine() {
            String str = null;
            try {
                str = reader.readLine();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            return str;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public Double nextDouble() {
            return Double.parseDouble(next());
        }

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }
    }

}

  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Studying~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值