首页 > 其他分享 >Codeforces Round 970 (Div. 3)A~F

Codeforces Round 970 (Div. 3)A~F

时间:2024-09-26 10:24:50浏览次数:1  
标签:const 970 int ll nullptr cin Codeforces Div mod

Codeforces Round 970 (Div. 3)A~F

A. Sakurako's Exam

把1的个数和2的个数按奇偶分类讨论即可。

// AC one more times
// nndbk
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;

int main()
{
    ios::sync_with_stdio(false);   cin.tie(nullptr), cout.tie(nullptr);
    int t; cin>>t;
    while(t--)
    {
        int a,b; cin>>a>>b;
        if(a % 2){
            cout<<"NO\n";
            continue;
        }

        if(a % 2 == 0 && b % 2 == 0) cout<<"YES\n";
        else{
            if(a >= 2)
                cout<<"YES\n";
            else cout<<"NO\n";
        }
    }


    return 0;
}

B. Square or Not

映射到矩阵暴力判断就行了。

// AC one more times
// nndbk
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;

int main()
{
    ios::sync_with_stdio(false);   cin.tie(nullptr), cout.tie(nullptr);
    set<int>st;
    int x = 1;
    while(x < N){
        st.insert(x*x);
        x++;
    }

    int t; cin>>t;
    while(t--)
    {
        int n; cin>>n;
        string s; cin>>s;
        if(st.find(n) == st.end()){
            cout<<"No\n";
            continue;
        }

        int m = sqrt(n);
        s = "?" + s;
        char a[m+10][m+10];
        int idx = 0;
        for(int i = 1;i <= m; i++)
        {
            for(int j = 1;j <= m; j++){
                a[i][j] = s[++idx];
            }
        }

        // for(int i = 1;i <= m; i++)
        // {
        //     for(int j = 1;j <= m; j++){
        //         cout<<a[i][j];
        //     }
        //     cout<<"\n";
        // }
        // cout<<"\n";

        bool ok = true;
        for(int i = 1;i <= m; i++)
        {
            if(a[i][1] != '1' || a[i][m] != '1')ok = false;
            if(a[1][i] != '1' || a[m][i] != '1')ok = false;
            if(!ok)break;
        }


        for(int i = 2;i <= m-1; i++)
        {
            for(int j = 2;j <= m-1; j++)
            {
                if(a[i][j] != '0'){
                    ok = false;
                    break;
                }
            }
            if(!ok)break;
        }

        if(!ok)cout<<"No\n";
        else cout<<"Yes\n";

    }


    return 0;
}

C. Longest Good Array

发现是个等差,二分求最多能到哪。

// AC one more times
// nndbk
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;

int main()
{
    ios::sync_with_stdio(false);   cin.tie(nullptr), cout.tie(nullptr);
    int t; cin>>t;
    while(t--)
    {
        ll L,R; cin>>L>>R;
        ll l = 1,r = R-L+1;
        while(l <= r)
        {
            ll mid = (l + r)>>1;
            if((1+mid)*mid/2 >= R-L+1)r = mid - 1;
            else l = mid + 1;
        }
        cout<<r + 1<<"\n";
    }


    return 0;
}

D. Sakurako's Hobby

思路:因为是个排列,那么就不会有多个环连起来的情况。我们画个图就能发现,在一个环上的都可相互到达。那么连通性可以用并查集来维护。

// AC one more times
// nndbk
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;



int val[N],p[N],siz[N];

int find(int x) {
    return p[x] == x ? x : p[x] = find(p[x]);
}

bool merge(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return false;
    siz[x] += siz[y];
    p[y] = x;
    return true;
}


int main()
{
    ios::sync_with_stdio(false);   cin.tie(nullptr), cout.tie(nullptr);
    
    int t; cin>>t;
    while(t--)
    {
        int n; cin>>n;
        for(int i = 1;i <= n; i++)
        {
            int x; cin>>x;
            val[i] = x;
            p[i] = i;
            siz[i] = 1;
        }
        string s; cin>>s;

        s = "?"+s;
        for(int i = 1;i <= n; i++)
        {
            if(p[i] == i){
                bool notself = false;
                int last = i,now = val[i];
                while(1)
                {
                    if(now == i){
                        break;
                    }
                    last = now,now = val[now];
                    // cout<<"last = "<<last<<" now = "<<now<<"\n";
                    
                    merge(last,now);
                }
            }
        }

        map<int,int>mp;
        for(int i = 1;i <= n; i++)
        {
            if(s[i] == '0')//black
            {
               mp[p[i]]++;
            }
        }

        for(int i = 1;i <= n; i++)
        {
            cout<<mp[p[i]]<<" ";
        }
        cout<<"\n";

    }


    return 0;
}

E. Alternating String

思路:因为要求长度是偶数,且奇数位相同,偶数位相同。且删数操作只能最多一次,那么肯定是长度奇数的时候必须要用删数,长度偶数只能进行改数。

我们注意到如果是偶数长度,只考虑该数。统计哪个字母出现最多就行了。

