AtCoder Regular Contest 098(C,D)题解

13 篇文章 0 订阅
13 篇文章 1 订阅

                    AtCoder Regular Contest 098

C题题意:给定N个人的东西朝向,找到一个人当作中心点,让其他人都朝向他,求其他人最少的转动次数。

C题题解:从前记录一遍朝东的前缀和,从后记录一遍朝西的前缀和,然后遍历,如果让第i个人当中心点就是两个前缀和的和-1,找最小值即可。

AC代码:

#include<iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 300005;
#define _for(i,a,b) for(int i=a;i<=b;i++)
char s[maxn];
int n,a[maxn],b[maxn];
int main(int argc, char const *argv[])
{
    cin>>n;
    _for(i,1,n)cin>>s[i];
    _for(i,1,n)
    {
        if(s[i]=='W')a[i]=a[i-1]+1;
        else a[i]=a[i-1];
    }
    for(int i=n;i>=1;i--)
    {
        if(s[i]=='E')b[i]=b[i+1]+1;
        else b[i]=b[i+1];
    }
    int ans = maxn+7;
    _for(i,1,n)ans=min(ans,a[i]+b[i]-1);
    cout<<ans<<endl;
    return 0;
}

D题题意:给定一个数组,求有多少个子序列满足Al xor Al+1 xor … xor Ar=Al Al+1 + … Ar  。

D题题解:对于一个序列满足该关系式的充要条件是各数字每一位的和<=1因为大于2时加法的贡献永远是比异或的贡献大,就不可能成立,所以用尺取法,前后遍历一遍即可。

AC代码:

#include <iostream>
using namespace std;
const int maxn = 200006;
#define _for(i,a,b) for(int i=a;i<=b;i++)
int n;
long long a[maxn],ans;
int main(int argc, char const *argv[])
{
    cin>>n;
    _for(i,1,n)cin>>a[i];
    long long k = 0;
    long long now = 0;
    int l = 1;
    int r = 1;
    long long next = 0;
    while(l<=n+1&&r<=n+1)
    {

        if(now==k)
        {          
            ans+=next;
            next++;
            k+=a[r];
            now=now^a[r];
            r++;
        }
        else
        {
            next--;
            k-=a[l];
            now=now^a[l];
            l++;
        }
    }
    cout<<ans<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值