题目链接

BZOJ2213

题解

考虑任意一对点的贡献,单独拿出那些点所在位置

一个设为\(1\),一个设为\(-1\),从头到尾扫一遍维护前缀和,以及当前最小前缀和

两者相减更新答案

需要注意的是当前最小前缀和更新的位置之后必须存在另一个字符,否则就不满足最小出现次数最少大于\(0\)的限制

由于每个位置最多拿出来\(26\)次,所以是\(O(26n)\)的

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cctype>
#include<cmath>
#include<vector>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 1000005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
vector<int> pos[26];
int n,ans;
void work(int x,int y){
int nowx = 0,nowy = 0,minx = INF,miny = INF,mnx = 0,mny = 0;
unsigned int px = 0,py = 0;
while (px < pos[x].size() || py < pos[y].size()){
if (py == pos[y].size() || (px < pos[x].size() && pos[x][px] < pos[y][py])){
miny = min(miny,mny);
nowx++; nowy--;
px++;
}
else {
minx = min(minx,mnx);
nowy++; nowx--;
py++;
}
mnx = min(nowx,mnx); mny = min(mny,nowy);
if (minx < INF) ans = max(ans,nowx - minx);
if (miny < INF) ans = max(ans,nowy - miny);
}
}
int main(){
n = read(); int x; char c = getchar();
while (!isalpha(c)) c = getchar();
for (int i = 1; i <= n; i++){
x = c - 'a';
pos[x].push_back(i);
c = getchar();
}
for (int x = 0; x < 26; x++)
for (int y = x + 1; y < 26; y++)
work(x,y);
printf("%d\n",ans);
return 0;
}

BZOJ2213 [Poi2011]Difference 【乱搞】的更多相关文章

  1. BZOJ2213: [Poi2011]Difference

    2213: [Poi2011]Difference Time Limit: 10 Sec  Memory Limit: 32 MBSubmit: 343  Solved: 108[Submit][St ...

  2. bzoj 2217 [Poi2011]Lollipop 乱搞 贪心

    2217: [Poi2011]Lollipop Time Limit: 15 Sec  Memory Limit: 64 MBSec  Special JudgeSubmit: 383  Solved ...

  3. BZOJ2213[Poi2011]Difference——DP

    题目描述 A word consisting of lower-case letters of the English alphabet ('a'-'z') is given. We would li ...

  4. 【BZOJ2217】[Poi2011]Lollipop 乱搞

    [BZOJ2217][Poi2011]Lollipop Description 有一个长度为n的序列a1,a2,...,an.其中ai要么是1("W"),要么是2("T& ...

  5. bzoj2213: [Poi2011]Difference(思维题)

       今天颓了一天T T 这题有两种写法... ①预处理出每种字符在原字符串中的位置,枚举两种字符作为最大值和最小值,把这两种字符的坐标归并排序,把最大值设为1,最小值设为-1,求最大子段和.注意因为 ...

  6. 【BZOJ2213】[Poi2011]Difference DP

    [BZOJ2213][Poi2011]Difference Description A word consisting of lower-case letters of the English alp ...

  7. [bzoj2213][Poi2011]Difference_动态规划

    Difference bzoj-2213 Poi-2011 题目大意:已知一个长度为n的由小写字母组成的字符串,求其中连续的一段,满足该段中出现最多的字母出现的个数减去该段中出现最少的字母出现的个数最 ...

  8. URAL 1827 Indigenous Wars(排序、乱搞)

    题意:给一个长度为n数组{a[i]}.有m个操作Ti,Si,Li表示找以Ti值结束,以Si值开始,长度为Li的连续子串.找到后,将区间的答案值设为1.一开始答案值全部为0.最后输出n个答案值. 好久没 ...

  9. UVA 11853 [dfs乱搞]

    /* 大连热身E题 不要低头,不要放弃,不要气馁,不要慌张 题意: 在1000×1000的格子内有很多个炮弹中心,半径给定. 为某人能否从西部边界出发,从东部边界走出. 不能输出不能,能的话输出最北边 ...

随机推荐

  1. 【微服务架构】SpringCloud组件和概念介绍(一)

    一:什么是微服务(Microservice) 微服务英文名称Microservice,Microservice架构模式就是将整个Web应用组织为一系列小的Web服务.这些小的Web服务可以独立地编译及 ...

  2. play-with-vim1~5

    1.移动 h,j,k,l分别对应左下上右 2.模式 vim有四种模式:普通模式,插入模式,可视模式,命令行模式 进入vim 默认为普通模式,光标为方块 输入i 进入插入模式,窗口左下角为insert ...

  3. linux下实现压测-html报表生成-控制台参数优化【jmeter】

    jmeter - 单机压测 - 命令行模式-html报表生成-控制台参数优化 一/ 准备工作 1.压力机安装并配置好 jdk 2.调试好程序脚本 再上传到 linux下 3.进入jmeter  bin ...

  4. Oracle扩展包(pipe,alert,job,scheduler)

    --定义包中函数的纯度级别 create or replace package purityTest is type dept_typ is table of dept%rowtype index b ...

  5. 在Maven上Web项目添加Spring框架

    1. pom.xml添加Spring依赖包 <!-- spring 核心依赖--> <!-- context依赖beans,aop,core,expression;core依赖log ...

  6. Deep learning for visual understanding: A review 视觉理解中的深度学习:回顾 之一

    Deep learning for visual understanding: A review 视觉理解中的深度学习:回顾 ABSTRACT: Deep learning algorithms ar ...

  7. ofo容器pass架构分享

    一.我们先要了解一下,为什么企业需要一个paas平台?或者可以说paas到底能做什么? 1.1 我们先来了解一下paas到底是什么? PaaS是Platform-as-a-Service的缩写,意思是 ...

  8. 三羊献瑞:dfs / next_permutation()

    三羊献瑞 观察下面的加法算式: 祥 瑞 生 辉  +   三 羊 献 瑞-------------------   三 羊 生 瑞 气 (如果有对齐问题,可以参看[图1.jpg]) 其中,相同的汉字代 ...

  9. Vue+webpack项目中,运行报错Cannot find module 'chalk'的处理

    刚开始用vue + webpack新建项目,在github上下载了一个示例,输入npm init >>>npm run dev 后报错 Cannot find module 'cha ...

  10. SDWebImage 错误汇总

    1.  [UIImageView sd_setImageWithURL:placeholderImage:]: unrecognized selector sent to instance 打包静态库 ...