首页 > 其他分享 >Codeforces Round 979 (Div. 2)

Codeforces Round 979 (Div. 2)

时间:2024-10-21 14:44:27浏览次数:1  
标签:cnt int texttt Codeforces 979 -- solve Div include

Codeforces Round 979 (Div. 2) 总结

A

首先第一位的贡献一定是 \(0\),然后考虑接下来 \(n-1\) 位,我们可以把最大值和最小值放在第一位和第二位,这样贡献就是 \((max-min) \times (n-1)\)。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
const int N=1005;
int n;
void solve()
{
    int mx=0,mi=N;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        mx=max(mx,x);
        mi=min(mi,x);
    }
    cout<<(mx-mi)*(n-1)<<'\n';
}
int main ()
{
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif 
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}

B

因为不同位置的 \(1\) 和 \(0\) 组成的子序列算作不同的,观察可知,当只有一个 \(1\) 时最小,为 \(1\)。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>

using namespace std;
int n;
void solve()
{
    cin>>n;
    cout<<1;
    for(int i=1;i<n;i++) cout<<0;
    cout<<'\n';
}
int main ()
{
    #ifndef ONLINE_JUDGE 
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}

C

充分理解下题意,当 \(1\) 的左右都是 \(0\) 时,才能用 \(and\) 操作消掉 \(1\)。所以如果开头或结尾是 \(1\),或者有连续的两个 \(1\),输出 YES

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
const int N=2e5+5;
int n;
string s;

void solve()
{
    cin>>n>>s;
    s="0"+s;
    int st=0;
    if(s[1]=='1'||s[n]=='1') st=1;
    for(int i=1;i<n;i++) if(s[i]=='1'&&s[i+1]=='1') st=1;
    if(st) puts("YES");
    else puts("NO");
}
int main ()
{
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    #endif 
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}

D

首先 \(\texttt{L}\) 和 \(\texttt{R}\) 相当于左移和右移,以及我们只需要考虑右移(或左移)即可。

对于每个形如 \(\texttt{RRR} \dots \texttt{LLL} \dots\) 的子串 \([l,r]\),其中的 \(p_i\)(\(l \le i \le r\)) 显然可以到达 \([l,r]\) 中的任一位置,也就是说我们可以任意安排 \([l,r]\) 的顺序。
可知 \(s\) 由多个上述子串构成。

而对于 \(1 \le i < n\),若 \(s_i=\texttt{L}\) 且 \(s_{i+1}=\texttt{R}\),都会在 \(i\) 处形成一个断点。也就是说 \([1,i]\) 中的数都不可能右移到 \([i+1,n]\) 的位置。因为只考虑右移,我们只考虑最大值,序列要合法就必须所有的 \([l,r]\) 的最大值小于等于 \(r\)。

因此我们考虑每个 \(\texttt{L} \texttt{R}\),下标为 \(i\) 和 \(i+1\),如果 \([1,i]\) 的最大值大于 \(i\),就说明不可以实现。统计这样不合法的 \(\texttt{L} \texttt{R}\) 的个数 \(cnt\),等于 \(0\) 输出 YES

最后思考修改后会有什么影响,就是看看改之前和改之后是不是 \(\texttt{L} \texttt{R}\),更新 \(cnt\) 即可。

我用的是 ST 表维护区间最值。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>

using namespace std;
typedef long long ll;
const int N=2e5+5;
int n,q;
int log_2[N];
int a[N];
string s;
int f[N][25];
void init()
{
    for(int i=1;i<=n;i++) f[i][0]=a[i];
    for(int j=1;j<=20;j++) 
        for(int i=1;i+(1<<j)-1<=n;i++)
            f[i][j]=max(f[i][j-1],f[i+(1<<j-1)][j-1]);
}
int query(int l,int r)
{
    int t=log_2[r-l+1];
    return max(f[l][t],f[r-(1<<t)+1][t]);
}
bool check(int x)
{
    return query(1,x)>x;
}

void solve()
{
    cin>>n>>q;
    for(int i=1;i<=n;i++) cin>>a[i];
    init();
    cin>>s;
    s='0'+s;
    int cnt=0;
    for(int i=2;i<n;i++)
        if(s[i]=='L'&&s[i+1]=='R') 
            if(check(i))
                cnt++;
    while(q--)
    {
        int x;
        cin>>x;
        if(s[x]=='L')
        {
            s[x]='R';
            if(s[x+1]=='R') if(check(x)) cnt--;
            if(s[x-1]=='L') if(check(x-1)) cnt++;
        }
        else 
        {
            s[x]='L';
            if(s[x-1]=='L') if(check(x-1)) cnt--;
            if(s[x+1]=='R') if(check(x)) cnt++;
        }
        if(!cnt) puts("YES");
        else puts("NO");
    }
}
int main ()
{
    for(int i=2;i<=N-5;i++) log_2[i]=log_2[i/2]+1;
    int T;
    cin>>T;
    while(T--) solve();
    return 0;
}

