首页 > 其他分享 >CF1400E Clear the Multiset

CF1400E Clear the Multiset

时间:2023-06-24 19:23:26浏览次数:34  
标签:CF1400E int Clear work suml pile 区间 Multiset 最大值

CF1400E Clear the Multiset

一道经典简单的分治

由贪心可知,对于一段区间[L,R],一共有两种处理方式

1.一个一个减,次数为l-r+1

2.先区间减,直到最小的减没了,在考虑最小值隔开的两个区间。如果有多个最小值,其实也不影响,再往下分的时候一定会分开。区间答案就是 $min(l-r+1,f(l,p-1)+f(p+1,r)+minn)$ 具体含义见代码

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int a[N],n;
int work(int l,int r){
    if(l>r) return 0;
    if(l==r) return min(a[l],1);
    int minn=2e9+555,p;
    for(int i=l;i<=r;i++){
        if(minn>a[i]){
            minn=a[i];p=i;
        }
    }
    for(int i=l;i<=r;i++) a[i]-=minn;
    return min(r-l+1,work(l,p-1)+work(p+1,r)+minn);
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    cout<<work(1,n);
    return 0;
}

 Distribution

Cakzy have a sequence a[1...n], and a[i] represents the number of candies in i-th candy pile. And every candy is the same. There are k children around Cakzy. Cakzy want to select an interval [l, r] (1 ≤ l ≤ r ≤ n) in the sequence. He will take away the maxmium pile in the interval [l, r]. Then he will mix the remaining candies of the interval [l, r] into one pile and distribute them equally to the k children. Attention:: One candy can not be divided into several pieces. Cazky wants to know how many intervals can meet his demand. Input The first line contains two integers n (1 ≤ n ≤ 3 × 105 ), m (1 ≤ m ≤ 3 × 106 ). Following one line contains n integers a[i] (1 ≤ a[i] ≤ 10000), denoting the number of candies in i-th pile. Output Output one line containing one decimal, denoting the answer. Example standard input standard output 4 3 1 2 3 4 3 Note In the example test, n = 4, m = 3. The 3 intervals are [1, 3], [1, 4], [3, 4].

一言以蔽之,就是对于一个区间,他合法当且仅当去掉最大值,他的和可以被区间长度整除。问总区间里有多少个子区间符合题意(必须连续)。

考虑分治。对于一个区间,我们只处理横跨中点的情况,左右子区间里的递归下去,这样就可以不重不漏处理所有情况。对于左区间,我们钦定一个最大值,向左扫,并记录后缀和,后缀最大值,然后享有,枚举,直到碰见更大的值,开一个桶,统计各个余数的个数,然后统计答案。同样的方法处理最大值在右区间的情况。注意处理右边之前,左边的情况数要减回来。

// ubsan: undefined
// accoders
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e6+66;
int a[N],ans,cnt[N];
int n,m;
void work(int l,int r){
    if(l==r) return;
    int M=(l+r)>>1;
    work(l,M);work(M+1,r);
    int u=M,mxl=0,suml=0,mxr=0,sumr=0;//只处理横跨左右区间的情况,
                                      //其他情况直接递归下去
    //左:                                  
    for(int i=M;i>=l;i--){//相当于已经钦定了一个最大值
        mxl=max(mxl,a[i]);
        suml+=a[i];//记录后缀和,后缀最大值
        while(u<r&&a[u+1]<mxl){//向右枚举时不能大于钦定的最大值
            u++;
            sumr=(sumr+a[u])%m;
            cnt[sumr]++;//余数为 sumr 的情况数
        }
        ans+=cnt[(m-((suml-mxl)%m))%m];
    }
    sumr=0;
    for(int i=M+1;i<=u;i++){
        sumr=(sumr+a[i])%m;
        cnt[sumr]--;
        //处理右边之前,左边的情况数要减回来
    }
    //右:   与左基本相同
    u=M+1,mxl=0,suml=0,mxr=0,sumr=0;
    for(int i=u;i<=r;i++){
        mxr=max(mxr,a[i]);sumr+=a[i];
        while(u>l&&a[u-1]<=mxr){
            u--;
            suml=(suml+a[u])%m;
            cnt[suml]++;
        }
        ans+=cnt[(m-((sumr-mxr)%m))%m];
    }
    suml=0;
    for(int i=M;i>=u;i--){
        suml=(suml+a[i])%m;
        cnt[suml]--;
    }
}
signed main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    work(1,n);
    cout<<ans;
    return 0;
}

 

