A1046. Shortest Distance (20)

Airsim动态 | 安装ROS包装器(AirSim ROS Wrapper) 本期内容我们将带来在Airsim环境下使用ROS Wrapper的相关教程,以下是AirSim客户端库上的一些ROS Wrapper相关配置。 一、启动 以下步骤适用于 Linux 系统。如果是在 Windows 上运行 AirSim,可以使用 Windows Subsystem for Linux (WSL) 来运行 ROS 包装器,如果不想在主机上安装ROS相关工具,也可以使用Docker容器进行安装。请检查下方内容并按照需求安装,具体内容可以参照最后网址。 如果您的默认 GCC 版本不是8或更高版. 阅读详情

A1046

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.

Input Specification:

Each input file contains one test case. For each case, the first line contains an integer N (in [3, 105]), followed by N integer distances D1 D2 ... DN, where Di is the distance between the i-th and the (i+1)-st exits, and DN is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (<=104), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 107.

Output Specification:

For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.

Sample Input:
5 1 2 4 14 9
3
1 3
2 5
4 1
Sample Output:
3
10
7

我的AC代码如下;

#include <cstdio>
const int maxn = 100005;
int dis[maxn], sum, n;
int main() {
    scanf("%d", &n);
    int num;
    dis[0] = 0;
    sum = 0;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &num);
        dis[i] = dis[i-1]+num;
        sum += num;
    }
    int m, left, right;
    int ans, n_ans;
    scanf("%d", &m);
    while (m--) {
        scanf("%d%d", &left, &right);
        if (left > right) {
            int tmp = left;
            left = right;
            right = tmp;
        }
        ans = dis[right - 1] - dis[left - 1];
        n_ans = sum - ans;
        if (ans > n_ans) {
            ans = n_ans;
        }
        printf("%d\n", ans);
    }
    return 0;
}

注:一段一段保存总和,例如dis[1]是从第0个点到第1个点的距离,而dis[2]是从第0个点开始到第2个点的距离。

所以第1个点到第2个点的距离是dis[2]-dis[1];

但是由于输入时是从1开始计入的,所以是dis[left-1]-dis[right-1]

联邦学习中的差分隐私保护详解及python实现 随着数据隐私法规(如 GDPR、CCPA)日益严格,传统将数据集中到云端训练模型的方式面临合规与安全风险。联邦学习通过让各客户端保留本地数据,仅共享模型更新,缓解了数据流动带来的隐私风险。但模型更新仍可能泄露个体信息。为进一步保障隐私,可在本地更新中引入差分隐私(DP)机制,通过向梯度或模型更新中注入随机噪声,防止反向推断原始数据citeturn0search9。 阅读详情

相关推荐

实战指南 | 利用TSMaster的C小程序实时监控DBC报文发送周期

本文提供了一份利用TSMaster的C小程序实时监控DBC报文发送周期的实战指南。通过创建用户变量、编写C小程序核心逻辑并关联图形模块,工程师可以直观地可视化关键报文(如ID 0x64)的发送间隔,从而快速发现周期异常,提升汽车电子开发和测试中总线问题定位的效率与实时性。

weixin_29178069的博客 418

A1046 Shortest Distance 20分

A1046 Shortest Distance 20分 题目描述: 刚开始做的时候,由于翻译错了题目导致做得不是很顺畅,三个点超时了一个。 思路: 以dis[i]表示从1号顶点以顺时针方向到i+1号顶点的距离,sum表示为一圈的总距离,于是对每个顶点都有总距离=顺时针+逆时针,以顶点V3为例,总距离等于顺时针V1->V2->V3的距离加上V1->V5->V4->V3,也就是逆时针距离=sum-dis[2]。接着比较顺时针和逆时针的距离输出较小的就好了。 需要注意的是给出初始

qq_45735298的博客 220

多尺度变换(Multidimensional Scaling ,MDS)详解

介绍多尺度变换基本原理

