题目链接

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. 【坚持】Selenium+Python学习记录 DAY8

    2018/05/ 28 [来源:菜鸟教程](http://www.runoob.com/python3/python3-examples.html) 继续敲类相关的代码 #No.1 class peo ...

  2. 部署mysql版本项目问题记录

    一,com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure报错 将url从jdbc:mysq ...

  3. Navicat将oracle中数据复制到mysql

    1,首先两个数据库都要处于连接状态 2,工具 -- 数据传输 3,选择来源数据库以及要传输的表和目标数据库 4,点击开始 PS:遇到一个问题:[Err] [Dtf] 1426 - Too-big pr ...

  4. 《Spring 2之站立会议3》

    <Spring 2之站立会议3> 昨天,查找了本机的端口号,并对代码作进一步的了解. 今天,对我们项目的基本框架进行了了解,即主界面和各个分界面的基本架构: 遇到的问题,虽然了解了基本框架 ...

  5. install4j 工具为java程序打包exe

    用 install4j 工具为java程序打包exe 制作人:mark 制作时间:2013-05-02 用Eclipse 将程序源码打包成jar文件. 打包jar方法我不做介绍了,相信大家都会,不会的 ...

  6. WCF 和 ASP.NET Web API

    地址:https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/wcf-and-aspnet-web-api WCF 是 Microsoft 为生成面 ...

  7. IHttpModule理解-知识补充

    文章:IHttpModule的那些事 可以自定义类实现IHttpModule接口,然后实现接口方法Init,Init方法可以得到HttpApplication 的实例化对象. 然后给对象的事件的注册各 ...

  8. 团队GIT实战总结

    项目要求 组长博客 遇到的困难及解决办法 组员1(组长):王彬 遇到的困难  在团队任务分工的时候没有充分照顾到所有人,导致队员们的工作量不均. 现场编程时间不够 解决办法 在此对组员们表示抱歉,由于 ...

  9. 四则运算截图and代码

    1.运行截图 2.代码 #include<stdio.h> #include<stdlib.h> int main() { int i=300; int a=0; while( ...

  10. EGener2四则运算出题器

    项目源码: https://git.coding.net/beijl695/EGener2.git (代码纯属原创,设计细节不同,请思量) 项目发布后,由于期间各种事情,耽搁至最后一天交付.这次的项目 ...