标签:cnt,int,texttt,Codeforces,979,--,solve,Div,include
From: https://www.cnblogs.com/zhouruoheng/p/18489412

相关文章

  • 「题解」Codeforces Round 979 (Div. 2)
    before\(A\simD\)的题解A.AGiftFromOrangutanProblemA.AGiftFromOrangutanSol&Code\(c_i-b_i\)最大就是\(a\)的最大值减最小值。将\(a\)的最大值\(x\)和最小值\(y\)放到头两位即可得到答案\((x-y)(n-1)\)。#include<bits/stdc++.h>#defineM......
  • Codeforces Round 980 (Div. 2) C. Concatenation of Arrays
    题目:给定n个数组a1,a2,…,an。每个数组的长度都是2。因此,ai=[ai,1,ai,2]。你需要将这些数组连接成一个长度为2n的单一数组,以便使结果数组中的逆序数最小。注意,你不需要实际计算逆序的数量。更正式地说,你需要选择一个长度为n的排列p,使得数组b=[ap1,1,ap1,2,......
  • CF round 979 div2(D-E)
    D容易观察到需要连续一段区间。这不单点修改区间查询,然后我思维就开始往线段树飘了。。。并且我到这里就以为做完了开始想实现,实际上性质都没观察准确。。但是因为这是一个1500的题所以显然有不用线段树的解。题解是差分做的,确实差分也可以操作区间观察到“LR”一定是隔断点,那......
  • Codeforces Round 979 div2 个人题解(A~E)
    CodeforcesRound979div2个人题解(A~E)Dashboard-CodeforcesRound979(Div.2)-Codeforces火车头#define_CRT_SECURE_NO_WARNINGS1#include<algorithm>#include<array>#include<bitset>#include<cmath>#include<cstdio>#inc......
  • Codeforces Round 979 (Div. 2) C. A TRUE Battle
    题目链接:题目大意:Alice和Bob先后轮流向一串01字符串中加入or或and,前者想让结果为1后者想让结果为0,问Alice能不能赢。思路:对于相同的两个布尔值,or和and都不能改变它们,而对于Alice,他倾向于向01之间加入or,Bob则想加入and。一开始我想比较1和0的多少不就行了吗,但是不对,当你......
  • Codeforces Round 979 (Div. 2) B. Minimise Oneness
    题目链接:题目大意:构造长度为nnn的01字符串,使得全为零的子序列和至少有一个1的子序列的数量之差的绝对值最小。思路:很明显,所有子序列中不是全为0就是至少有一个1,所以算......
  • Codeforces Round 980 (Div. 2)
    糖丸了,什么沟史比赛A.ProfitableInterestRate初始有\(a\)个硬币,可以花费硬币开通盈利账户与非盈利账户开通盈利账户需要至少花费\(b\)个金币开通非盈利账户没有限制每在非盈利账户花费\(x\)元,盈利账户的限制\(b\)就减少\(2x\)元求最大的在盈利账户上的花......
  • Codeforces Round 977 (Div. 2)
    一万一参赛,赛时排名151A.MeaningMean简单贪心题。显然,排在越后面的数,除以2的次数越少。因此贪心地从小到大计算结果即为答案。#include<bits/stdc++.h>usingnamespacestd;constintN=55;intT,n,a[N];intmain(){ scanf("%d",&T); while(T--){ scanf......
  • Codeforces Round 977 (Div. 2, based on COMPFEST 16 - Final Round) A-C1
    ​A.MeaningMean2024.10.17算法:模拟,贪心思路:居然时没看题解直接做出来的T^T贪心:题目要求最后剩下的一个数,使得最大那么我们从这个最大的最后一个数思考,最后一个数肯定时由最后两个数进行相加,再除以2,同时上下取整而得到的。方便陈述,我们设最大的最后一个数,也就是最终答案......
  • 【并查集+dfs】codeforces 1833 E. Round Dance
    题意输入一个正整数\(T(1\leqT\leq10^4)\),表示接下来输入\(T\)组测试用例,对于每一个测试用例:第一行,输入一个正整数\(n(2\leqn\leq2*10^5)\)第二行,输入\(n\)个正整数\(a_i(1\leqa_i\leqn)\),表示节点\(i\)到节点\(a_i\)存在一条有向边,保证无自环这\(n......