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


Rikka with wood sticks
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 446    Accepted Submission(s): 130
Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:
Yuta have 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 
1  of the wood stick are 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 are 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?
 
Input
This problem has multi test cases (no more than 
20 ). For each test case, The first line contains two numbers 
n,m(1≤n≤1000000,1≤m≤1000) . The next line contains m numbers (some of them may be same) – the position of each wood sticks which is not strong.
 
Output
For each test cases print only one number – the ways to cut the wood sticks.
 
Sample Input

   
   
   
   
6 1 3 5 1 3

 
Sample Output

   
   
   
   
2 0

 
公式の問題解:
                  
   
    L
          
   
    R
   
   
    [L,R]
   。
         :
1.
   
    L=1
    
   
    R=n
   
                ,              ,                   ,        。
2.  
   
    1
        
        ,                    ,             ,      ,          。
     
   
    O(n+m)
   。
Hack :1.  long long。2.                   。3.     
   
    O(n2)
      

ps :
1つ目のケースはパリティを考慮して、1つのセグメントを列挙して、もう1つのセグメントを計算することができます
コード:
#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
",ans); } return 0; }