Beep beep Mile Mile 2019 autumn programming trick question --- cottage gold sparkle

 

 

 

 Analysis: This is the subject of an ability to form a triangle on the

Triangular configuration conditions: a + b> c (provided a <= b <= c, i.e. ordering) plus a> 0 && b> 0 && c> 0 (there is no abnormality subject data is not considered)

 Therefore, according to the request directly after the subject is determined in ascending order and then a [i] + a [i + 1]> a [i + 2], plus the number 1 if there is a direct

Explain why a [i] + a [i + 1]> a [i + 2] can be determined, there is no case where a [i] + a [i + 1]> a [i + 2] is not satisfied but there are triangles of it?

If there are ascending array after a [i] + a [i + x]> a [i + y], (x> y> = 2), i.e. the random number acquisition triangular if three conditions are satisfied

So there must be a [i] + a [i + y-1]> a [i + y], (because it is ascending, the larger the number the greater the subscript, the subscript i + y-1> = i + x Therefore a [i + y-1]> = a [i + x], the equation is still valid)

Similarly a [i + y-2] > = a [i], so there must be a [i + y-2] + a [i + y-1]> a [i + y], that is, a [i ] + a [i + 1] > a [i + 2] must exist. .

Note: The title does not require obtained [l, r] How many triangles range so do not do the extra work

c ++ code is as follows:

#include<bits/stdc++.h>
using namespace std;
const int MAXN=(int)1e7 + 5;
int n,a[MAXN],m;
vector<int>v;
int main() {
    while(~scanf("%d",&n)) {
        for(int i=1; i<=n; i++)scanf("%d",&a[i]);
        scanf("%d",&m);
        int cnt=0;
         The while (M-- ) {
             int L, R & lt; 
            Scanf ( " % D% D " , & L, & R & lt);
             IF (L-R & lt + . 1 > = 47 ) CNT ++; // because a large interval is easy once form a triangle, the others out of the boundaries of the test 
            the else  IF (L-R & lt + . 1 < . 3 ) Continue ;
             the else { 
                v.clear (); 
                for ( int I = L; I <= R & lt; I ++ ) v.push_back (a [I] ); 
                Sort (v.begin (), v.end ()); 
                int len = v.size ();
                bool flag=0;
                for(int i=0; i<len-2; i++) {
                    if(v[i]+v[i+1]>v[i+2]) {
                        flag=1;
                        break;
                    }
                }
                if(flag)cnt++;
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

learn by analogy:

Now requires an array of statistics interval how many triangles can be constructed. This is a topic of leetcode: https://leetcode-cn.com/problems/valid-triangle-number/?utm_source=LCUS&utm_medium=ip_redirect_q_uns&utm_campaign=transfer2china

c ++ code is as follows:

 

class Solution { // Title thought: fixing c, respectively, then variable around the left sliding c 
public :
     int triangleNumber (Vector < int > & the nums) {
         int COUNT = 0, size = nums.size (); 
        Sort (the nums. the begin (), nums.end ()); 
        for ( int I = size -. 1; I> = 2; i--) { // [I] NUM is C 
            int left = 0, right = I -. 1; / / NUM [left] is a, num [right] is B 
            the while (left < right) {
                 IF (the nums [left] + the nums [right]> the nums [I]) { 
                    COUNT + = (right - left); //Should not explain it 
                    Right -; // calculate all triangles appear in b, b to the left 
                }
                 the else { 
                    left ++; // and too small, a right 
                } 
            } 
        } 
        return COUNT; 
    } 
};

 

Guess you like

Origin www.cnblogs.com/cstdio1/p/11484710.html