Gym - 102860A_B题解。。Jumping Machine ——Triangles and a Circle

本文介绍了两道算法题目,分别是关于跳跃机械的最远覆盖范围计算和包含圆心的三角形计数。对于跳跃机械问题,通过动态规划和映射计算所需保护垫的数量;对于三角形计数问题,通过不同方法(尺取法、二分查找)确定包含圆心的三角形个数。文章提供了详细的代码解析和优化过程。

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

对于这套题目锐佬晚上分享啦自己的博客,太强啦。直接粘过来留着。博客分享

标题A - Jumping Machine Gym - 102860A

题目链接
Jumping Machine
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 512 megabytes
Young inventor has created a new jumping machine. In order to test it, he brought it to the testing
polygon. The polygon is an infinite square grid.
Initially, the machine is located in the cell (0, 0). The machine has n springs, the i-th spring has the force
of li and allows the machine to jump li cells up or li cells to the right. Therefore this spring allows the
machine to get from cell (x, y) either to cell (x + li
, y), or to cell (x, y + li). After jumping, the spring is
thrown back and cannot be reused. The machine can use the springs in any order.
During the tests the cells, that the machine will fly over, will be stained with machine oil. In order not
to clean the grid after himself, the inventor has decided to put a protective mat on each cell the machine
could potentially fly over.
Now the inventor is wondering how many protective mats he needs to bring to the test with him.
Input
The first line of input contains n — the number of springs that the machine has (1 ≤ n ≤ 100). The
second line contains n integers li — the springs forces (li ≥ 1; 1 ≤ l1 + l2 + · · · + ln ≤ 106
).
Output
Output a single integer: the number of mats that inventor needs to bring.
Example
standard input standard output
2
4 2
22
Explanation
All the cells that can get dirty when the machine jumps in the example test are colored orange one the
figure below.
在这里插入图片描述
一共n个数,看能走过多少格子。
看的别的代码,大致看懂啦,在这里粘一下,附带一点个人理解。

#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
//#include <>
using namespace std;
typedef long long int ll;
const int N = 1e6+10;
//unordered_
map<int ,int > ma;//注意本题需要用map进行映射,因为个数可能不多,但是ma里面存的东西(下标)真的够大
int n, l[N],dp[N],sum1[N];
vector<int >s;
int main()
{
    scanf("%d",&n);
    ll sum=0,i,j;
    for(i=1;i<=n;i++){
        scanf("%d",&l[i]);
        sum+=l[i];
    }
    dp[0]=1;
    for(i=1;i<=n;i++){
        for(j=sum;j>=l[i];j--){
            if(dp[j-l[i]]&&!dp[j]){
                dp[j]=1;
                s.push_back(j);
            }
        }
    }
    sort(s.begin(),s.end());
    int len=s.size();
    for(i=0;i<len;i++){
        ma[s[i]]=i+1;//求一下竖行编号。
    }
    ll ans = 2*sum+1;
    for(i=1;i<sum;i++){
        if(dp[i]){
            ans+=sum-i;//jiashang meiyishuhang de shuliang;
        }
    }
    for(i=1;i<sum;i++){
        if(dp[i]){
            ans+=i-ma[i];//竖着从左往右看图,对于每一个拐点都是当前节点之前有几个竖行然后i-那些竖行就是剩余的格子数。
        }
    }
    cout<<ans<<endl;
    return 0;
}

标题B - Triangles and a Circle Gym - 102860B

You are given n distinct points on a circle of length L.

You need to find the number of triangles with vertices in these points that contain the center of the circle inside, or on their border.

Input
The first line of input contains two integers n and L (3≤n≤300000, n≤L≤109).

Let’s choose any point on the circle and call it S. Then any point A on the circle can be encoded as x, 0≤x<L: clockwise distance from S to A. We will call this number a coordinate of A.

The second line contains n distinct integers x1,x2,…,xn: coordinates of the given points on the circle (0≤xi<L).

Output
Print one integer: the number of triangles with vertices in the given points, that contain the center of the circle inside or on their border.