如果是奇数呢?考虑先删再改。删哪个呢?我们枚举删每一个点的代价,且注意到这个被删的点之后的奇偶性发生互换,写的时候要小心边界。

// AC one more times
// nndbk
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;


char a[N],b[N];
int cnta[N][30],cntb[N][30];
int main()
{
    ios::sync_with_stdio(false);   cin.tie(nullptr), cout.tie(nullptr);
  
    int t; cin>>t;
    while(t--)
    {
        int n; cin>>n;
        string s; cin>>s;
        if(n == 1){
            cout<<1<<"\n";
            continue;
        }

        s = "?" + s;
        int n1 = 0,n2 = 0;
        memset(cnta,0,sizeof(a));
        memset(cntb,0,sizeof(b));

        for(int i = 1;i <= n; i++)
        {
            if(i % 2)a[++n1] = s[i];
            else b[++n2] = s[i];
        }

        // for(int i = 1;i <= n1; i++)
        //     cout<<a[i];
        // cout<<"\n";

        // for(int i = 1;i <= n2; i++)
        //     cout<<b[i];
        // cout<<"\n";

        // cout<<"____________________\n";

        for(int i = 1;i <= n1; i++){
            for(int j = 0;j < 26; j++){
                cnta[i][j] = cnta[i-1][j];
            }
            // cout<<"a[i] - 'a' ="<<a[i]-'a'<<"\n";
            cnta[i][a[i]-'a']++;
            // for(int j = 0;j < 26; j++){
            //     cout<<cnta[i][j]<<" ";
            // }
            // cout<<"\n";
        }

        for(int i = 1;i <= n2; i++){
            for(int j = 0;j < 26; j++){
                cntb[i][j] = cntb[i-1][j];
            }
            cntb[i][b[i]-'a']++;
        }

        // cout<<"n = "<<n<<"\n";
        // for(int i = 0;i < 26; i++)
        //     cout<<cnta[n1][i]<<" ";
        // cout<<"\n";
        // for(int i = 0;i < 26; i++)
        //     cout<<cntb[n2][i]<<" ";
        // cout<<"\n";

        if(n % 2 == 0){
            int ans1 = 0,ans2 = 0;
            for(int i = 0;i < 26; i++)
                ans1 = max(ans1,cnta[n1][i]);
            ans1 = n1-ans1;
            for(int i = 0;i < 26; i++)
                ans2 = max(ans2,cntb[n2][i]);
            ans2 = n2-ans2;
            cout<<ans1+ans2<<"\n";
        }else{
            n--;
            int res = 1e9;
            int ans1 = 0,ans2 = 0;

            for(int i = 1;i <= n1; i++){//删奇数位置
                 // cout<<"i = "<<i<<"\n";
                ans1 = 0,ans2 = 0;
                for(int j = 0;j < 26; j++){
                    // cout<<"j = "<<j<<"\n";
                    
                    ans1 = max(ans1,cnta[i-1][j] + cntb[n2][j]-cntb[i-1][j]);
                    ans2 = max(ans2,cntb[i-1][j] + cnta[n1][j]-cnta[i][j]);

                    // cout<<"cnta[i-1][j] = "<<cnta[i-1][j]<<" cntb[n2][j]-cntb[i-1][j] = "<<cntb[n2][j]-cntb[i-1][j]<<"\n";
                    // cout<<"cntb[n2][j]="<<cntb[n2][j]<<" cntb[i-1][j]="<<cntb[i-1][j]<<"\n";
                    // cout<<"ans1 = "<<ans1<<" ans2 = "<<ans2<<"\n";
                }

                ans1 = n/2-ans1;
                ans2 = n/2-ans2;
                // cout<<"i = "<<i<<" ans1 = "<<ans1<<" ans2 = "<<ans2<<"\n";
                res = min(res,ans1+ans2);
            }

            ans1 = 0,ans2 = 0;
            for(int i = 1;i <= n2; i++){//删偶数位置
                ans1 = 0,ans2 = 0;
                
                for(int j = 0;j < 26; j++){
                    ans1 = max(ans1,cnta[i][j] + cntb[n2][j]-cntb[i][j]);
                    ans2 = max(ans2,cntb[i-1][j] + cnta[n1][j]-cnta[i][j]);
                }

                ans1 = n/2-ans1;
                ans2 = n/2-ans2;
                // cout<<"i = "<<i<<" ans1 = "<<ans1<<" ans2 = "<<ans2<<"\n";
                res = min(res,ans1+ans2);
            }


            cout<<res+1<<"\n";
        }
    }

    return 0;
}

F. Sakurako's Box

思路:不难发现答案是:\(\dfrac{\sum_{i = 1}^{n-1}\sum_{j = i+1}^na_ia_j}{\dfrac{n(n+1)}{2}}\)

做一个化简:\(\dfrac{2\times\sum_{i = 1}^{n-1}a_i\sum_{j = i+1}^na_j}{n(n+1)}\)

