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  n1  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(1n1000000,1m1000) . 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 :

第一种情况分奇偶考虑下,枚举一段,另一段就可以算出来



代码:

#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;
}



你可能感兴趣的:(Algorithm,编程,ACM,BestCoder)