Code reference from:xyz111
Test instructions: As we all know, Meng Meng's six flowers are not good at maths, so Yong too gave her some math problems to do exercises, one of which is this:
Yong too have a stick of length n, this stick is composed of n 1 small sticks of length, of course, due to the time placed long, some small sticks have not been strong, so Yong too want to let six flowers to the stick into a positive integer length of 4 paragraphs, of which there are 3 paragraphs to no firm small sticks, Yong too hope that the length of these 3 pieces of sticks and can be the largest. At the same time, six flowers hope to meet Yong too requirements to let these three sticks can spell a triangle, ask Meng Meng six flower how many kinds of feasible partition plan?
Of course, this question is very difficult for the six flowers, can you help her?
Analysis: First find out all the small sticks in the leftmost position L and the rightmost position R, can be determined that one of the paragraph must be [L,r]. Because only a paragraph contains unstable, so it must be l,r, I did not think when writing
Then consider the following in two different situations:
1.l=1 or R=n
So the remaining three paragraphs are truncated by a whole block of sticks, and we can enumerate the length of the leftmost segment, so that we can get an inequality about the second stick, and a little discussion.
The discussion here needs to be described in detail
Here, we enumerate the leftmost segment length as x1,
By the conditions that make up the triangle
(x1+n-x1-x2) >x2 X2<N/2, the same can be X1<N/2
X1+x2>n-x1-x2 x2>n/2-x1.
That's N/2-X1<X2<N/2.
Then the number of eligible X2 is (n-1)/2-(N/2-X1)//Note here is less than or less than equals, so subtract a 1 more
2. In addition to the 1 situation
This is relatively easy, the enumeration is the left paragraph or the right paragraph as a complete paragraph, and then enumerate another segment of the cutting point, judging whether it is legal, if legal to add one to the answer.
Time complexity O (n+m).
Hack point: 1. A long long is not opened. 2. The first case is not considered or the first case is wrong. 3. Direct attempt to use O (N2) for violence
1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cmath>5 using namespacestd;6 intl,r,x,n,m;7 intOkintAintBintc)8 {9 if(a<b) Swap ( A, a);Ten if(a<c) Swap (A,C); One if(B+c>a)return true; A Else return false; - } - Long LongCALC2 (intXinty) the { - Long Longtot=0; - for(intI=1; i<x;i++) - if(OK (i,x-i,y)) tot++; + for(intI=1; i<y;i++) - if(OK (i,y-i,x)) tot++; + returntot; A } at Long LongCalc1 (intN) - { - Long Longtot=0; - for(intI=1; i<=n-2; i++) - if(2*i<N) - { intot+= (n1)/2-(n/2-i); - } to returntot; + } - intMain () the { * while(SCANF ("%d%d", &n,&m)! =EOF) $ {Panax NotoginsengL=n,r=1; - for(intI=1; i<=m;i++) the { +scanf"%d",&x); AL=min (l,x); theR=Max (r,x); + } - if(r==n) printf ("%lld\n", Calc1 (l1)); $ Else if(l==1) printf ("%lld\n", Calc1 (nR)); $ Elseprintf"%lld\n", CALC2 (l1, N-R)); - } - return 0; the}
HDU 5203 && BC Round #37 1002