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个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- 分析params_s方法
/** * 解析启动模式参数 * @param $opt */ static public function params_s($opt) { //判断传入了s参数但是值,则提示错误 if ((iss ...
- Python.URLs
1. The Future of Asynchronous IO in Python https://medium.com/@paulcolomiets/the-future-of-asynchron ...
- Bowtie2的安装与使用
Bowtie2的安装与使用 2017-06-15 18:58:52 342 0 0 Bowtie2用来快速比对短reads(50-100bp)与参考基因组,与常规的比对软件不 ...
- 网卡驱动如何设置组播MAC地址
参考资料: https://blog.csdn.net/abccheng/article/details/50465268 将网卡加入到组播组中.
- python 字典遍历
dic1={"name":"kxb","age":28}for k,v in dic1.items(): print(k+",,, ...
- vue脚手架搭建的具体步骤
1.全局安装cli npm install -g vue-cli 在全局安装vue的命令行工具 2.初始化项目 vue init webpack my-project 初始化一个基于webpack ...
- 之前的一些Oracle的经验总结
1. 安装: 1) 关于字符集的选择,现在还不很了解,修改是需要进入一个模式下才可以修改,当然新建一个数据库实例的时候可以重新设定: UTF8是相对比较大的一个字符集, 可以简单实用这个就能保存很多的 ...
- mybatis学习八 事物
1.事物的定义: 是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行. 事务处理可以确保除非事务性单元内的所有操作都成功完成,否则不会永久更新面向数据的资源. 2,事物的特性: ...
- Python10/23--继承/派生
(继承)1. 什么是继承? 在程序中继承是一种新建子类的方式,新创建的类称之为子类\派生类,被继承的类称之为父类\基类\超类 继承描述的是一种遗传关系,子类可以重用父类的属性 2. 为何用继承? 减少 ...
- 747. Largest Number At Least Twice of Others
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...