标签:CF1400E,int,Clear,work,suml,pile,区间,Multiset,最大值
From: https://www.cnblogs.com/DongPD/p/17501521.html

相关文章

  • glctx.ClearColor 参数说明
    glctx.ClearColor的参数信息如下://ClearColorspecifiestheRGBAvaluesusedtoclearcolorbuffers.////http://www.khronos.org/opengles/sdk/docs/man3/html/glClearColor.xhtmlClearColor(red,green,blue,alphafloat32)这四个参数指定由glClear清除颜色缓存时所使......
  • Perfectly Clear Workbench(图片编辑软件)mac/win
    PerfectlyClearWorkBench是一款由AthentechImaging开发的图像校正软件,它提供了高效、自动化的图像颜色和曝光校正工具。该软件可以使用各种算法对图像进行智能分析和处理,以确保最佳视觉结果。Mac版PerfectlyClearWorkbench下载Win版PerfectlyClearWorkbench中文版Perfe......
  • How to Clear Logs of Running Docker Containers
    HowtoClearLogsofRunningDockerContainers https://www.howtogeek.com/devops/how-to-clear-logs-of-running-docker-containers/UnderstandingtheProblemDockercollectslogsfromthestandardoutputanderrorstreamsofcontainerforegroundprocesses.......
  • STL-multiset(ACM)
    1.与set不同的是,multiset可以允许多个相同元素同时出现重载函数(默认)multiset<int,int>mu;基本操作mu.erase(x);//把所有与x相同的元素删除//如果我们只想删除一个的话//通过删除迭代器实现mu.erase(mu.find(x));mu.count(x);//查的时间与x的个数有关,慎用需......
  • jQuery队列控制方法详解queue()/dequeue()/clearQueue()
    jQuery遍历-jQuery.queue()方法:[url]http://www.w3school.com.cn/jquery/data_jquery_queue.asp[/url]jQuery核心中,有一组队列控制方法,这组方法由queue()/dequeue()/clearQueue()三个方法组成,它对需要连续按序执行的函数的控制可以说是简明自如,......
  • STL之Set和multiset容器
    STL之Set和multiset容器 1.set/multiset的简介set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列。元素插入过程是按排序规则插入,所以不能指定插入位置。set采用红黑树变体的数据结构实现,红黑树属于平衡二叉树。在插入操作和删除操作上比vector快。set不可......
  • couldn't clear tomcat cache java.lang.NoSuchFieldException: resourceEntries
    2015-09-2500:17:11,435WARN[dqapp24http-nio-8002-exec-22]com.opensymphony.xwork2.util.LocalizedTextUtilcouldn'tcleartomcatcachejava.lang.NoSuchFieldException:resourceEntriesatjava.lang.Class.getDeclaredField(Class.java:2062)~[na:1.8......
  • Presentation-Nuclear Power
    欢迎观众:Helloeveryone,Iamxxx,I'mfromComputerScienceInstituteandmajoringinArtificialIntelligence,welcometomypresentation,I'msogladtosharemytopicwithyou,thesubjectofmypresentationis therecommendationofNuclearPow......
  • 详解c++STL—容器set/multiset
    1、set基本概念1.1、功能所有元素都会在插入时自动被排序1.2、本质:set/multiset属于关联式容器,底层结构是用二叉树实现。1.3、set和multiset区别set不允许容器中有重复的元素multiset允许容器中有重复的元素2、set构造和赋值2.1、功能描述创建set容器以及赋值2.1、构造set<T>st;/......
  • set, multiset 和 unordered_set, unordered_multiset
    (39条消息)set,multiset和unordered_set,unordered_multiset_unordered_set求并集_张松超的博客-CSDN博客其次抄了点一、使用前提引入头文件:#include<unordered_set>11二、unordered_set是什么unordered_set容器,可直译为“无序set容器”。即unordered_se......