Rikka with wood sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 863 Accepted Submission(s): 242
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?
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
Source
BestCoder Round #37 ($)
题目大意:
给出一些链接起来的木棍,其中有一些木棍不结实,把当前木棍分成连续的四段,要求不结实的木棍都放在其中一段当中,剩下的三段木棍可以拼接成三角形
题目分析:
首先所有不结实的木棍必须放在一个连续的段中,所以只需要找到木棍中最左和最右的两个即可,就是不结实木棍所在段的左右端点,那么之后考虑两种情况:
1.剩下的木棍是分开的两节
那么我们就可以通过三角形的三边不等关系,一定是切割较大的那节木棍,算出切割点的临界值即可
2.剩下的木棍是连续的一节
可以先枚举第一节木棍的长度,然后剩余这节木棍的处理就等同了情况1,直接处理一下即可
题目大意:
给出一些链接起来的木棍,其中有一些木棍不结实,把当前木棍分成连续的四段,要求不结实的木棍都放在其中一段当中,剩下的三段木棍可以拼接成三角形
题目分析:
首先所有不结实的木棍必须放在一个连续的段中,所以只需要找到木棍中最左和最右的两个即可,就是不结实木棍所在段的左右端点,那么之后考虑两种情况:
1.剩下的木棍是分开的两节
那么我们就可以通过三角形的三边不等关系,一定是切割较大的那节木棍,算出切割点的临界值即可
2.剩下的木棍是连续的一节
可以先枚举第一节木棍的长度,然后剩余这节木棍的处理就等同了情况1,直接处理一下即可
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 1007
using namespace std;
typedef long long LL;
LL a[MAX];
LL n,m;
int main ( )
{
while ( ~scanf ( "%lld%lld" , &n , &m ) )
{
for ( int i = 0 ; i < m ; i ++ )
scanf ( "%lld" , &a[i] );
LL minn = a[0] , maxn = a[0];
for ( int i = 1 ; i < m ; i ++ )
minn = min ( minn , a[i] ),
maxn = max ( maxn , a[i] );
LL num = 0 , ans = 0;
if ( minn > 1 ) num++;
if ( maxn < n ) num++;
LL a = min ( minn - 1 , n - maxn );
LL b = max ( minn - 1 , n - maxn );
if ( num == 2 )
{
if ( b > a && a+b >= 3 )
{
LL t = (b-a+1)/2;
if ( (b-a)&1 ) ans = b - 2*t + 1;
else ans = b - 2*t - 1;
}
}
if ( num == 1 )
{
if ( b >= 3 )
{
for ( LL i = 1 ; i < (b+1)/2 ; i++ )
{
LL tb = b - i;
a = i;
LL t = (tb-a+1)/2;
if ( (tb-a)&1 ) ans += tb - 2*t + 1;
else ans += tb - 2*t - 1;
}
}
}
printf ( "%lld\n" , ans );
}
}