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. 在Ubuntu上下载、编译和安装Android最新源代码

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6559955 看完了前面说的几本书之后,对Lin ...

  2. Linux下找不到动态链接库

    1.生成静态库 生成静态库使用ar工具,其实ar是archive的意思 $ar cqs libhello.a hello.o 2.生成动态库 用gcc来完成,由于可能存在多个版本,因此通常指定版本号: ...

  3. 如何给你的Android 安装文件(APK)瘦身

    如何给你的Android 安装文件(APK)瘦身 本文翻译自:Putting Your APKs on Diet           原作者:Cyril Mottier Android的apk文件越来 ...

  4. timed out waiting for input: auto-logout

    The ssh "timed out waiting for input: auto-logout" messages is generated by ssh upon reach ...

  5. 查看Oracle当前用户下的信息(用户,表视图,索引,表空间,同义词,存储过程函数,约束条件)

    0.表空间 SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select * from user ...

  6. My way to Python - Day04 - 模块

    re模块 什么是正则表达式 正则表达式,英文叫做Regular Expression.简单说,正则表达式就是一组规则,用于实现字符串的查找,匹配,以实现关于字符串的相关操作,比如替换,删除等. 正则表 ...

  7. 【转】Difference between Point-To-Point and Publish/Subscribe JMS Messaging Models

    Difference between Point-To-Point and Publish/Subscribe JMS Messaging Models   Point-to-Point (PTP) ...

  8. unison实时双向数据同步

    软件下载 ocamlopt下载地址:http://caml.inria.fr Unison下载地址:http://www.seas.upenn.edu/~bcpierce/unison 1.安装uni ...

  9. 无法显示TabHost的setIndicator设置的图片的问题解决办法

    本想做一个 带有图片的 分页显示的demo 但是 自己的代码写的也木有问题 ,所以只有mainifest 有问题了 主要是用到了  Tabhost <TabHost xmlns:android= ...

  10. RegExp子模式- "()"

    读书笔记 把JavaScript权威指南拿出来瞅瞅,正巧看到了第十章 正则表达式的模式匹配 最初接触js的时候,基本上都是在做验证.什么数字验证.命名验证.身份证格式验证.电话号码验证.都是用正则表达 ...