hdu 5203 Rikka with wood sticks

针对一个数学问题,探讨如何将包含部分不结实段的长木棍切割为四段,使得其中仅一段包含不结实的部分,且其余三段能组成三角形。文章分析了不同切割策略并提供了具体的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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
 

Source
BestCoder Round #37 ($)
题目大意:
给出一些链接起来的木棍,其中有一些木棍不结实,把当前木棍分成连续的四段,要求不结实的木棍都放在其中一段当中,剩下的三段木棍可以拼接成三角形
题目分析:
首先所有不结实的木棍必须放在一个连续的段中,所以只需要找到木棍中最左和最右的两个即可,就是不结实木棍所在段的左右端点,那么之后考虑两种情况:
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 );
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值