题目: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:

  1. 写一些小数据就可以发现,其实对于一个询问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的更多相关文章

  1. @codeforces - 455E@ Function

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 已知 a 序列,并给定以下关系: \[\begin{cases} ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. 虚拟机vmware centos7 扩展磁盘空间

    0.思路 创建一个新的逻辑分区,将新的逻辑分区格式化ext3(或其他类型)的文件系统,mount到磁盘空间不够的文件系统,就跟原来的分区/文件系统一样的使用 1.准备 1.1 注意使用VMware自带 ...

  2. Linux 中的文件锁

    参考资料: https://www.ibm.com/developerworks/cn/linux/l-cn-filelock/index.html

  3. 会调色了不起吗? SORRY,会调色真的了不起!

    其实,现实的世界,大部分都是非常普通和常见的.所以调色师才有他们发挥的空间.如何把镜头中的世界变成梦幻一般. 把画面的颜色统一之后,逼格马上提升了很多! 发现表情不对,从其他照片把表情P回来,哈哈 这 ...

  4. Robotframework与unittest对比

    都可以自动挂ui测试 都可以自动化接口测试

  5. vue 获取跳转上一页组件信息

    项目中有一需求,需要根据不同的页面路径url跳转进行不同的操作,首先需要获得上一页面的url,利用 beforeRouteEnter 这个钩子中的from参数获得之前url的信息,然后给 next 传 ...

  6. dedecms首页搜索 添加仿百度下拉框

    1:找到uploads/templets/default/head.htm 2: 找到 <input name="q" type="text"  clas ...

  7. PDO beginTransaction (),exec(),commit ()

    $dsn = 'sqlsrv:server=.\SQLExpress;Database=thinkphp'; $user = 'admin'; $password = 'pass1234'; try ...

  8. Office 365 API Tools预览版已提供下载

    Office 365 API Tools预览版地址:http://visualstudiogallery.msdn.microsoft.com/7e947621-ef93-4de7-93d3-d796 ...

  9. python学习 day3 (3月4日)---字符串

    字符串: 下标(索引) 切片[起始:终止] 步长[起始:终止:1] 或者-1 从后往前 -1 -2 -3 15个专属方法: 1-6  : 格式:大小写 , 居中(6) s.capitalize() s ...

  10. MacBook小技巧

    退出全屏:Control+Command+F.关闭当前的应用程序:Command+W.退出应用程序,可对着Dock上的应用程序辅助点按(右键),选择退出.也可直接按Commnad+Q退出当前的应用程序 ...