Hdu 5203 Rikka with wood sticks (Bestcoder Round #37)

Source: Internet
Author: User

Rikka with wood sticksTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 446 Accepted Submission (s): 130


Problem Descriptionas We know, Rikka is poor at math. Yuta is worrying on this situation, so he gives Rikka some math tasks to practice. There is one of the them:


Yuta has a wood stick of length n which consists of n Linked sticks of length 1 . So it has n? 1 Connection points. Yuta finds that some sticks of length < Span class= "Mrow" id= "mathjax-span-16" style= ">1 Span style= "" > Of the wood stick is not strong. So he wants to choose three different connection points to cut it into four wood sticks and only one of them contains wood Sticks which is not strong. And Yuta wants to minimize, the length of this piece which contains bad wood sticks. Besides, Rikka wants to use the other three wood sticks to make a triangle. Now she wants to count the number of the ways to cut the wood sticks which can make both Yuta and herself happy.


It is too difficult for Rikka. Can you help her?
Inputthis problem have multi test cases (no more than - ). For each test case, the first line contains the numbers n m ( 1 " span class= "Mo" id= "mathjax-span-28" style= "" >≤ ≤ 1 ≤ m ≤ 1000 ) . The next line contains m numbers (some of them may being same), haven position of each wood sticks which are not strong.
Outputfor Each test cases print is only one number–the ways to cut the wood sticks.
Sample Input
6 135 13

Sample Output
20


Official:

First, find the leftmost position of all the small sticks that are not strong.
    
        L   
  
 
    
 and the rightmost position.
    
        R   
  
 
    
 , it can be determined that one of the paragraph must be
    
        [L,R]    
  
 
    
 。 Then consider in two cases: 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. 2. In addition to
    
        1   
  
 
    
 The situation is relatively easy, the enumeration is the left or the right paragraph as a complete paragraph, and then enumerates another segment of the cutting point, judging whether it is legal, if legal to add one to the answer. Complexity of Time
    
        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. Try directly with
   
    
        O(n2)  
 

    
   of violence

Ps:

The first case is divided into odd and even consideration, enumerate a paragraph, another paragraph can be calculated



Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <    queue>using namespace Std;const int maxn=10000+100;int a[maxn];int main () {int n;    int m;        while (~SCANF ("%d%d", &n,&m)) {for (int i=1;i<=m;i++) scanf ("%d", &a[i]);        int l=a[1];        int r=a[1];            for (int i=2;i<=m;i++) {if (a[i]<l) l=a[i];        if (a[i]>r) r=a[i];        } Long long ans=0;        L=l-1,r=n-r; if (l==0| |            r==0) {N=max (l,r);                if (n%2) {for (int i=1;i<=n/2;i++) {ans+=i;                    }} else {for (int i=1;i<n/2;i++) {                ans+= (i-1);            }}} else {for (int i=1;i<l;i++) {int x, y, Z;    X=i,y=l-i,z=r;                if (x+y>z&&z+x>y&&y+z>x) {ans++;                }} for (int i=1;i<r;i++) {int x, y, Z;                X=i,y=r-i,z=l;                if (x+y>z&&z+x>y&&y+z>x) {ans++;    }}} printf ("%i64d\n", ans); } return 0;}



Hdu 5203 Rikka with wood sticks (Bestcoder Round #37)

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.