AtCoder Grand Contest 024 迟到早退记+ABC题解

本文记录了一场AtCoder比赛的经历,作者在晚上九点临时加入比赛,仅用半小时完成ABC三题,最终意外获得绿名并提高了评级。文章分享了解题思路及代码实现,包括数论题、序列操作题等。

比赛链接

https://agc024.contest.atcoder.jp

游记

晚上九点才想起来有这个比赛……

随便打了打,搞完ABC就21:40了,然后睡觉。

居然还上了绿名,rating+=195?震惊!

A - Fairness

题目大意

一开始有33个数A,B,CA,B,C, 每次操作A1=B+C,B1=A+C,C1=A+B,A1=B+C,B1=A+C,C1=A+B,,然后A=A1,B=B1,C=C1A=A1,B=B1,C=C1,问操作KK次后ABA−B的值。

思路

结论题,手玩一波,发现偶数ABA−B,奇数BAB−A

代码

#include <cstdio>

long long a,b,c,k;

int main()
{
  scanf("%lld%lld%lld%lld",&a,&b,&c,&k);
  if(k&1)
    {
      printf("%lld\n",b-a);
    }
  else
    {
      printf("%lld\n",a-b);
    }
  return 0;
}

B - Backfront

题目大意

给定一个排列,每次可以选择一个元素,将其移到序列最前面或最后面,求最少移动多少次能把排列排好序。

思路

本质就是求每次+1+1的子序列的长度。

代码

#include <cstdio>

const int maxn=200000;

int read()
{
  int x=0,f=1;
  char ch=getchar();
  while((ch<'0')||(ch>'9'))
    {
      if(ch=='-')
        {
          f=-f;
        }
      ch=getchar();
    }
  while((ch>='0')&&(ch<='9'))
    {
      x=x*10+ch-'0';
      ch=getchar();
    }
  return x*f;
}

int n,p[maxn+10],f[maxn+10],ans;

int main()
{
  n=read();
  for(int i=1; i<=n; ++i)
    {
      p[i]=read();
      f[p[i]]=f[p[i]-1]+1;
    }
  for(int i=1; i<=n; ++i)
    {
      if(f[i]>=ans)
        {
          ans=f[i];
        }
    }
  printf("%d\n",n-ans);
  return 0;
}

C - Sequence Growing Easy

题目大意

有一个序列XX,初始值都为0,一种操作,使Xi=Xi1+1Xi=Xi−1+1,现在求将序列XX经过多次操作后,是否能够变成给定序列AA,如果能,输出最少操作次数;如果不能,输出1−1

思路

两种情况输出1−1

  1. Ai>=iAi>=i
  2. Ai>Ai1+1Ai>Ai−1+1

否则,如果Ai=Ai1+1Ai=Ai−1+1,答案+1+1,如果Ai<Ai1+1Ai<Ai−1+1,答案+Ai+Ai

代码

#include <cstdio>

const int maxn=200000;

int read()
{
  int x=0,f=1;
  char ch=getchar();
  while((ch<'0')||(ch>'9'))
    {
      if(ch=='-')
        {
          f=-f;
        }
      ch=getchar();
    }
  while((ch>='0')&&(ch<='9'))
    {
      x=x*10+ch-'0';
      ch=getchar();
    }
  return x*f;
}

int n,a[maxn+10];
long long ans;

int main()
{
  n=read();
  for(int i=1; i<=n; ++i)
    {
      a[i]=read();
      if(a[i]>=i)
        {
          puts("-1");
          return 0;
        }
    }
  for(int i=2; i<=n; ++i)
    {
      if(a[i]>a[i-1]+1)
        {
          puts("-1");
          return 0;
        }
      if(a[i]==a[i-1]+1)
        {
          ++ans;
        }
      else
        {
          ans+=a[i];
        }
    }
  printf("%lld\n",ans);
  return 0;
}

D,E,F

不存在的。

转载于:https://www.cnblogs.com/Canopus-wym/p/10376181.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值