题意:由01组成的长度为n的子串,AT由0表示,GC由1表示,求一段长度大于等于L且GC率最高的子串的起始终止坐标,若GC率相同,取长度较小,若长度相同,取起始坐标最小。

分析:

1、一个子串(i+1,j)的GC率为(sum[j] - sum[i]) / (j - i),sum[j]为前缀和

上式可以理解为点(i, sum[i])与点(j, sum[j])的斜率表达式。

因此问题可转化为求横坐标之差大于等于L的两点所组成的直线斜率的最大值。

2、https://wenku.baidu.com/view/b97cd22d0066f5335a8121a3.html?from_page=view&from_mod=download

上述论文可知,任何一个点Pt的检查集合中,不可能存在一个对最优结果有贡献的上凸点,因此我们可以删去每一个上凸点,剩下的则是一个下凸折线。

3、利用单调性求过检查点Pt与下凸折线相切的直线,从而得到当前右端点下最优的左端点。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
char s[MAXN];
int sum[MAXN];
deque<int> q;
int judge(int a, int b, int c, int d){
return (sum[a] - sum[b]) * (c - d) - (sum[c] - sum[d]) * (a - b);
}
int main(){
int T;
scanf("%d", &T);
while(T--){
memset(sum, 0, sizeof sum);
q.clear();
int n, L;
scanf("%d%d", &n, &L);
scanf("%s", s + 1);
for(int i = 1; i <= n; ++i){
sum[i] = sum[i - 1] + s[i] - '0';
}
int ansl = 0, ansr = L;//最优区间初始化为(1,L)
for(int i = L; i <= n; ++i){//枚举右端点
int p = i - L;
while(q.size() > 1){//维护下凸折线
int tmpj = q[q.size() - 1];
int tmpk = q[q.size() - 2];
if(judge(tmpj, tmpk, p, tmpj) > 0){//上凸
q.pop_back();
}
else{
break;
}
}
q.push_back(p);//在右端加入新的点
while(q.size() > 1){//找与下凸折线的切点
if(judge(i, q[0], i, q[1]) <= 0){
q.pop_front();
}
else break;
}
int tmp = judge(i, q[0], ansr, ansl);
if(tmp > 0 || (tmp == 0 && i - q[0] < ansr - ansl)){
ansl = q[0];
ansr = i;
}
}
printf("%d %d\n", ansl + 1, ansr);
}
return 0;
}

  

UVA - 1451 Average (斜率优化)的更多相关文章

  1. UVa 1451 Average - 斜率优化

    A DNA sequence consists of four letters, A, C, G, and T. The GC-ratio of a DNA sequence is the numbe ...

  2. UVA 1451 Average平均值 (数形结合,斜率优化)

    摘要:数形结合,斜率优化,单调队列. 题意:求一个长度为n的01串的子串,子串长度至少为L,平均值应该尽量大,多个满足条件取长度最短,还有多个的话,取起点最靠左. 求出前缀和S[i],令点Pi表示(i ...

  3. UVA 1451 Average

    A DNA sequence consists of four letters, A, C, G, and T. The GC-ratio of a DNA sequence is the numbe ...

  4. HDU 2993 MAX Average Problem dp斜率优化

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  5. MAX Average Problem(斜率优化dp)

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. 【UVA 1451】Average

    题 题意 求长度为n的01串中1占总长(大于L)的比例最大的一个子串起点和终点. 分析 前缀和s[i]保存前i个数有几个1,[j+1,i] 这段区间1的比例就是(s[i]-s[j])/(i-j),于是 ...

  7. HDU 2993 MAX Average Problem(斜率优化DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给定一个长度为n(最长为10^5)的正整数序列,求出连续的最短为k的子序列平均值的最大 ...

  8. UVALive 4726 Average ——(斜率优化DP)

    这是第一次写斜率优化DP= =.具体的做法参照周源论文<浅谈数形结合思想在信息学竞赛中的应用>.这里仅提供一下AC的代码. 有两点值得注意:1.我这个队列的front和back都是闭区间的 ...

  9. 斜率优化dp(POJ1180 Uva1451)

    学这个斜率优化dp却找到这个真心容易出错的题目,其中要从n倒过来到1的确实没有想到,另外斜率优化dp的算法一开始看网上各种大牛博客自以为懂了,最后才发现是错了. 不过觉得看那些博客中都是用文字来描述, ...

随机推荐

  1. 【转】android之在activity中控制另一个activity的UI更新_如何在activity之间传递handler

    来自:http://blog.csdn.net/jason0539/article/details/18055259 遇到一个问题,需要在一个activity中控制另一个acitivity做一些更新, ...

  2. LNMP一键安装包 PHP自动升级脚本

    LNMP一键安装包 PHP自动升级脚本 2011年03月15日 上午 | 作者:VPS侦探 前一段时间完成了lnmp一键安装包的PHP自动升级脚本,今天发布出来,如果想升级PHP版本的lnmp用户可以 ...

  3. redis(笔记)

    ./redis-cli -a root 带密码链接 root 为我的密码del key 删除key keys * 查询 所有keyexist key key是否存在 成功返回1 失败Wie0 set ...

  4. keyup事件、keydown事件和input事件的区别

    keydown.keyup 属于键盘事件,input 属于文本事件 详细说明: keydown:当用户按下键盘上的任意按键时触发,如果按住不放,会重复触发此事件. keyup:当用户释放键盘上的按键时 ...

  5. [蓝桥杯2015决赛]四阶幻方(DFS + 剪枝)

    题目描述 把1~16的数字填入4x4的方格中,使得行.列以及两个对角线的和都相等,满足这样的特征时称为:四阶幻方. 四阶幻方可能有很多方案.如果固定左上角为1,请计算一共有多少种方案. 比如: 1  ...

  6. javaScript中this的指向?

    javaScript中this对象是在运行时基于函数的执行环境绑定的,在全局函数中,this等于window,而当函数被作为某个对象的方法调用时,this等于那个对象. 但在实际中,代码环境复杂,th ...

  7. 微信小程序(基础)

    文档官网:https://developers.weixin.qq.com/miniprogram https://developers.weixin.qq.com/miniprogram/dev/f ...

  8. LeetCode206. Reverse Linked List(反转链表)

    题目链接:https://leetcode.com/problems/reverse-linked-list/ 方法一:迭代反转 https://blog.csdn.net/qq_17550379/a ...

  9. VUE框架下安装自带http协议

    在控制台CMD 中输入 npm install vue-resource --save-dev

  10. 在vnware中配置好redis后,不能使用图形化工具打开

    1.先检查防火墙的状态 通过systemctl status firewalld查看firewalld状态,发现当前是dead状态,即防火墙未开启 通过systemctl start firewall ...