【Codechef】Chef and Triangles -Problem Code: MAKETRI

Questions surface:

  Xiao Ming was tired of cooking, cooking for him too simple. That day, he decided to challenge yourself. He has chosen N root pasta, ready to do a dish. He needs to select a pasta, pasta can be such that the root and the existing N root surface in a two form a triangle . The length of the pasta must be selected in the [L, R] in the range. Please find Xiao Ming selected number scheme .

Input:

  The first line gives the N, L, R. The second line gives the number N len1, len2, ..., lenN, the N denotes the length of the pasta.

  2<=N<=1e6 1<=L,R<=1e18,1<=leni<=1e18

Output:

  The number of output solutions.

Example:

The INPUT: 
5 1 4 
1 2 3 4 5 
the Output: 
3 

This title card me a long time. After all, I have not done this kind of topic before.

Or the meaning of the title is simple, but is from the [L, R] can range selected sides of roads number given to make up the number of triangular array .
See the data size of nature is not violence, after all, O (n2) plus size 1e6 is bound to explode.

First, after the title is to scrape triangle, we can think of the inevitable nature triangles:
1. any two sides of a third side is greater than
the difference between any two sides is less than 2. The third side

suppose we take x in the [L, R] a length of the side length of the array, and from then to take the given edge length Ai and Aj.
Easy to get the following equation:
· the X-+ Ai> Aj -> the X-> Aj - Ai
· the X-+ Aj> Ai -> the X-> Ai - Aj
· Ai + Aj> the X--> the X-<Ai + Aj
About Why are these three equations, in fact, you can even write the other three also into this, I just took for the convenience of such a formula.

So we can naturally come up, [L, R] and the number of program x [max (Ai-Aj, Aj-Ai) +1, Ai + Aj-1] is the intersection of the selected Ai and Aj.
The above means that both the intersection but also the composition can be a triangle with Ai and Aj in the [L, R] range range.

But how about the selected Ai and Aj is indeed still a problem. After all, if violence or O ((R-L + 1 ) * n2).
Here introduce a very important conclusion:

Suppose there are three numbers A1 <A2 <A3, we define R (i, j) represents the triangular range Ai, Aj are able to form.
Therefore,
  R & lt (1,2) = [A1-A2. 1 +, A2 + A1-1],
  R & lt (2,3) = [+ A3-A2. 1, A3 + A2-1]
  R & lt (l, 3) = [ A3-A1 + 1, A3 + A1-1].
R (2,3) is completely contained R (1,3) of. This is more proof of the good, after all, two larger and more similar to the triangle formed by the range of the number, must be better than two smaller far away from the large number to.
(Specific example can prove)

With this conclusion we can sort A first array of n-1 after passing through the pair of R (j-1, j) and collector (1-n a total of n-1 and j j-1), then the [L, R] determined number of intersection i.e. the program can be asked for.
But be sure to pay attention to special circumstances, or will send a lot of WA.

The following is the AC codes:
#include<bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
    long long n,l,r;
    scanf("%lld %lld %lld",&n,&l,&r);

    ll a[n];

    for(ll i=0;i<n;i++)
    scanf("%lld",&a[i]);
    sort(a,a+n);

    ll sum,diff,ans=0;
    if (l > r)
    {
        cout << ans << endl;
        return 0;
    }

    for(ll i=n-1;i>0;i--)
    {
        sum = a[i]+a[i-1]-1;
        diff = a[i]-a[i-1]+1;
        //这里直接求交集
        ll s = max(l,diff);
        ll d = min(sum,r);
        if (s <= d)
            ans += d-s+1;
        //把选用过的长度去掉
        r = min(r,diff-1);
    }

    printf("%lld\n",ans);

    return 0;
}
View Code

Guess you like

Origin www.cnblogs.com/Vikyanite/p/12177631.html