Rikka with wood sticks(hdu5203)

Rikka with wood sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1166    Accepted Submission(s): 356


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
思路:那段不好的肯定要包括全部的坏木。
所以有三种情况坏的那段[minn,maxx];minn>1&&maxx<n;
那么定一段,然后暴力枚举另一段分成两段;
minn==1,那么就是[maxx+1,n]要分成三段的方案,那么这个就是a+b+c=l;
那么c = l-a-b;那么根据三角形三边限制就可以得到方程组a+b > l/2;
a < l/2;b<l/2;
那么暴力枚举a统计b就行了;
maxx == n这个和第二中是一样的
复杂度O(n);
 1 #include <stdio.h>
 2 #include<math.h>
 3 #include<string.h>
 4 #include<math.h>
 5 typedef long long LL;
 6 const long long mod = 1e9+7;
 7 bool check(int a,int b,int c);
 8 int main(void)
 9 {
10     int n,m;
11     while(scanf("%d %d",&n,&m)!=EOF)
12     {
13         int i,j;
14         int maxx = 0;
15         int minn = 1e9;
16         while(m--)
17         {
18             int id;
19             scanf("%d",&id);
20             if(id > maxx )
21                 maxx = id;
22             if(minn > id)
23             {
24                 minn = id;
25             }
26         }
27         int ll = minn - 1;
28         int rr = n - maxx;
29         LL sum = 0;
30         if(ll!=0&&rr!=0)
31         {
32             for(i = 1; i <= rr; i++)
33             {
34                 int a = ll;
35                 int b = i;
36                 int c = rr-i;
37                 if(check(a,b,c))
38                 {
39                     sum++;
40                 }
41             }
42             for(i = 1; i <= ll; i++)
43             {
44                 int a = rr;
45                 int b = i;
46                 int c = ll-i;
47                 if(check(a,b,c))
48                 {
49                     sum++;
50                 }
51             }
52         }
53         else if(ll == 0)
54         {
55             for(i = 1; i <= ((rr-1)/2); i++)
56             {
57                 if(rr/2+1-i <= (rr-1)/2)
58                 {
59                     int yy = (rr-1)/2-rr/2+i;
60                     sum += (LL)(yy);
61                 }
62             }
63         }
64         else if(rr == 0)
65         {
66             for(i = 1; i <= ((ll-1)/2); i++)
67             {   if(ll/2+1-i<=(ll-1)/2)
68                 {
69                     int yy = (ll-1)/2-ll/2+i;
70                     sum+=(LL)(yy);
71                 }
72             }
73         }
74         printf("%lld\n",sum);
75     }
76     return 0;
77 }
78 bool check(int a,int b,int c)
79 {
80     if(a+b>c&&b+c>a&&a+c>b)
81     {
82         return true;
83     }
84     return false;
85 }

 

posted @ 2016-12-03 14:53  sCjTyC  阅读(253)  评论(0编辑  收藏  举报