目录
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());
}
}
}