首页 > 其他分享 >Codeforces Round #845 (Div. 2) and ByteRace 2023(A,B,C)

Codeforces Round #845 (Div. 2) and ByteRace 2023(A,B,C)

时间:2023-02-02 21:37:25浏览次数:69  
标签:845 include int cnt Codeforces maxn ByteRace ans 排序

Codeforces Round #845 (Div. 2) and ByteRace 2023(A,B,C)

A

A

这个题目大意是有n个数,我们需要让ai和ai+1的奇偶性不一样

我们可以让相邻的两个数变成一个数,那个数就是这两个数相乘的结果

我们知道,对于两个奇偶性一样的,两个数相乘还是不能改变奇偶性,所以我们主要考虑那些多余1的同一性质的数,让其中的数量变成1个

#include <iostream>
using namespace std;
const int maxn=2e5+10;
#define int long long 
int t,n;
void solve()
{
    cin>>n;
    int ans=0;
    int cnt=0;
    int flag=-1;
    for (int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if (i==1)
        {
            cnt=1;
            flag=x&1;
        }
        else 
        {
            int now=x&1;
            if (now==flag)
            {
                cnt++;
            }
            else 
            {
                ans+=cnt-1;
                cnt=1;
                flag=now;
            }
        }
    }
    ans+=cnt-1;
    cout<<ans<<'\n';
    return ;
}
signed main ()
{
    cin>>t;
    while (t--)
    {
        solve();
    }
    system ("pause");
    return 0;
}

B

B

这个题大意是对于给出的一个n,我们可以得到一个排序,那么我们可以得到一个数组有原来的排序和翻转过来的排序,然后我们问n个数得到的n个阶乘个的排序得到的n个阶乘的数组的逆序对的个数

我发现不管是哪一种排序

对于一个数x,可以找到2(x-1)个逆序对,那么一个排序就有2(1+2+3+。。。+n-1)

这里我们可以用数组求和

s=2(n-1)(1+n-1)

总共有n的阶乘个排序

然后再乘

记得取模

#include <iostream>
using namespace std;
const int maxn=1e5+10;
#define int long long 
const int mod=1e9+7;
int t,n;
int f[maxn];
void init()
{
    f[1]=1;
    for (int i=2;i<=1e5+5;i++)
    {
        f[i]=(f[i-1]*i)%mod;
    }
    return ;
}
void solve()
{
    cin>>n;
    int ans=f[n];
    ans=(ans*n)%mod;
    ans=(ans*(n-1))%mod;
    cout<<ans<<'\n';
    return ;
}
signed main ()
{
    cin>>t;
    init();
    while (t--)
    {
        solve();
    }
    system ("pause");
    return 0;
}

C

C

这个题是给你n个数,a[i]可以解决是a[i]的因子的数

我们需要解决1到m的数,那么我们可以选择任意个数,去解决1到m的数,我们要让我们选的数的差值最小

我们需要排序,我们只需要最大的那个数,最小的那个数,然后中间的数我们有没有都对答案没区别,为了更好的解决这m个数,我们让这种间的数都放进去

这个我们一个一个遍历l,找到最小的r,然后找最小的那个差值

这里的一个判断解决m个问题,和放a[i]可以解决那些问题,很撤销a[i]后解决问题会不会变少

