首页 > 其他分享 >Codeforces Round 882 (Div. 2) A-D

Codeforces Round 882 (Div. 2) A-D

时间:2023-07-07 22:22:47浏览次数:52  
标签:882 ch int Codeforces read maxn ans Div getchar

AThe Man who became a God 

假设sum为 omiga abs(a[i] - a[i -1]) 1 <= i <= n 

只有设置断点的时候,假设设置在t和t-1之间 the value才会减少abs(a[t]-a[t-1]) 

所以把差距最大的几个地方分段就行了

#include<bits/stdc++.h>
using namespace std ;
#define maxn 400100
#define int long long
int read(){
    int ans = 0 , f = 1 ; char ch = getchar() ;
    while ( !isdigit(ch) ){ if( ch == '-' ) f = -1 ; ch = getchar() ; }
    while ( isdigit(ch) ) ans = (ans * 10) + (ch ^ '0') , ch = getchar() ;
    return ans * f ;
}
int t = read() ; 
int a[maxn] , cha[maxn]; 
signed main(){
    while(t--){
        int n = read() , k = read() - 1 , sum = 0 ; 
        for(int i = 1 ; i <= n ; i++){
            a[i] = read() ; 
            sum += cha[i] = abs(a[i] - a[i - 1] ); 
        }
        cha[1] = 0 ; sum -= a[1] ; 
        sort(cha + 1 , cha + 1 + n) ; 
        for(int i = n ; i >= n - k + 1 ; i--)
            sum -= cha[i] ; 
        printf("%lld\n" , sum) ; 
    }
    return 0 ;
 }

BHamon Odyssey 

贪心 在能and到0并且后面也能and成0的地方就断

#include<bits/stdc++.h>
using namespace std ;
#define maxn 400100
#define int long long
int read(){
    int ans = 0 , f = 1 ; char ch = getchar() ;
    while ( !isdigit(ch) ){ if( ch == '-' ) f = -1 ; ch = getchar() ; }
    while ( isdigit(ch) ) ans = (ans * 10) + (ch ^ '0') , ch = getchar() ;
    return ans * f ;
}
int t = read() ;
int a[maxn] , qian[maxn] , hou[maxn] ; 
signed main(){
    while(t--){
        int n = read() ; 
         qian[1] = a[1] = read() ; 
         hou[1] = 0 ; 
        for(int i = 2 ; i <= n ;i++)
            a[i] = read() , qian[i] = qian[i - 1] & a[i] , hou[i] = 0 ; 
        hou[n] = a[n] ; 
        hou[n + 1] = 0 ;
        for(int i = n - 1; i >= 1 ; i--){
            hou[i] = hou[i + 1] & a[i] ; 
        }
        if(qian[n] != 0){
            printf("1\n") ; 
            continue ; 
        }
        int cnt = 0 ; 
        bool flag = 0 ; 
        int sum = 0 ; 
        for(int i = 1 ; i <= n ; i++){
            if(!flag){
                flag = 1 ;
                sum = a[i] ; 
            }
            else {
                sum &= a[i] ; 
            }
            if(sum == 0){
                if(hou[i + 1] == 0){
                    cnt++ ; 
                    flag = 0 ; 
                }
            }
        }
            
        printf("%lld\n" , cnt) ; 
    }
    return 0 ;
 }

CVampiric Powers, anyone?

 模拟之后可以发现其实本质上是找异或之后的最大子段  注意到a最大只有255 所以异或之后也不超过255 利用类似前缀和性质即可 
#include<bits/stdc++.h>
using namespace std ;
#define maxn 400100
#define int long long
int read(){
    int ans = 0 , f = 1 ; char ch = getchar() ;
    while ( !isdigit(ch) ){ if( ch == '-' ) f = -1 ; ch = getchar() ; }
    while ( isdigit(ch) ) ans = (ans * 10) + (ch ^ '0') , ch = getchar() ;
    return ans * f ;
}
int t  = read() ; 
int cnt[500] ; 
signed main(){
    while(t--){
        int n = read() ; 
        for(int  i = 0 ; i < 256 ; i++) cnt[i] = 0 ; 
        int sum = 0 , ans = 0 ; 
        cnt[0] = 1 ; 
        for(int i = 1 ; i <= n ; i++){
            sum ^= read() ; 
            cnt[sum]= 1 ; 
            for(int i = 0 ; i < 256 ; i++)
                if(cnt[i]) ans = max(ans , sum ^ i) ; 
        }
        printf("%lld\n" , ans) ; 
    }
    return 0 ;
 }

