Advertisement
Guest User

Untitled

a guest
Apr 13th, 2013
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class trapezoid_va {
  5.     public static void main(String[] args) {
  6.         new trapezoid_va().run();
  7.     }
  8.  
  9.     BufferedReader br;
  10.     StringTokenizer in;
  11.     PrintWriter out;
  12.  
  13.     public String nextToken() throws IOException {
  14.         while (in == null || !in.hasMoreTokens())
  15.             in = new StringTokenizer(br.readLine());
  16.         return in.nextToken();
  17.     }
  18.  
  19.     public int nextInt() throws IOException {
  20.         return Integer.parseInt(nextToken());
  21.     }
  22.  
  23.     public void solve() throws IOException {
  24.         int n = nextInt();
  25.         int[] a = new int[n];
  26.  
  27.         for (int i = 0; i < n; i++) {
  28.             a[i] = nextInt();
  29.         }
  30.         Arrays.sort(a);
  31.  
  32.         int[] cnt = new int[n];
  33.  
  34.         int l = 0;
  35.         cnt[0] = 1;
  36.         for (int i = 1; i < cnt.length; i++) {
  37.             if (a[i] == a[i - 1]) {
  38.                 cnt[l]++;
  39.             } else {
  40.                 l++;
  41.                 a[l] = a[i];
  42.                 cnt[l] = 1;
  43.             }
  44.         }
  45.         l++;
  46.  
  47.         a = Arrays.copyOf(a, l);
  48.         cnt = Arrays.copyOf(cnt, l);
  49.         long ans = 0;
  50.         n = l;
  51.         int[] sum = new int[n];
  52.         for (int i = 0; i < n; i++) {
  53.             sum[i] = (i > 0 ? sum[i - 1] : 0) + cnt[i];
  54.         }
  55.  
  56.         for (int i = 0; i < n; i++) {
  57.             if (cnt[i] == 1)
  58.                 continue;
  59.             long mul = cnt[i] * (cnt[i] - 1) / 2;
  60.             cnt[i] -= 2;
  61.             l = 0;
  62.             int r = 0;
  63.             while (l < n - 1) {
  64.                 while (r < n && a[r] < a[l] + 2 * a[i])
  65.                     r++;
  66.  
  67.                 int z = sum[r - 1] - sum[l];
  68.                 if (l + 1 <= i && i < r) {
  69.                     z -= cnt[i] + 2;
  70.                     ans += mul * cnt[l] * cnt[i] / 3;
  71.                 }
  72.  
  73.                 ans += mul * z * cnt[l] / (l == i ? 3 : 1);
  74.  
  75.                 l++;
  76.             }
  77.  
  78.             for (int j = i + 1; j < sum.length; j++) {
  79.                 ans += mul * cnt[j] * (cnt[j] - 1) / 2;
  80.             }
  81.             ans += mul * cnt[i] * (cnt[i] - 1) / 12;
  82.  
  83.             cnt[i] += 2;
  84.         }
  85.  
  86.         out.println(ans);
  87.     }
  88.  
  89.     public void run() {
  90.         try {
  91.             br = new BufferedReader(new InputStreamReader(System.in));
  92.             out = new PrintWriter(System.out);
  93.  
  94.             int t = nextInt();
  95.             for (int i = 0; i < t; i++)
  96.                 solve();
  97.  
  98.             out.close();
  99.         } catch (IOException e) {
  100.             e.printStackTrace();
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement