Codeforces 1037F. Maximum Reduction
总感觉我这种做法会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的更多相关文章
- [Manthan, Codefest 18][Codeforces 1037F. Maximum Reduction]
题目链接:1037F - Maximum Reduction 题目大意:给出一段代码,给你一个长度为n的数组和数字k,求程序运行结果,mod 1e9+7输出 简单翻译下代码的意思,初始定义一个空数组b ...
- Codeforces 484B Maximum Value(高效+二分)
题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...
- Codeforces C. Maximum Value(枚举二分)
题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...
- [codeforces 508E]Maximum Matching
题目:Maximum Matching 传送门:http://codeforces.com/contest/1038/problem/E 分析: 一个块拥有{color1,val,color2},两个 ...
- codeforces B.Maximum Absurdity 解题报告
题目链接:http://codeforces.com/contest/332/problem/B 题意:在一个序列中,在所有长度为k的区间里找出两个不重叠的最大和,输出这两个最大和所对应的开头的位置a ...
- Codeforces 484B Maximum Value(排序+二分)
题目链接: http://codeforces.com/problemset/problem/484/B 题意: 求a[i]%a[j] (a[i]>a[j])的余数的最大值 分析: 要求余数的最 ...
- Codeforces 888E Maximum Subsequence
原题传送门 E. Maximum Subsequence time limit per test 1 second memory limit per test 256 megabytes input ...
- CodeForces 280B Maximum Xor Se
题目链接:http://codeforces.com/contest/280/problem/B 题目大意: 给定一个由n个数组成的一个序列,s[l..r] (1 ≤ l < r ≤ n)代表原 ...
- [Codeforces Round #508 (Div. 2)][Codeforces 1038E. Maximum Matching]
前几天给舍友讲这题的时候感觉挺有意思的,就贴上来吧... 题目链接:1038E - Maximum Matching 题目大意:有\(n\)个棒子,每个条两端有颜色\(c1,c2\)以及他的价值\(v ...
随机推荐
- LCS+LIS
#include<iostream> #include<string> using namespace std; string a,b; ][]; int main() { w ...
- glup自动化构建工具
实现的功能包括 js压缩,css文件合并压缩并在html加版本号,压缩html文件 1.安装gulp 建议参考官网就行http://www.gulpjs.com.cn/docs/getting-sta ...
- cookie不能删除
cookie不仅仅包含一个键值对,还包含域 domain 路径path, 一般domain是请求的地址 www.baidu.com/news.html 那domain就是www.baidu.com ...
- Java对字符串进行加密
package anli1; import java.util.Scanner; public class example { public static void main(String[]args ...
- [译]如何将docker日志重定向到单个文件里
原文来源: how-to-redirect-docker-logs-to-a-single-file 问题: 我想把某一个docker的log全部导出到一个文件里进行分析,我该怎么做? 其实不用那样操 ...
- jquery select chosen 动态绑定值
$("#ddlMstData").find("option[value=" + data.MstKey + "]").attr(" ...
- redis cluster管理工具redis-trib.rb详解
redis cluster管理工具redis-trib.rb详解 来源 http://weizijun.cn/2016/01/08/redis%20cluster%E7%AE%A1%E7%90%86% ...
- [Atcoder Grand Contest 006 F][AGC006F] Blackout [染色]
题面 传送门 思路 首先,这个涂黑的方法我们来优化一下模型(毕竟当前这个放到矩形里面,你并看不出来什么规律qwq) 我们令每个行/列编号为一个点,令边(x,y)表示一条从x到y的有向边 那么显然只要有 ...
- 堆栈(Stacks)
堆栈(Stacks) 准备工作 安装Docker 1.13及以上版本 安装Docker Compose正如第三部分的准备工作. 安装Docker Machine正如第四部分的准备工作. 阅读第一部分的 ...
- 《c程序设计语言》读书笔记-5.5-指针实现strncpy,strncat,strncmp
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> ...