Professor Higashikata

 以第二个样例为例模拟一遍 

 

#include<bits/stdc++.h>
using namespace std ;
#define maxn 300100
#define int long long
int read(){
    int ans = 0 , f = 1 ; char ch = getchar() ;
    while ( !isdigit(ch) ){ if( ch == '-' ) f = -1 ; ch = getchar() ; }
    while ( isdigit(ch) ) ans = (ans * 10) + (ch ^ '0') , ch = getchar() ;
    return ans * f ;
}
#define fi first 
#define se second 
int n , m , q ; 
pair<int,int> ques[maxn] , pi[maxn]; 
int val[maxn << 2] , a[maxn] , s[maxn] , sum , tag[maxn << 2]; 
char in[maxn] ; 
void pushdown(int o){
    tag[o << 1]= tag[o << 1 | 1]= tag[o] ; 
    val[o << 1 | 1] = val[o << 1] = tag[o] ; 
    tag[o] = 0 ; 
    return ; 
}
#define lr o << 1 
#define rr ((o << 1) | 1)
#define mid ((l + r) >> 1)
void modify(int o , int l , int r , int ql , int qr , int k){
    if(l > qr || r < ql) return ; 
    if(tag[o]) pushdown(o) ; 
    if(l >= ql && r <= qr){
        tag[o] = val[o] = k ; 
        return ; 
    }
    modify(lr , l , mid , ql , qr , k) ;
    modify(rr , mid + 1 , r , ql , qr , k ) ; 
    return ; 
}
int que(int o , int l , int r , int poss){
    if(tag[o]) pushdown(o) ; 
    if(l == r) return val[o] ; 
    if(poss <= mid) return que(lr , l , mid , poss) ; 
    else return que(rr , mid + 1 , r  , poss) ; 
}
int c[maxn] ; 
#define lowbit(x) (x & (-x))
void add(int pos , int x ){
    while(pos <= n){
//        printf("pos : %lld x : %lld \n" , pos , x) ; 
        c[pos] += x ; 
        pos += lowbit(pos) ; 
    }
    return ; 
}
int query(int pos){
    int suum = 0 ; 
    while(pos){
        suum += c[pos] ; 
        pos -= lowbit(pos) ; 
    }
    return suum ; 
}
vector<int> G[maxn] ; 
signed main(){
    n = read() ; m = read() , q = read() ; 
    scanf("%s" , in + 1) ;  
    for(int i = 1 ; i <= n ; i++)
        sum += s[i] = in[i] - '0' ; 
    for(int i = 1 ; i <= m ; i++)
        ques[i] = {read() , read()} ; 
    for(int i = m ; i >= 1 ; i--){
        modify(1 , 1 , n , ques[i].fi , ques[i].se , i) ; 
    }
    for(int i = 1 ; i <= n ; i++){
        a[i] = que(1 , 1 , n , i) ; 
//        printf("a[%lld] : %lld \n" , i , a[i]) ; 
        G[a[i]].push_back(i) ; 
    }
    int num = 0 ; 
    for(int i = 1 ; i <= m ; i++){
        if(G[i].size()){
            for(int j = 0 ; j < G[i].size() ; j++){
                int v = G[i][j] ; 
                a[v] = ++num ; 
//                printf("a[%lld] : %lld \n" , v , a[v]) ; 
                add(num , s[v]) ; 
            }
        }
    }
//    printf("num : %lld \n" , num) ; 
    while(q--){
        int x = read() ; 
        if(s[x] == 1){
            s[x] = 0 ; 
            sum-- ; 
            if(a[x] != 0){
                add(a[x] , -1) ; 
            }
        }
        else {
            s[x] = 1  ;
            sum++ ; 
            if(a[x] != 0)
                add(a[x] , 1) ;// printf("yes"); 
        }//
        printf("%lld\n" , min(sum , num ) - query(min(sum , num)) ) ; 
    }
    return 0 ;
 }

 

 

 

 

