codeforces 455E
题目:http://codeforces.com/problemset/problem/455/E
题意:给定数组a,及f的定义:
f[1][j] = a[j]; 1 <= j <= n
f[i][j] = min(f[i-1][j-1], f[i-1][j]) + a[j]; 2 <= i <= j <= n
给定q个询问,每个询问为l,r,求f[l][r]
My solution:
- 写一些小数据就可以发现,其实对于一个询问l,r,其实等价于:
从[r-l+1, r]这个区间内取l个数,使其和最小。但是比较特殊的是,一个数可以取多次,并且如果取了一个x,那么[x+1,r]间的所有数必须得取。
例如,对于n=3, a = {2, 1, 3}
询问l=3, r=3的取法有:3+3+3=9, 3+3+1=7, 3+1+1=5, 3+1+2= 6;答案为3+1+1=5
2.设答案f[l][r]的询问答案合法区间是在[x, r]这一段取得的,我们还可以发现如下的性质:
1)a[x]一定是[x,r]中最小的,否则存在 x<=y<=r, a[y] <= a[x],比[x,r]更优的区间[y, r]
除[y, r]的共同区间外,剩下的l-y-r个[y,r]区间可以全取a[y],显然比[x,r]更小
2)a[x+1]~a[y]各取一个,剩下的全取a[x],因为a[x]是区间最小元素,取越多答案越小
3.基于2我们可以维护一个递增的序列来求答案,但是这样还是不够,妥妥TlE
只能看下决策之间有什么关系了;
对于询问(l,r)不妨设两个决策k<j,并且决策k优于j
那么 (sum[r] - sum[k]) - (l - (r - k)) * a[k] <= (sum[r] - sum[j]) - (l - (r - j)) * a[j];
整理一下式子:
(sum[k] - a[k]*k) - (sum[j] - a[j]*j) / (a[k] - a[j]) <= l - r;
这不就是个斜率的式子,剩下的就是维护一个凸壳即可
4.具体的话对于询问按照r排序,然后离线做
然后二分一左边界,找到合法区间完,再二分找到合法的斜率。具体看代码吧
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cctype>
#include <ctime>
#include <utility>
#define M0(x) memset(x, 0, sizeof(x))
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define vii vector< pair<int, int> >::iterator
using namespace std;
const int maxn = ;
vector< pair<int, int> > ask[maxn];
int n, m;
long long sum[maxn], ans[maxn];
int s[maxn], top, a[maxn]; inline double slope(int k, int j){
double yk = sum[k] - (double)k * a[k], yj = sum[j] - (double)j * a[j];
return (yk - yj) / (a[k] - a[j]);
} void init(){
for (int i = ; i <= n; ++i)
scanf("%d", a+i), sum[i] = sum[i-] + a[i];
for (int i = ; i <= n; ++i) ask[i].clear();
scanf("%d", &m);
int l, r;
for (int i = ; i < m; ++i){
scanf("%d%d", &l, &r);
ask[r].pb(mp(l, i));
}
} void solve(){
int top = ;
int l, r, mid, pos;
for (int i = ; i <= n; ++i){
while (top > && a[s[top]] >= a[i]) --top;
while (top > && slope(s[top], i) >= slope(s[top-], i)) --top;
s[++top] = i;
for (vii it = ask[i].begin(); it != ask[i].end(); ++it){
l = lower_bound(s+, s+top+, i-it->x+) - s;
r = top-, pos = i;
while (l <= r){
mid = (l + r) >> ;
if (slope(s[mid], s[mid+]) <= it->x - i) pos = s[mid], r = mid - ;
else l = mid + ;
}
ans[it->y] = sum[i] - sum[pos] + (long long)a[pos]*(it->x+pos-i);
}
}
for (int i = ; i < m; ++i)
printf("%I64d\n", ans[i]);
}
int main(){
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
while (scanf("%d", &n) != EOF){
init();
solve();
}
return ;
}
codeforces 455E的更多相关文章
- @codeforces - 455E@ Function
目录 @description@ @solution@ @accepted code@ @details@ @description@ 已知 a 序列,并给定以下关系: \[\begin{cases} ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- docker 运行 elasticsearch + kibana
一.elasticsearch的安装 1.从官网上拉取 elasticsearch 的镜像 docker pull elasticsearch:5.6.11docker images 2.运行容器 m ...
- POJ3662或洛谷1948 Telephone Lines
二分答案+单源最短路 POJ原题链接 洛谷原题链接 显然可以二分答案,检验\(mid\)可以使用最短路来解决. 将大于\(mid\)的边看成长度为\(1\)的边,说明要使用免费升级服务,否则长度为\( ...
- [Chrome Headless + Python] 截长图 (Take Full-page Screenshot)
# -*- coding: utf-8 -*- import time import os from selenium import webdriver from selenium.webdriver ...
- Python之路番外(第二篇):PYTHON基本数据类型和小知识点
一.基础小知识点 1.如果一行代码过长,可以用续行符 \换行书写 例子 if (signal == "red") and \ (car == "moving") ...
- 转:Struts2返回JSON数据的具体应用范例
http://blog.csdn.net/jspamd/article/details/8810109 纠错: <result type="json" name=" ...
- spring boot 启动自动跳到 断点 throw new SilentExitException
项目 debug 启动,自动跳到 断点 ,而且就算F8 ,项目还是停止启动. 百度了一下,答案都是 Eclipse -> Preferences ->Java ->Debug去掉&q ...
- 跟我学Spring Boot(一)创建Spring Boot 项目
本人开发环境为idea15.02 + jdk8 步骤1: 步骤2: 步骤3: 步骤4: 步骤5: 相关目录介绍: resources/static:这里主要存放一些资源文件 例如 css.js.ima ...
- putty中查询乱码问题
我们在putty连接Linux时候,有时候查询会出现乱码问题...如下图 这个是因为putty中设置编码字符集的原因..将此换为utf8格式的即可解决 解决后查询如下:
- CentOS下利用mysqlbinlog恢复MySQL数据库
如果不小心对数据库进行误操作,而又没有及时备份怎么办?这恐怕是广大的coder经常遇到的一类问题.我今天就因为不小心删除了某个数据库,但最后的备份是1个礼拜前的,唯一能解决的办法就是通过mysqlbi ...
- 进入快速通道的委托(深入理解c#)
1.方法组:所有的名称相同的重载方法合在一起就成为一个方法组. 2.协变性和逆变性: 协变性指的是——泛型类型参数可以从一个派生类隐式转化为基类. 逆变性指的是——泛型类型参数可以从一个基类隐式转化为 ...