那么可以对后面的\(\sum_{j = i+1}^na_j\)做一个前缀和的预处理。然后算就行了。取模要千万小心!当然你也可以直接用python写。

// AC one more times
// nndbk
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
ll qmi(ll a, ll b, ll mod)
{
    ll ans = 1 % mod;
    a %= mod;
    while(b)
    {
        if(b & 1) ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}


ll a[N];
ll s[N];
ll n;
int main()
{
    ios::sync_with_stdio(false);   cin.tie(nullptr), cout.tie(nullptr);
    int t; cin>>t;

    while(t--)
    {
        cin>>n;
        ll inv = qmi(n*(n-1),mod-2,mod);

        for(int i = 1;i <= n; i++)
            cin>>a[i];

        for(int i = 1;i <= n; i++)
        {
            s[i] = (s[i-1]+a[i])%mod;
        }

        __int128 ans = 0;
        for(int i = 1;i <= n-1; i++){
            ll t = (s[n]-s[i])%mod;
            if(t < 0) t += mod;
            t %= mod;
            t *= a[i];
            ans = ans + t;
            ans %= mod;
        }

        ans *= 2ll;
        ans %= mod;
        ans *= inv;
        ans %= mod;
        
        cout<<(ll)ans<<"\n";

    }



    return 0;
}

标签:const,970,int,ll,nullptr,cin,Codeforces,Div,mod
From: https://www.cnblogs.com/nannandbk/p/18432943

相关文章

  • Codeforces Round 964 (Div. 4)A~G1
    CodeforcesRound964(Div.4)A~G1A.A+BAgain?签到//AConemoretimes//nndbk#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintmod=1e9+7;constintN=2e5+10;intmain(){ios::sync_with_stdio(false);ci......
  • Codeforces Round 973 (Div. 2)
    C.PasswordCracking(C)若字符串只向一个方向延伸,则一定能通过\(2n\)次询问获得结果;可以先向后猜测,当该侧继续添加字符1与0都不符合要求时、说明已经到达字符串末端,继续询问前缀即可。voidsolve(){intn;cin>>n;strings="";boolflag=0;w......
  • Codeforces Round 971 (Div. 4)A~G1
    CodeforcesRound971(Div.4)A~G1A.Minimize!签到不多说。//AConemoretimes//nndbk#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintmod=1e9+7;constintN=2e5+10;intmain(){ ios::sync_with_stdio(false......
  • Codeforces Round 974 (Div. 3)题解记录
    A.RobinHelps签到模拟,遍历一遍即可,注意没钱时不给钱。\(O(n)\)#include<iostream>#include<set>#include<map>#include<vector>#include<algorithm>#include<bitset>#include<math.h>#include<string>#include<string.h>#......
  • Codeforces Round 959 (Div. 1 + Div. 2) C. Hungry Games题解
    CodeforcesRound959(Div.1+Div.2)C.HungryGames题解题目链接大致题意:给定一个长度为n的数组,并且给出一对l,r表示一个区间,如果∑i......
  • Codeforces Round 974 (Div.3) 题解
    CodeforcesRound974(Div.3)题解A.RobinHelps模拟按照题意模拟即可。voidShowball(){intn,k;cin>>n>>k;intcur=0,ans=0;for(inti=0;i<n;i++){intx;cin>>x;if(x>=k)cur+=x;elseif(!x){if(cur>=1)cu......
  • codeforces round 974(div.3) D(学会灵活拆分数据)
    解题历程:首先想到的是用数组记录,遍历每一个任务的区间,对区间内的数值加1,比如对于发生在4和8天之内的任务,a[4]++,a[5]++……a[8]++。然后用双指针,记录持续天数的开始下标和结束下标,以l和l+d为边界的窗口遍历每一天,若是最高位寻找任务最多的一天,和区间最大值最小的一天。后来发现......
  • p标签不能嵌套div,h1~h6,p,如果嵌套浏览器会如何解析
    有时候做项目会不小心用p嵌套div,发现控制不了样式,我们放到最后去讲p嵌套div的问题首先,我们先用p标签来嵌套h1~h6,这里我选择h1(h1~h6测试结果都一样),上代码及效果图让我们看下浏览器如何解析我们发现,浏览器把h1标签给单独摘出来了,并且多了个p标签,导致这样的原因:看代码图:首......
  • Codeforces Round 973 (Div. 2)
    A.Zhan'sBlender有\(n\)个水果,每秒可以消耗\(\min\{x,y\}\)个,问需要多少时间。整数除法的上取整操作即可。点击查看代码#include<bits/stdc++.h>usingnamespacestd;#definelllonglong#defineullunsignedlonglongintread(){intx=0;boolf=......
  • Codeforces Round 972(Div.2)题解
    CodeforcesRound972(Div.2)题解A.SimplePalindrome贪心贪心,尽可能元素数量平均,并且相同字母放在一起。#include<bits/stdc++.h>usingnamespacestd;#definefffirst#definesssecond#definepbpush_back#defineall(u)u.begin(),u.end()#defineendl'\n'#de......