5218

PAT A1046 Shortest Distance (20分)

PAT A1046 Shortest Distance (20分)

weixin_48165493的博客 194

更换光猫的原理与方法

当你给装维打电话说你需要更换光猫时,对于装维来说,他并不需要知道怎么更换,他只需要生成一张工单,由后台进行业务操作后,在去用户家把新光猫给装好即可,此时装维到底起到了什么作用呢?我想可能只是运输员吧,把一个设备从一个地方运到另一个地方罢了,此时并不能指望装维做什么,因为本质上来说装维的能力真的很差很差,我是不知道那些装维哪来的勇气做视频,还说自己什么什么能力很强?很强吗?吹牛的技术确实很强。 更换光猫之前,需要先明白为什么要更换光猫,怎么更换光猫,更换后不能用怎么办?怎么才能更换成功,换光猫以后被限速了怎么

qq_34146290的博客 1万+

第 4 课 树莓派5的换源与中文输入法

树莓派换源、中文输入法的安装

嵌入式老牛的博客 2289

PAT A1046. Shortest Distance (20)

原文链接: PAT A1046. Shortest Distance (20) ...

阿豪 135

10xGenomics官方可视化工具——Loupe Browser

Loupe Browser是⼀款10xGenomics开发的交互式桌⾯应⽤程序,⽤于探索10x单细胞数据,可以用来找出感兴趣的细胞、找出显著差异表达的基因、鉴定细胞类型、探索细胞亚类和数据共享。

weixin_47195452的博客 3361

A1046 Shortest Distance (20分)【C语言】

A1046 Shortest Distance (20分)【C语言】 原题链接 题目描述: The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. 输入格式: Each input file contains one test case. For each

SASAKI_130的博客 260

Altium AD20差分对走线设置方法

AD20的PCB差分对走线设置 1、原理图添加差分对 先在原理图中,讲要设置差分对的网络对添加网络标签。 名称前面的内容要相同,后缀用 _P 和 _N 加以区分。 最后使用 放置差分对,将两个网络标记。 2、编译,更改到PCB 编译,更新到PCB,确认差分对网络的出现。 验证更改,执行更改。将改动更新到PCB中。 PCB中,可通过 右下角 Panels - 调出 PCB 显示。来对已添加的差分对进行查看。 这时原本的走线有一点点小问题,需要删掉原来走线,设置差分对规则,并重新布线。

Mark_md的博客 3万+

图神经网络实战(2)——图论基础

图论 (Graph theory) 是数学的一个基本分支,涉及对图研究。图是复杂数据结构的可视化表示,有助于理解不同实体之间的关系。图论提供了大量建模和分析现实问题的工具,如交通系统、社交网络和互联网等。在本节中,将介绍图论的基本原理,主要涉及三个方面:图属性、图概念和图算法。

盼小辉丶的博客 1万+

PAT-A 1046 Shortest Distance (20 分)

1.在一个封闭圆环上,从一个点到另一个点可以走顺时针方向,也可以走逆时针方向,最短距离为走两个方向的距离中较小的那个。 2.如果对于每一对给定的点,都进行遍历计算,在题目条件的极限情况下(105个点,104对距离),肯定会超时。  3.假设出口点从0开始编号,保存从出口0到出口m的距离,在计算两个点之间顺时针的距离时,可以借助出口0到这两个点之间的距离,这样计算时间复杂度可以降到O(1).

qq_38127801的博客 180

解决【x2openEuler环境检查未通过——CheckRepoCurlActor不通过】问题

摘要:x2openEuler使用默认repo源时出现环境检查失败问题,原因是默认源返回308永久重定向错误。解决方法包括:1)为CentOS主机配置华为yum源;2)在x2openEuler Web界面修改为自定义可用源。具体步骤包括备份原源、下载华为源、清除缓存,并通过curl测试确认新源可用性。最终成功配置openEuler-24.03-LTS-SP1版本的repo源后,环境检查顺利通过。

