总感觉我这种做法会T,一直没写,看了其他人的题解也是这样,,,就果断写了,,可能数据不太深,或者玄学复杂度

题意即求xk-1长度的所有区间的最大值的和,对每一个i(数组下边),他对答案的贡献数量就是在以ar[i]为最大值的最大子区间中所有符合条件的区间数量

求ar[i]的作用区间,即,求最小的l,对于x>=l&&x<i,ar[x]<=ar[i];求最大的r,对于k<=r&&k>i,ar[k]<ar[i];则ar[i]的作用区间为[l,r];我代码中的ar[i]的作用区间为(pre[i],nex[i])

然后遍历i左右中更小的区间,求ar[i]对答案的贡献数量即可,代码思路应该很清晰

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<fstream>
#include<cstdlib>
#include<ctime>
#include<list>
#include<climits>
#include<bitset>
using namespace std;
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("input.in", "r", stdin);freopen("output.in", "w", stdout);
#define left asfdasdasdfasdfsdfasfsdfasfdas1
#define tan asfdasdasdfasdfasfdfasfsdfasfdas
typedef long long ll;
typedef unsigned int un;
const int desll[][]={{,},{,-},{,},{-,}};
const ll mod=1e9+;
const int maxn=2e6+;
const int maxm=1e9+;
const double eps=1e-;
int m,n,k;
int ar[maxn];
int pre[maxn],nex[maxn];
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&ar[i]);
pre[]=;
for(int i=;i<=n;i++){
int ins=i-;
while(ins> && ar[ins]<ar[i])ins=pre[ins];
pre[i]=ins;
}
nex[n]=n+;
for(int i=n-;i>;i--){
int ins=i+;
while(ins<=n && ar[ins]<=ar[i])ins=nex[ins];
nex[i]=ins;
}
//for(int i=1;i<=n;i++)cout<<pre[i]<<" ";cout<<endl;
//for(int i=1;i<=n;i++)cout<<nex[i]<<" ";cout<<endl;
ll ans=;
for(int i=;i<=n;i++){
int l=pre[i],r=nex[i];
ll a,b;
if(i-l<r-i){
for(int x=l+;x<=i;x++){//x为符合条件的区间的起始点
a=(i-x+-+m-)/(m-);//a为包含i的区间中最少的(m-1)段数
a=max(a,1LL);
b=(r-x-)/(m-);//b为以r-1为区间终点,可以贡献的最多(m-1)段数
///cout<<"x = "<<x<<" "<<a<<" "<<b<<endl;
if(b>=a){
ans = (ans + (b-a+)*ar[i]%mod)%mod;
}
}
}
else{
for(int x=i;x<r;x++){
a=(x-i+-+m-)/(m-);
a=max(a,1LL);
b=(x-l-)/(m-);
//cout<<"x = "<<x<<" "<<a<<" "<<b<<endl;
if(b>=a){
ans = (ans + (b-a+)*ar[i]%mod)%mod;
}
}
}
//cout<<i<<" "<<ans<<" "<<a<<" "<<b<<endl;
}
printf("%I64d\n",ans); return ;
}

Codeforces 1037F. Maximum Reduction的更多相关文章

  1. [Manthan, Codefest 18][Codeforces 1037F. Maximum Reduction]

    题目链接:1037F - Maximum Reduction 题目大意:给出一段代码,给你一个长度为n的数组和数字k,求程序运行结果,mod 1e9+7输出 简单翻译下代码的意思,初始定义一个空数组b ...

  2. Codeforces 484B Maximum Value(高效+二分)

    题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...

  3. Codeforces C. Maximum Value(枚举二分)

    题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. [codeforces 508E]Maximum Matching

    题目:Maximum Matching 传送门:http://codeforces.com/contest/1038/problem/E 分析: 一个块拥有{color1,val,color2},两个 ...

  5. codeforces B.Maximum Absurdity 解题报告

    题目链接:http://codeforces.com/contest/332/problem/B 题意:在一个序列中,在所有长度为k的区间里找出两个不重叠的最大和,输出这两个最大和所对应的开头的位置a ...

  6. Codeforces 484B Maximum Value(排序+二分)

    题目链接: http://codeforces.com/problemset/problem/484/B 题意: 求a[i]%a[j] (a[i]>a[j])的余数的最大值 分析: 要求余数的最 ...

  7. Codeforces 888E Maximum Subsequence

    原题传送门 E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input ...

  8. CodeForces 280B Maximum Xor Se

    题目链接:http://codeforces.com/contest/280/problem/B 题目大意: 给定一个由n个数组成的一个序列,s[l..r] (1 ≤ l < r ≤ n)代表原 ...

  9. [Codeforces Round #508 (Div. 2)][Codeforces 1038E. Maximum Matching]

    前几天给舍友讲这题的时候感觉挺有意思的,就贴上来吧... 题目链接:1038E - Maximum Matching 题目大意:有\(n\)个棒子,每个条两端有颜色\(c1,c2\)以及他的价值\(v ...

随机推荐

  1. 求 n的阶乘

    def chengji(n): if n == 0: return 1 return chengji(n-1)*nprint(chengji(n))

  2. ImportError: dynamic module does not define module export function (PyInit__caffe)

    使用python3运行caffe,报了该错误. 参考网址:https://stackoverflow.com/questions/34295136/importerror-dynamic-module ...

  3. PAT——甲级1065:A+B and C(64bit) 乙级1010一元多项式求导

    甲级1065 1065 A+B and C (64bit) (20 point(s)) Given three integers A, B and C in [−2​63​​,2​63​​], you ...

  4. HDU 3775 Chain Code pick定理

    pick定理:一个计算点阵中顶点在格点上的多边形面积公式:S=a+b÷2-1,其中a表示多边形内部的点数,b表示多边形边界上的点数,s表示多边形的面积. 思路:http://blog.csdn.net ...

  5. Tomcat 顶层结构

    Tomcat中最顶层的容器叫Server,代表整个服务器,Server中包含至少一个Service,用于具体提供服务. Service主要包含两部分:Connector   和   Container ...

  6. 重复造轮子系列--内存池(C语言)

    这个代码是我上个公司工作项目的里面内存管理(基于伙伴算法)的一个简化又简化的版本. 因为没有内存边界检查: 因为没有内存使用统计: 因为没有考虑线程安全: 因为没有内存分配操作的具体文件位置信息: 因 ...

  7. 从 C10K 到 C500K

    国外的 Urban Airship 公司的工程师在其官方网志上发文章介绍他们在产品环境中做到 50 万并发客户端,Java + Pure NIO 的实现,最近又有文章介绍针对 Linux Kernel ...

  8. 多线程和CPU的关系

    什么是CPU (1)         Central  Progressing  Unit 中央处理器,是一块超大规模的集成电路,是一台计算机的运算核心和控制核心. (2)         CPU包括 ...

  9. hdu 1426 Sudoku Killer (dfs)

    Sudoku Killer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  10. 如何在CentOS7上改变网络接口名

    如何在CentOS7上改变网络接口名 传统上,Linux的网络接口被枚举为eth[0123...],但这些名称并不一定符合实际的硬件插槽,PCI位置,USB接口数量等,这引入了一个不可预知的命名问题( ...