Examples
Input
3 10
0 1 2
Output
0
Input
10 10
0 1 2 3 4 5 6 7 8 9
Output
60
Sponsor
题意,就是给一个圆周长(0——len-1),然后在上面有坐标点,问选取三个坐标点构成的三角形有多少个能包含圆心或者使得圆心落在三角形边上。
思路:选取相邻的点,只要他们之间的差值比len/2小就是不可能构成复合条件的三角形。这样确定最后一个点r取在l到r-1的点中任意选取两个都能保证符合并且不和前面求过的不符合的情况一样。取一周,就好啦。
自己用尺取法写的,直接废物。我就是个废物。。。
最后学长帮我解决啦。是因为我的l的存值是错的。会漏解。
在这里插入图片描述
行就这样。然后代码:

#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
//#include <>
using namespace std;
typedef long long int ll;
ll b[350000];
ll a[950000];
ll q[350000];
ll ccc2(ll n)
{
    return n*(n-1)/2;
}
ll ccc3(ll n)
{
    return n*(n-1)*(n-2)/6;
}
int main()
{
    ll n,m,j,i,k,len;
    scanf("%lld%lld",&n,&len);
    for(int i=0;i<n;i++){
        scanf("%lld",&b[i]);
    }
    sort(b,b+n);
    for(ll i=0;i<n;i++)
    {
        a[i+n]=a[i]=b[i]*2;
        a[i+n]+=len*2;
    }
    ll l,r,sum,num;
    ll ban=len;
    ll ans=ccc3(n);
    r=l=0;
    memset(q,0,sizeof(q));
    while(r!=2*n&&l<=n-1)
    {
        if(a[r]-a[l]<ban)
        {
                
            r++;
        }
        else
        {q[l]=max(q[l],ccc2(r-l-1));
            l++;}
    }
    for(int i=0;i<n;i++)
    {
        ans-=q[i];
    }
    cout<<ans<<endl;
    return 0;
}

学长说的那种短小型尺取法:
破而后立:

#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
//#include <>
using namespace std;
typedef long long int ll;
ll b[350000];
ll a[950000];
ll ccc2(ll n)
{
    return n*(n-1)/2;
}
ll ccc3(ll n)
{
    return n*(n-1)*(n-2)/6;
}
int main()
{
    ll n,m,j,i,k,len;
    scanf("%lld%lld",&n,&len);
    for(int i=0;i<n;i++){
        scanf("%lld",&b[i]);
    }
    sort(b,b+n);
    for(ll i=0;i<n;i++)
    {
        a[i+n]=a[i]=b[i]*2;
        a[i+n]+=len*2;
    }
    ll l,r,sum,num;
    ll ban=len;
    ll ans=ccc3(n);
    r=l=0;
    for(ll i=0;i<n;i++)
    {
        while(a[r]-a[i]<ban) r++;
        ans-=ccc2(r-i-1);
    }
    cout<<ans<<endl;
    return 0;
}

最后加一句话,我们将那个所有数字都乘以2,对于就没有啦除以 2的操作,这样就可以遇到⚪的周长是奇数我们也不用管直接算就行啦。还有一个就是一定要排序,我一开始没有排序一直wa4
最后在来一份二分lower——bound版本的,(偷看学长的代码)真香

#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
//#include <>
using namespace std;
typedef long long int ll;
ll b[350000];
ll a[950000];
ll q[350000];
ll ccc2(ll n)
{
    return n*(n-1)/2;
}
ll ccc3(ll n)
{
    return n*(n-1)*(n-2)/6;
}
int main()
{
    ll n,m,j,i,k,len;
    scanf("%lld%lld",&n,&len);
    for(int i=0;i<n;i++){
        scanf("%lld",&b[i]);
    }
    sort(b,b+n);
    for(ll i=0;i<n;i++)
    {
        a[i+n]=a[i]=b[i]*2;
        a[i+n]+=len*2;
    }
    ll l,r,sum,num;
    ll ban=len;
    ll ans=ccc3(n);
    for(int i=0;i<n;i++)
    {
        r=lower_bound(a,a+2*n,a[i]+ban)-a-1;
        ans-=ccc2(r-i);
    }
    cout<<ans<<endl;
    return 0;
}

尺取快,尺取140,用二分170😕

