Lightoj 1307-counting Triangles (two minutes)

Source: Internet
Author: User
Tags acos printf sort time limit valid cmath

Title Link: http://lightoj.com/volume_showproblem.php?problem=1307


1307-counting Triangles

PDF (中文版) Statistics Forum
Time Limit:2 second (s) Memory limit:32 MB

You is given N sticks having distinct lengths; You have a to form some triangles using the sticks. A Triangle is valid if it is positive. Your task is to find the number of ways you can form a valid triangle using the sticks. Input

Input starts with an integer T (≤10), denoting the number of test cases.

Each case is starts with a line containing an integer N (3≤n≤2000). The next line contains N integers denoting the lengths of the sticks. You can assume the lengths is distinct and each length lies in the range [1, 109]. Output

For each case, print the case number and the total number of ways a valid triangle can is formed.

Sample Input Output for Sample Input

3

5

3 12 5) 4 9

6

1 2 3 4 5 6

4

100 211 212 121

Case 1:3

Case 2:7

Case 3:4

Problem Setter:jane ALAM JAN

Developed and maintained by
JANE ALAM JAN
copyright©2012
Lightoj, Jane Alam Jan




Title: Give the length of the triangle, the number of triangles

Topic Analysis: To all sides by the order from small to large, judging is not a triangle, meet a[i] + a[j] > A[k] (i < J < K) can (self-proof,,), and then the range of two k, to seek and


The code is as follows:


Two points (time:412 MS):

#include <iostream> #include <algorithm> #include <map> #include <stack> #include <queue> #include <vector> #include <set> #include <string> #include <cstdio> #include <cstring> #
include<cctype> #include <cmath> #define N 10009 using namespace std;
const int inf = 1E9;
const INT mod = 1<<30;
Const double EPS = 1e-8;
Const double PI = ACOs (-1.0);
typedef long Long LL;

int a[n];
    int main () {int T, n, cnt = 0, I, J;
    scanf ("%d", &t);
        while (t--) {scanf ("%d", &n);
        for (i = 1; I <= n; i++) scanf ("%d", &a[i]);
        Sort (A + 1, a + n + 1);
        int ans = 0, L, R, MD, now; for (i = 1; I <= N, i++) {for (j = i + 1; j <= N; j + +) {L = j; r = N
                ;
                    while (L <= r) {MD = (L + r) >> 1;
           if (A[i] + a[j] > A[MD]) now = MD, L = MD + 1;         else R = md-1;
            } ans + = now-j;
    }} printf ("Case%d:%d\n", ++cnt, ans);
} return 0; }


Non-dichotomy (time:88 MS):

#include <iostream> #include <algorithm> #include <map> #include <stack> #include <queue> #include <vector> #include <set> #include <string> #include <cstdio> #include <cstring> #
include<cctype> #include <cmath> #define N 10009 using namespace std;
const int inf = 1E9;
const INT mod = 1<<30;
Const double EPS = 1e-8;
Const double PI = ACOs (-1.0);
typedef long Long LL;

int a[n];
    int main () {int T, n, cnt = 0, I, J;
    scanf ("%d", &t);
        while (t--) {scanf ("%d", &n);
        for (i = 1; I <= n; i++) scanf ("%d", &a[i]);
        Sort (A + 1, a + n + 1);
        int ans = 0, k;
            for (i = 1; I <= n; i++) {k = i;//must be written so that the second loop will be LTE for (j = i + 1; j <= N; j + +)
                {while (k <= n && a[i] + a[j] > A[k]) k++;
            Ans = ans + k-j-1;
  }} printf ("Case%d:%d\n", ++cnt, ans);  } return 0; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.