题目链接

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. 【读书笔记】《Computer Organization and Design: The Hardware/Software Interface》(1)

    笔记前言: <Computer Organization and Design: The Hardware/Software Interface>,中文译名,<计算机组成与设计:硬件 ...

  2. Python遗传算法工具箱DEAP框架分析

    本文主要介绍python遗传算法工具箱DEAP的实现.先介绍deap的如何使用,再深入介绍deap的框架实现,以及遗传算法的各种实现算法. 代码可以参考 https://github.com/suma ...

  3. js传输txt文档内容

    要求:实现修改text文档内容,即可将text修改内容传到页面显示: HTML: <!doctype html> <html lang="en"> < ...

  4. 封装,策略,Asp换脸

    封装.策略 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...

  5. BETA随笔6/7

    前言 我们居然又冲刺了·六 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 过去两天完成了哪些任务 新方案代码比之前的更简单,但是对场景的要求相应变高了,已经实现,误差感人 代码 ...

  6. BETA-1

    前言 我们居然又冲刺了·一 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 过去两天完成了哪些任务 发现之前的代码居然已经有了陌生感,默默地复习一遍并做注释 阅读关于基于视频的车 ...

  7. 31_网络编程(Socket套接字编程)_讲义

    今日内容介绍 1.网络三要素及传输协议 2.实现UDP协议的发送端和接收端 3.实现TCP协议的客户端和服务器 4.TCP上传文件案例 01网络模型 *A:网络模型 TCP/IP协议中的四层分别是应用 ...

  8. Internet History, Technology and Security (Week 9)

    Week 9 Security: Web Security We are now on the second to last week of the class and finishing up ou ...

  9. Internet History, Technology and Security (Week 8)

    Week 8 Security: Encrypting and Signing This week we start two weeks of Internet Security. It is a l ...

  10. Robot Framework 教程 (6) - 使用条件表达式

    本篇文章,主要对如何在Robot Framework中使用条件表达式做过程控制作说明. 按照Robot Framework的官方文档介绍,Robot Framework并不建议在TestCase或Ke ...