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个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- iOS.Location-Based Service
基于位置区域的服务 1. 背景 Ref[1] 在iOS设备锁屏的状态下,App的icon会出现在屏幕的左下角. iOS 8 Feature: Location-based Lockscreen App ...
- Python配置工具类ConfigParser使用
ConfigParser模块定义了类ConfigParser,用于实现配置文件解释器.该模块ConfigParser在Python3中,已更名为configparser. 一,函数介绍 1.读取配置文 ...
- 20岁的设计师vs30岁的设计师
20岁的设计师vs30岁的设计师 如果你还是20来岁,要恭喜你,你还年轻, 一切才刚刚开始 还有时间去探索无尽的可能 还有时间去找到无限的前途 如果30岁的你还不够强大, 请记得时刻给予自己信心, ...
- @1-5使用pandas保存豆瓣短评数据
使用pandas保存豆瓣短评数据 Python爬虫(入门+进阶) DC学院 本节课程的内容是介绍open函数和pandas两种保存已爬取的数据的方法,并通过实际例子使用pandas保存数据. ...
- Can I win LT464
In the "100 game," two players take turns adding, to a running total, any integer from 1.. ...
- c#使用Stopwatch来计算时间间隔
今天要记录一个接口的耗时情况,就要求去写一段测试各个代码运行时间的方法,于是就加了这么一段代码.原来的做法是在代码执行之前和之后获取系统时间,然后相减.被改成了使用c#里的Stopwatch来计算时间 ...
- maven下的jar项目打包的方法
1.先对pom项目进行install: RunAs->maven install,如果出现如下错误: Failed to execute goal org.apache.maven.plugin ...
- 提升HTML5的性能体验系列之一 避免切页白屏
窗体切换白屏的现实问题 HTML5的性能比原生差很多,比如切页时白屏.列表滚动不流畅.下拉刷新和上拉翻页卡顿.在低端Android手机上,很多原生App常用的功能和体验效果都很难使用HTML5技术模拟 ...
- css中元素的位置
一.display 1.display:none 隐藏标签 2.display:inline 将块级标签改为内联标签 3.display:block 将内联标签改为块级标签 4.display:inl ...
- Linux 文件授权
Linux用户权限 在Linux操作系统中,root的权限是最高的,相当于windows的administrator,拥有最高权限,能执行任何命令和操作,在Linux系统中,通过UID来区分用 ...