<think>好的,用户遇到了Python脚本joint_monkey.py中的未识别参数--headless和--debug的问题。我需要根据他们提供的引用信息来分析可能的原因和解决方法。首先,这些引用中提到的错误都是关于unrecognized arguments,通常与argparse模块的使用有关。比如引用[1]、[2]、[3]、[4]、[5]都涉及类似的问题,可能是因为参数未正确添加到解析器中,或者在调用脚本时参数传递的方式不正确。 首先,用户需要检查他们的代码中是否正确定义了这些参数。可能是在使用argparse时没有为脚本添加对应的参数选项。例如,用户可能忘记在add_argument方法中定义--headless和--debug这两个参数,或者在代码中有拼写错误,导致解析器无法识别。 其次,用户可能在运行脚本时传递参数的方式有误。例如,是否在参数前正确使用了双破折号(--),或者参数是否需要赋值(如--headless可能是一个布尔标志,不需要值,而--debug可能需要一个值)。需要确保命令行参数的格式与代码中定义的匹配。 另外,用户可能遇到了环境问题,比如虚拟环境中的依赖不一致,导致参数解析出现问题。引用[5]中提到,有时在PyCharm和VSCode中运行结果不同,可能是因为虚拟环境的配置不同。因此,检查虚拟环境是否正确配置,或者尝试在其他环境中运行脚本可能会有帮助。 还有一个可能性是,用户可能在调用脚本时,参数被其他工具或框架错误地捕获,比如在使用pytest或其他测试框架时,参数可能被误解为框架的选项而不是脚本自身的参数。引用[4]中的情况显示,参数被传递给pytest而不是用户的脚本,导致错误。因此,需要确保参数正确地传递给目标脚本,而不是被中间工具处理。 最后,用户可能需要检查是否有代码中存在其他地方的参数解析,或者在主函数中没有正确调用parse_args方法。例如,是否在脚本中存在多个ArgumentParser实例,导致参数被错误地解析。 总结可能的解决步骤:1.检查并确保参数已正确定义;2.确认命令行参数格式正确;3.检查虚拟环境和依赖;4.确保参数传递给正确的脚本;5.处理冲突的库或框架参数。根据这些思路,逐步排查问题所在。</think>### 解决Python脚本`joint_monkey.py`未识别参数`--headless`和`--debug`的问题 #### 问题分析 当运行Python脚本时出现`unrecognized arguments`错误,通常是因为参数解析器(如`argparse`)未正确定义相关参数,或参数传递方式有误。根据引用中的类似案例[^1][^2][^3][^4][^5],可能原因包括: 1. **参数未在代码中声明**:`--headless`和`--debug`未被`argparse.ArgumentParser`正确添加。 2. **参数格式错误**:参数名称拼写不一致(如`--headless`与`--headles`)。 3. **环境依赖冲突**:虚拟环境或IDE配置问题导致参数解析异常。 4. **参数被其他工具拦截**:参数被测试框架(如`pytest`)或IDE插件错误捕获。 --- #### 逐步解决方案 ##### 1. **检查参数定义** 确保`joint_monkey.py`中使用`argparse`正确定义了`--headless`和`--debug`参数。示例代码: ```python import argparse def main(): parser = argparse.ArgumentParser(description="Joint Monkey脚本参数配置") # 添加--headless参数(布尔类型,无需赋值) parser.add_argument("--headless", action="store_true", help="无界面模式") # 添加--debug参数(可选赋值,如--debug 1) parser.add_argument("--debug", type=int, default=0, help="调试级别") args = parser.parse_args() # 其他逻辑... if __name__ == "__main__": main() ``` - **关键点**: - `action="store_true"`表示`--headless`为布尔标志,无需额外值。 - `type=int`表示`--debug`需要赋值(如`--debug 1`),否则可能报错。 ##### 2. **验证命令行调用格式** 运行脚本时,参数格式需与定义一致: ```bash # 正确方式 python joint_monkey.py --headless --debug 1 # 错误方式(若--debug未定义为布尔类型) python joint_monkey.py --headless --debug ``` ##### 3. **检查环境依赖** 若代码无误但报错仍存在,可能是虚拟环境问题: - **重新安装依赖**:确保`argparse`库正确安装(Python自带,通常无需额外安装)。 - **切换IDE环境**:如引用[^5]所述,PyCharm可能因虚拟环境配置异常导致问题,可尝试在终端或VSCode中运行。 ##### 4. **处理参数拦截问题** 若使用测试框架(如`pytest`)或IDE插件,可能需明确分隔脚本参数与框架参数。例如: ```bash # 使用--分隔参数(防止框架误解析) python joint_monkey.py -- --headless --debug 1 ``` --- #### 示例验证 假设修复后的`joint_monkey.py`已正确定义参数,运行结果应如下: ```bash $ python joint_monkey.py --headless --debug 2 # 无报错,参数被正确解析 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值