这里的具体操作看代码

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;
int n,m;
vector<int>factor[maxn];
int cnt[maxn];
int a[maxn],tot;
void del(int x)
{
    for (int d:factor[x])
    {
        if (d>m) break;
        --cnt[d];
        if (!cnt[d]) ++tot;//只有删除完所有相关的因子,那么这一个数就算删除了
    }
    return ;
}
void add(int x)
{
    for (int d:factor[x])
    {
        if (d>m) break;
        if (!cnt[d])//只有cnt[d]==0才算是算一个,其他的因为每个数只能算一次
        {
            --tot;
        }
        cnt[d]++;
    }
}
void solve()
{
    cin>>n>>m;
    for (int i=1;i<=m;i++)
    {
        cnt[i]=0;
    }
    for (int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+1+n);
    int ans=1e9;
    int l=1,r=0;
    tot=m;
    for (;l<=n;l++)
    {
        while (r<n&&tot!=0)
        {
            add(a[++r]);
        }
        if (tot>0) break;
        ans=min(ans,a[r]-a[l]);
        del(a[l]);
    }
    if (ans==1e9)
    {
        cout<<-1<<'\n';
    }
    else cout<<ans<<'\n';
    return ;
}
void init()
{
    for (int i=1;i<=1e5;i++)
    {
        for (int j=i;j<=1e5;j+=i)
        {
            factor[j].push_back(i);
        }
    }
    return ;
}
signed main ()
{
    int t;
    cin>>t;
    init();
    while (t--)
    {
        solve();
    }
    system ("pause");
    return 0;
}

标签:845,include,int,cnt,Codeforces,maxn,ByteRace,ans,排序
From: https://www.cnblogs.com/righting/p/17087470.html

相关文章

  • 2.2 vp Codeforces Round #848 (Div. 2)
    A-FlipFlopSum题意给出长度为n的序列a,a中只包括1和-1,你必须操作一次,让相邻两个元素由1变-1或由-1变1,问操作后数组总和最大多少思路暴力即可voidsolve(){ intn......
  • Codeforces Round #848 (Div. 2)
    题目链接A核心思路按照题目模拟就好了//Problem:A.FlipFlopSum//Contest:Codeforces-CodeforcesRound#848(Div.2)//URL:https://codeforces.com/cont......
  • Codeforces Round #831 (Div. 1 + Div. 2) A-H
    CodeforcesRound#831(Div.1+Div.2)A-H题解好久之前写的,发现忘记发了比赛题目质量高,推荐!传送门CodeforcesRound#831(Div.1+Div.2)A.FactoriseN+M题......
  • Codeforces Round #848 (Div. 2) A~C
    A.给出一个-1,1序列,操作是交换相邻位置的符号,一定要操作一次,求最大sum。只有4种情况,扫一遍判断是否出现即可。B题面写得真清楚啊:)#include<bits/stdc++.h>usingnam......
  • Codeforces Round #848 (Div. 2) A~F 题解
    A.FlipFlopSum能换\(-1,-1\)就换,不能能换\(1,-1\)或\(-1,1\)也可以,否则只能换\(1,1\)。B.TheForbiddenPermutation如果原序列一开始就是好的那就结束,否则......
  • Codeforces1778E 【线性基】【换根DP】
    我,差30秒,就能AC这道题,我知道错哪的时候是倒计时30,不记得优先级想赌一把,因为我没有先写括号的习惯,所以倒计时14的时候交了,然后想改了括号再交一发,改了括号之后,比赛结束了。......
  • Educational Codeforces Round 135 (Rated for Div. 2) F. Fishermen 二分图最小带权
    不用真的建图,真的建图两人之间的代价不好算。等价转化为对给定的ai找出bi,使得bi=k*a[i],且互不相同k的上界为n,简易证明:[若a[i]互不相等,全部选a[i]*n会比a[i]*(n+1)更好;......
  • 2.1 vp Codeforces Round #842 (Div. 2)
    A-GreatestConvex题意给出k,要找出最大的x(1<=x<=k),使x!+(x-1)!是k的倍数,问是否存在,为多少思路变换一下即可得原式为(x-1)!(x+1),若要满足条件,令x=k-......
  • Educational Codeforces Round 134 (Rated for Div. 2) F - Matching Reduction 最小
    真傻逼Σ(☉▽☉"a——wa23是因为数组开小了,wa53是数组开大了爆了(不能任性随便开了问最少删多少点使得最大匹配数-1这题就考一个结论:最大匹配数==最小点覆盖 所以很......
  • Codeforces Round #843 (Div. 2)
    感觉难度递减?B.GardenerandtheArray题意:给出一个序列,问其是否存在两个不同的子序列,其或运算之和相同。解:题目很友好,直接给出一个数二进制下哪些位为1.如果一个数为......