Codeforces Round #731 (Div. 3) A—E

A、Shortest Path with Obstacle

题目
There are three cells on an infinite 2-dimensional grid, labeled A, B, and F. Find the length of the shortest path from A to B

if:
in one move you can go to any of the four adjacent cells sharing a side;
visiting the cell Fis forbidden (it is an obstacle).
Input
在这里插入图片描述Output
在这里插入图片描述题目可以理解为一个二维网格平面中存在A、B、F三个物体。在将F视为障碍物的情况下,找到从A到B的最短距离。
思路
二维网格平面中,只有上下左右四个方向。在没有障碍物足阻拦的情况下,两个物体最短距离唯一,最短路径不唯一(除两物体在一条直线上)。障碍物只有一个,所以除特殊情况外,均不会影响两物体间的最短距离。
故只需考虑A、B、F在同一直线,且F在A、B之间这种情况,其余情况最短距离均为两坐标横坐标相减的绝对值加纵坐标相减的绝对值 :|x1-x2|+|y1-y2| 。特例也可以很容易得出最短距离为 :|x1-x2|+|y1-y2|+2。
代码

B. Alphabetical Strings

题目
在这里插入图片描述Input
在这里插入图片描述Output
在这里插入图片描述题目规定了一种类型的字符串,判断所给字符串是否满足条件。
思路
该类型字符串一定是由同种类型字符串“生长”而来,即在同种字符串首尾增添小写字母。ASCll值大的字母一定出现在字符串的首尾部分。利用双指针解决问题。
代码

	#include <iostream>
    #include<string.h>
    using namespace std;
    int main() {
        int t;
        cin >> t;
        while (t--) {
            string str;
            cin >> str;
            int n = str.size();//通过字符串长度来确定ASCLL码值最大的字母
            int xx = n, l = 0, r = n - 1;//左右两指针初始化
            bool flag = true;//初始化判断依据
            while (xx--) {
                char ch = 'a' + xx;
                if (str[l] == ch) {
                    l++;
                } else if (str[r] == ch) {
                    r--;
                } else {
                    flag = false;
                    break;
                }
            }//逆推字符串的生长过程
            if (flag) {
                cout << "YES" << endl;
            } else {
                cout << "NO" << endl;
            }
        }
            return 0;
    }

C. Pair Programming

题目
在这里插入图片描述Input
在这里插入图片描述Output
在这里插入图片描述两个人分别进行增改两种操作,让你寻找一种正确顺序使得两个人的操作都合法化。
思路
由于两人只存在增加和修改两种操作,文件行数只会递增,只需让一人操作,当操作不合法时,换另一人操作即可(题目所给出的样例输出逻辑为,若双方存在一方为0,则优先操作)。
代码

#include <iostream>
using namespace std;
int main() {
    int t;
    cin >> t;
    int l1,l2;
    int a1[330],a2[330];//储存A、B的操作
    int arr[660];//储存最终的操作顺序
    while (t--) {
        l1=0;l2=0;//分别表示A、B该进行第几步操作
        int k,n,m;
        cin>>k>>n>>m;
        for(int i=0;i<n;i++){cin >> a1[i];}
        for(int i=0;i<m;i++){cin >> a2[i];}
        bool flag=true;
        for(int i=0;i<n+m;i++){
            if(k >= a1[l1] && l1 < n){
                arr[i]=a1[l1];
                if(a1[l1] == 0)
                    k++;
                l1++;
            }else if(k >= a2[l2] && l2 < m){
                arr[i]=a2[l2];
                if(a2[l2] == 0)
                    k++;
                l2++;
            }else{
                flag=false;
                break;
            }
        }
        if(!flag){cout<<"-1"<<endl;}
        else{
            for(int i=0;i<n+m;i++){
                cout<<arr[i]<<" ";
            }
            cout<<endl;
        }
    }
    return 0;
}

D. Co-growing Sequence

题目
在这里插入图片描述Input
在这里插入图片描述Output
在这里插入图片描述题目给出一种数组形式,要求寻找一个数组Y与输入数组X异或后满足题目所给形式。
思路
在原给出的数组X的基础上,寻找Y来辅助X变为题中要求形式。目标数组可以通过数组X的每一项与目标数组前一项求或运算递推而来。而数组Y则是X数组与目标数组求异或得来。
代码

#include <iostream>
using namespace std;
int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        int arr[200020],att[200020],ans[200020];//分别为数组X、目标数组、数组Y
        cin>>n;
        for(int i=0;i<n;i++){cin>>arr[i];}
        att[0]=arr[0];
        for(int i=1;i<n;i++){
            att[i]=(arr[i] | att[i - 1]);
            ans[i] = arr[i] ^ att[i];
        }
        ans[0]=0;
        for(int i=0;i<n;i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

E. Air Conditioners

题目
在这里插入图片描述Input
在这里插入图片描述
Output
在这里插入图片描述
题目给出空调设置的温度,依据公式求每个房间的温度。
思路
房间的温度要么为左边房间的温度加一,要么为右边房间的温度加一。对于所有房间从前往后,再从后往前遍历一遍,每次遍历取最小值即可得出。
代码

#include <iostream>
#include<algorithm>
using namespace std;
int main() {
    int t;
    cin >> t;
    while (t--) {
        int n,k;
        int map[300030],arr[300030];
        cin>>n>>k;
        for(int i=0;i<n;i++){map[i]=2147483647;}
        for(int i=0;i<k;i++){
            int t;
            cin>>t;
            arr[i]=t-1;
        }
        for(int i=0;i<k;i++){
            cin>>map[arr[i]];
        }
        sort(arr,arr+k);//当时写代码时感觉nlnn不会超时所以使用的sort,可以修改,后续只使用首个有空调的房间号和最后一个有空调的房间号
        for(int i=arr[0]+1;i<n;i++){
            map[i]=min(map[i],map[i-1]+1);
        }
        for(int i=arr[k-1]-1;i>=0;i--){
            map[i]=min(map[i],map[i+1]+1);
        }
        for(int i=0;i<n;i++){
            cout<<map[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}
  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值