提供C#相关开发 、云计算、运维测试、网络安全相关技术分享与服务,有意向可联系。 259

PAT_A1046 1046 Shortest Distance (20 分)

题目 The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input file co...

朽木可雕_的博客 223

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

PAT A1046 Shortest Distance (20)

题目地址:https://www.patest.cn/contests/pat-a-practise/1046题目描述:The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any

一个程序员的成长之路。。。 399

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

A1046 Shortest Distance (20分)

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input file contain...

wvdon 236

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

A1046. Shortest Distance(20)

17/20,部分超时。 #include<bits/stdc++.h> using namespace std; int N,x,pairs; int a,b; vector<int> dist; void input(){ cin>>N; for(int i=0;i<N;i++){ cin>>x; ...

weixin_30387339的博客 60

PAT A 1046 Shortest Distance (20 分)

思路 构造dist[],dist[i]为i号出口距离1号出口的公里数,同时dist[N]即为环形公路总公里数; 输入e1,e2(若e2<e1则交换),由于公路是环形,则dist[e2] - dist[e1]即两出口间沿正向的距离,而dist[N] - (dist[e2] - dist[e1])即为两出口间沿逆向的距离;

Siumai's Blog 264

STM32F767与ADS122U04的高精度RTD测温方案解析

温度测量是工业控制与楼宇自控的基础环节,而RTD传感器与高精度ADC的组合是实现稳定测温和抗干扰设计的常见路径。ΔΣ架构的24位ADC凭借其分辨率和内置滤波器,能够在复杂电磁环境中捕捉微弱的温度信号。其UART通信接口简化了长线传输的时序设计,配合高性能MCU可高效完成线性化补偿、多通道轮询与协议解析。在HVAC及工业过程监控场景中,高精度温度采集系统的精度目标通常要求±0.2℃甚至更高。本文基于ADS122U04IPWR与STM32F767ZG的实际项目,从硬件前端、固件驱动到校准流程,提供一套可复用的工

weixin_34198453的博客 373

A1046 Shortest Distance (20 分)

A1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input file contains one test case. For each

weixin_46853752的博客 157

PAT(A)1046 Shortest Distance (20 分)

Sample Input 5 1 2 4 14 9 3 1 3 2 5 4 1 Sample Output 3 10 7 思路: 计算两个出口的最短距离,注意这是一个环,可以从两边计算。 代码: #include <bits/stdc++.h> using namespace std; int a[10001]; int main() { int n; sc...

Sqssq的博客 121

【PAT-A1046】 Shortest Distance (20分)(c++)

PAT-A1046 Shortest Distance (20分) 注:没有经过预处理dis数组和sum会很容易超时。在极端情况下需要遍历这个数组。 #include<iostream> #include<algorithm> using namespace std; int main() { int n = 0; int sum = 0; int num_arr[100005] ,dis[100005]; cin >> n; //初始数据 for (in

weixin_44771252的博客 111

PAT:A1046 Shortest Distance (20 分)

PAT:A1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input S...

Ecloss 344

PAT A 甲级 1046 Shortest Distance (20分)

1046 Shortest Distance (20分) 吐槽 看题目第一眼以为是图论,下一秒又想到20分怎么可能是图论。果然不是。 几分钟简简单单写完了程序…… 然后运行超时(微笑)。 思路 这题的唯一问题是,如果每次都老老实实把两个exit之间的各点长度加起来的话,测点2会报运行超时。 解决方法也很简单,一次性累加、保存所有点到点1的距离(从两个方向走的长度都保存下来),之后对任意两点就可以靠...

虚境的博客 245

A1046.Shortest Distance (20)

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits. Input Specification: Each input file c

daijie_whu的博客 300
上一篇: A1042. Shuffling Machine (20)
下一篇: A1025. PAT Ranking (25)
DemonInside
博客等级 码龄10年 1粉丝 18原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值