probelm

题意

给定一个序列和一个mod值,定义[l,r]合法当lr的全部元素和减去当中的最大值的结果能够整除mod。问共同拥有多少区间合法。

思路

一開始想的分治。

对于一个[l,r]我们能够把这之中最大的求出来,然后以这个数作为分界,把这个区间分成两部分,对于分布在两个区间中的答案,我们能够通过lowerbound和upperbunder在O(log(n))的时间下求出,然后递归求解。

然而对于这题,这样的做法的下界会达到O(n2)。所以这样做不行。

  • 看了题解,题讲解能够直接枚举那个最大值,然后把满足的区间找出来然后求出来。豁然开朗。这样我们仅仅须要把原数组进行排序,并记录每一个数的左右界。

    从最小的開始。在计算答案的同一时候去更新这个左右界。

    这样能够在O(nlog(n))的复杂度下求出答案。

  • 一道不错的题。希望以后能够坚持把每次做的比赛的题目补完。

AC代码

/*************************************************************************
> File Name: pf.cpp
> Author: znl1087
> Mail: loveCJNforever@gmail.com
> Created Time: 四 6/11 16:36:14 2015
************************************************************************/ #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#define LL long long
using namespace std;
int n,k;
LL s[300005];
vector<int> f[1000005];
LL num[300005];
int pre[300005],nxt[300005];
LL ask(int l,int r,LL in){
return upper_bound(f[in].begin(),f[in].end(),r)-lower_bound(f[in].begin(),f[in].end(),l);
}
LL cal(int m){
int l = pre[m]+1,r = nxt[m]-1;
LL maxn = num[m];
LL ans = 0;
if( r - m < m - l){
for(int i=m+1;i<=r;i++)
ans+=(ask(l-1,m-1,(s[i]-maxn%k+k)%k));
ans+=(ask(l-1,m-2,(s[m]-maxn%k+k)%k));
}else{
for(int i=l;i<m;i++)
ans+=(ask(m,r,(s[i-1]+maxn)%k));
ans+=(ask(m+1,r,(s[m-1]+maxn)%k));
}
pre[nxt[m]] = pre[m];
nxt[pre[m]] = nxt[m];
return ans;
}
int ord[300005];
int cmp(int a,int b){
return num[a]<num[b];
}
int main(){
cin>>n>>k;
s[0] = 0;
for(int i=1;i<=n;i++){
cin>>num[i],s[i] = (s[i-1]+num[i])%k,ord[i] = i;
pre[i] = i-1;
nxt[i] = i+1;
}
nxt[0] = 1;
nxt[n] = n+1;
pre[n+1] = n;
for(int i=0;i<=n;i++)f[s[i]].push_back(i);
sort(ord+1,ord+n+1,cmp);
LL ans = 0;
for(int i=1;i<=n;i++){
int pos = ord[i];
ans+=cal(pos);
}
cout<<ans<<endl;
return 0;
}

Codeforces 549F Yura and Developers的更多相关文章

  1. codeforces 549F Yura and Developers(分治、启发式合并)

    codeforces 549F Yura and Developers 题意 给定一个数组,问有多少区间满足:去掉最大值之后,和是k的倍数. 题解 分治,对于一个区间,找出最大值之后,分成两个区间. ...

  2. ●CodeForces 549F Yura and Developers

    题链: http://codeforces.com/problemset/problem/549/F题解: 分治,链表. 考虑对于一个区间[L,R],其最大值在p位置, 那么答案的贡献就可以分为3部分 ...

  3. Looksery Cup 2015 F - Yura and Developers 单调栈+启发式合并

    F - Yura and Developers 第一次知道单调栈搞出来的区间也能启发式合并... 你把它想想成一个树的形式, 可以发现确实可以启发式合并. #include<bits/stdc+ ...

  4. 【Codeforces549F】Yura and Developers [单调栈][二分]

    Yura and Developers Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample Input 4 ...

  5. Codeforces刷题计划

    Codeforces刷题计划 已完成:-- / -- [Codeforces370E]370E - Summer Reading:构造:(给定某些数,在空白处填数,要求不下降,并且相邻差值<=1 ...

  6. CF数据结构练习

    1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...

  7. Looksery Cup 2015 Editorial

    下面是题解,做的不好.下一步的目标是rating涨到 1800,没打过几次cf A. Face Detection Author: Monyura One should iterate through ...

  8. Codeforces Round #Pi (Div. 2) B. Berland National Library set

    B. Berland National LibraryTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  9. Codeforces Beta Round #69 (Div. 1 Only) C. Beavermuncher-0xFF 树上贪心

    题目链接: http://codeforces.com/problemset/problem/77/C C. Beavermuncher-0xFF time limit per test:3 seco ...

随机推荐

  1. Composer 基本指令操作使用

    Composer 基本指令操作使用 註: 若 composer.phar 改名為 composer, 請自行將 "php composer.phar" 替換成 "comp ...

  2. RMAN连接及简单操作

    一.RMAN的进入与退出 1.启动RMAN并连接到本地目标数据库 C:\Users\Administrator>set oracle_sid=orcl(如果只有一个实例,则不需要指定,RMAN会 ...

  3. HTML与CSS入门——第九章 使用颜色

    知识点: 1.为网站选择颜色的方法 2.颜色在Web上的工作方式 3.使用十六进制颜色值的方法 4.使用CSS设置背景.文本和边框颜色的方法 9.1 选择颜色的最佳方法: 直白地说:根据用户群体找到最 ...

  4. 开源项目Material Calendar View 学习记录 (一)

    开源项目Material Calendar View 学习记录 Github: https://github.com/prolificinteractive/material-calendarview ...

  5. ORACLE SQL 组函数【weber出品必属精品】

    组函数:对一组数据进行加工,每组数据返回一个值 常用的组函数:count()  avg()  max()   min()  sum()   count()函数  1. count(*) :返回总共的行 ...

  6. "客户端无法连接到远程计算机"错误的解决方法

    问题: 客户端无法连接到远程计算机. 可能没有启用远程连接或者计算机太忙不能接受新的连接. 也可能是网络问题阻止连接.请稍后重新尝试连接. 如果问题仍然存在 请与管理员联系. 解决方法: 1.首先确认 ...

  7. C# Excel 读取为Datatable

    最近项目用到的读取Excel 为DataTable 兼容2003.2007.2010.记录一下,以后会用到 引用 NPOI.dll 和 EPPlus.dll using System; using S ...

  8. hdu2488 dfs

    G - 深搜 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bi ...

  9. hdfs-over-ftp安装与配置

    hdfs-over-ftp是一个开源,简单易用的实现了对HDFS系统的下载和上传功能的小工具.可以作为管理工具来使用,快捷方便. 1 安装jdk(1.6以上版本)并配置环境变量分别执行java -ve ...

  10. 【android】android下防止anr程序无响应

    public class AnrActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState ...