B.Conveyor Belts

Codeforces Round 863 (Div. 3)
题目链接
在这里插入图片描述
题目大意:

有个矩阵传送带,从其中一个传送带跳到邻近的传送带需要消费一点能量。问从 x 1 , y 1 x_1,y_1 x1,y1 x 2 , y 2 x_2,y_2 x2,y2最少要多少能量

个人题解:

我只需要算出该点在哪个传送带即可,如果从内向外编号为 1 , 2 , 3 1,2,3 1,2,3,那么我可以通过判断点的位置,分两种情况。
1.在副对角线的上方, x , y x,y x,y取最小值即可。
2.在副对角线的下方, x , y x,y x,y取最大值即可。
然后再将得到的值和对称点取距离,两者相减即可
下面是图示

在这里插入图片描述

#include <iostream>
#include <cmath>
using namespace std;
inline int min(int a,int b){return a>b?b:a;}
inline int max(int a,int b){return a<b?b:a;}

void sove(){
    int n,x1,y1,x2,y2;
    cin>>n>>x1>>y1>>x2>>y2;
    double jun=(((1+(double )n)*n)/2.0)/n;
    double ans1,ans2;
    int num=n+1;
    if(x1+y1>num)ans1=max(x1,y1)-jun;
    else ans1=jun-min(x1,y1);
    if(x2+y2>num)ans2=max(x2,y2)-jun;
    else ans2=jun-min(x2,y2);
    cout<<(int)fabs(ans1-ans2)<<endl;
}

int main(void){
    int _=1;
    cin>>_;
    while(_--)sove();
    return 0;
}

官方题解(贪心)

直接算该点到最外围的传送带最少需要多少能量。
两个点算出来的值相减取绝对值就是答案!!!!!!!!!!!

#include <iostream>
#include <algorithm>
using namespace std;


void sove(){
    int n,x1,y1,x2,y2;
    cin>>n>>x1>>y1>>x2>>y2;
    int ans1=min({x1, y1, n - x1 + 1, n - y1 + 1});
    int ans2=min({x2,y2,n-x2+1,n-y2+1});
    cout<<abs(ans1-ans2)<<endl;
}

int main(void){
    int _=1;
    cin>>_;
    while(_--)sove();
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值