【CF #738 (Div. 2)】A. Mocha and Math+C. Mocha and Hiking

题目:

A. Mocha and Math

分析:

关于位运算的题目。前几次只要遇到位运算,我就卡,就是转不过弯,然后也没有补题。报应来了,当位运算题目出到A这个位置,我依旧不会做。
首先,题目有句: This operation can be performed any number of times.
翻译一下:该操作可以执行任意次数。
大概就知道其实是可以随意按位&操作这些数了。
那么怎样能让最大数变得最小,全部给他按位&一边就得了。(笑死,我对按位运算的不敏感程度根本想不到,我是弱智。

代码:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e6+3;

int main()
{
    int t;cin>>t;
    while(t--){
        int n;cin>>n;
        int ans;cin>>ans;
        n--;
        while(n--){
            int x;cin>>x;
            ans&=x;
        }
        cout<<ans<<endl;
    }

    return 0;
}

题目:

C. Mocha and Hiking

题意:

n+1个点,一些道路:
1)对于所有1≤I≤N−1的村庄,从村庄I到村庄I +1有N−1条道路。(注意这是已经规定好方向了的)
2)N条路可以用序列a1,…,an来描述。如果ai=0,第i条路是从村庄i到村庄n+1,否则从村庄n+1到村庄i,对于所有1≤i≤n。
怎么样遍历一遍这个数,并且不重复。

碎碎念:

上战绩,传说中的爬山算法(?)
一定要理解题意,一定要理解完题意再看题,不要猜题!!!血的教训!!!
在这里插入图片描述

分析:

其实能走完的也就三种情况
1)1到n+1
2)n+1->1到n
3)中间到达n+1但只能拐到前一个的下一个。即两个相邻的数完成一去一回。

代码:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e4+3;
int a[maxn];
int n,m;
int main()
{
    int  t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=1; i<=n; i++)cin>>a[i];
        a[n+1]=0;
        int flag=1;
        if(a[n]==0)
        {
            for(int i=1; i<=n; i++)cout<<i<<" ";
            cout<<n+1<<endl;
            flag=0;
        }
        else if(a[1]==1){
            cout<<n+1<<" ";
            for(int i=1; i<=n; i++)cout<<i<<" ";
        cout<<endl;
            flag=0;
        }
        if(flag) {
                int f=0;
                int nowp=-1;
                for(int i=1; i<=n; i++)
                {
                    if(a[i]==0&&a[i+1]==1)
                    {
                        nowp=i;
                        f=1;
                        break;
                    }
                }
                if(f)
                {
                    for(int i=1; i<=nowp; i++)cout<<i<<' ';
                    cout<<n+1<<' ';
                    for(int i=nowp+1; i<=n; i++)cout<<i<<' ';
                    cout<<endl;
                    flag=0;
                }
        }
        if(flag)cout<<-1<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值