标签:882,ch,int,Codeforces,read,maxn,ans,Div,getchar
From: https://www.cnblogs.com/Vellichor/p/17536181.html

相关文章

  • Educational Codeforces Round 151 (Rated for Div. 2) D. Rating System
    贪心由题可得,对于k的选择一定是单调递增的,对于前面选定的k后面选的k必须大于之前选的才会发生新的变化,因此k的选择其实是一个单调栈,由前缀和组成我们要想最后的结果最大,则k值一定要尽可能的高,例如当选中i为k值时,如果从i后面某个原本的前缀和要大于选k之后所得到的前缀和的话,说明......
  • Educational Codeforces Round 151 (Rated for Div. 2) C. Strong Password
    题目翻译,给定t组数据,每组数据包含一个字符串s,两个长度为m的字符串l和r,要求判断是否存在一个长度为m的字符串res,满足l[i]<=res[i]<=r[i](i->0~m)且不是s的子序列贪心首先对于所有满足l<res<r的字符串,我们只需判断是否存在一个字符串不是子序列即可,那么我们让res成为子序列的可能......
  • Codeforces Round 879 (Div. 2)
    Preface补题其实这场题目昨天基本就写好了,但因为昨天晚上有CF所以博客就先没写,鸽到今天才补这场的难度只能说有点过于简单了,D之前都是一眼题,E最近学校那边做过类似的题目,F读懂题意后想到关键后也是个丁真题A.UnitArray为了偷懒我就直接枚举最后有多少个\(-1\)了#include<......
  • Effective Diversity in Population-Based Reinforcement Learning
    发表时间:2020(NeurIPS2020)文章要点:这篇文章提出了DiversityviaDeterminants(DvD)算法来提升种群里的多样性。之前的方法通常都考虑的两两之间的距离,然后设计一些指标或者加权来增加种群多样性,这种方式容易出现cycling,也就是类似石头剪刀布的循环克制的关系,造成训练不上去,......
  • CodeForces 1142E Pink Floyd
    洛谷传送门CF传送门感觉很神奇啊,想了挺久的。如果没有粉色边是容易的。竞赛图中,强连通分量缩点后,拓扑序较小的点向每一个拓扑序比它大的点连边。利用这个性质,我们维护一个答案集合\(S\),当\(|S|\ge2\)时,每次随意取出两个点\(u,v\inS\)。如果边的方向是\(u\tov\)......
  • 题解-Codeforces Round 805 (Div. 3) E. Split Into Two Sets
    题解-CodeforcesRound805(Div.3)E.SplitIntoTwoSets(原题链接)[Problem-E-Codeforces]思路知识点:种类并查集网上关于种类并查集的教学已经很多,在此不赘述在理解种类并查集时,很多文章会提到“敌人”,“朋友”的概念。而在不同的题目中,互为“敌人”,“朋友”的两个......
  • 【补题记录】 Codeforces Round 797 (Div. 3) F Shifting String(置换环)
    CodeforcesRound797(Div.3)FShiftingString思路:根据这个排列进行替换的操作可以往置换环考虑,就是对于每一段字串,它的变换都是有规律的,经过一定的操作之后都会回到原点,可以想象转化成图上问题。参考ygg的题解,直接用链表模拟这个转化的过程,然后暴力计数,因为要满足所有点都......
  • Codeforces Round #222 (Div. 1) B - Preparing for the Contest
    先二分,输入排序,然后对于确定的天数,贪心判断是否可行。#include<iostream>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<cstdio>#include<algorithm>#include<cstring>#include<......
  • 【后缀自动机】Codeforces Round #305 (Div. 1) E. Mike and Friends
    对所有的串加特殊字符隔开,单串建立后缀自动机。然后将每个的fa边反向建树,对树dfs得到dfs序,对dfs序建立线段树。询问离线,每个询问拆成1-(l-1)和1-r。。。按端点排序,然后每次加入线段树,查询k对应的节点的子树和。。。#include<iostream>#include<queue>#include<stack>#include......
  • codeforces 540D - Bad Luck Island
    记忆化搜索...#include<iostream>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<cstdio>#include<algorithm>#include<cstring>#include<climits>#include......