bzoj 2213: [Poi2011]Difference
Description
A word consisting of lower-case letters of the English alphabet ('a'-'z') is given. We would like to choose a non-empty contiguous (i.e. one-piece) fragment of the word so as to maximise the difference in the number of occurrences of the most and the least frequent letter in the fragment. We are assuming that the least frequent letter has to occur at least once in the resulting fragment. In particular, should the fragment contain occurrences of only one letter, then the most and the least frequent letter in it coincide.
已知一个长度为n的由小写字母组成的字符串,求其中连续的一段,满足该段中出现最多的字母出现的个数减去该段中出现最少的字母出现的个数最大。求这个个数。
Input
The first line of the standard input holds one integer (1<=N<=1000000)() that denotes the length of the word. The second line holds a word consisting of lower-case letters of the English alphabet.
第一行,n
第二行,该字符串
1<=n<=1000000
Output
The first and only line of the standard output is to hold a single integer, equal to the maximum difference in the number of occurrences of the most and the least frequent letter that is attained in some non-empty contiguous fragment of the input word.
一行,表示结果
Sample Input
aabbaaabab
Sample Output
Explanation of the example: The fragment that attains the difference of 3 in the number of occurrences of a and b is aaaba.

转换成
我们用s[i]表示i这个字母的出现次数 f[i][j]表示前面某一时刻s[i]-s[j]的最小值
p[i][j]表示最小值转移时候的位置 last[i]表示i上次出现的位置
这样就完美解决答案了辣2333
#include<cstdio>
#include<cstring>
#include<algorithm>
using std::max;
using std::min;
const int M=1e6+,inf=0x3f3f3f3f;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
char c[M];
int ans,n,f[][],s[],last[],p[][];
int main(){
n=read(); scanf("%s",c+);
for(int i=;i<=n;i++){
int now=c[i]-'a';
s[now]++; last[now]=i;
for(int j=;j<;j++){
if(j!=now&&s[j]) ans=max(ans,(s[now]-s[j])-f[now][j]-(p[now][j]==last[j]));
if(j!=now&&s[j]) ans=max(ans,(s[j]-s[now])-f[j][now]-(p[j][now]==last[now]));
}
for(int j=;j<;j++){
if(f[j][now]>s[j]-s[now]) f[j][now]=s[j]-s[now],p[j][now]=i;
if(f[now][j]>s[now]-s[j]) f[now][j]=s[now]-s[j],p[now][j]=i;
}
}
printf("%d\n",ans);
return ;
}
bzoj 2213: [Poi2011]Difference的更多相关文章
- BZOJ2213: [Poi2011]Difference
2213: [Poi2011]Difference Time Limit: 10 Sec Memory Limit: 32 MBSubmit: 343 Solved: 108[Submit][St ...
- BZOJ 2530 Poi2011 Party 【枚举】
BZOJ 2530 Poi2011 Party Description Byteasar intends to throw up a party. Naturally, he would like i ...
- 【BZOJ2213】[Poi2011]Difference DP
[BZOJ2213][Poi2011]Difference Description A word consisting of lower-case letters of the English alp ...
- [bzoj 2216] [Poi2011] Lightning Conductor
[bzoj 2216] [Poi2011] Lightning Conductor Description 已知一个长度为n的序列a1,a2,-,an. 对于每个1<=i<=n,找到最小的 ...
- 洛谷 3519 && bzoj 2213 Difference
联考考试考到了这个题,随机化40分,现在来秒掉它吧. 题意: 给一个字符串,求其中的一段,使得出现次数最多的字符与出现次数最少的字符的出现次数之差最大. 输入输出样例 输入样例#1: 复制 10 aa ...
- [BZOJ 2212] [Poi2011] Tree Rotations 【线段树合并】
题目链接:BZOJ - 2212 题目分析 子树 x 内的逆序对个数为 :x 左子树内的逆序对个数 + x 右子树内的逆序对个数 + 跨越 x 左子树与右子树的逆序对. 左右子树内部的逆序对与是否交换 ...
- [BZOJ 2350] [Poi2011] Party 【Special】
题目链接: BZOJ - 2350 题目分析 因为存在一个 2/3 n 大小的团,所以不在这个团中的点最多 1/3 n 个. 牺牲一些团内的点,每次让一个团内的点与一个不在团内的点抵消删除,最多牺牲 ...
- BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )
线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...
- bzoj 2217 [Poi2011]Lollipop 乱搞 贪心
2217: [Poi2011]Lollipop Time Limit: 15 Sec Memory Limit: 64 MBSec Special JudgeSubmit: 383 Solved ...
随机推荐
- springmvc 路由
工作中MVC是较常使用的web框架,作为研发人员,也习惯了以编写Controller作为项目开始,写好了Controller和对应的方法,加上@RequestMapping注解,我们也就认为一切已经准 ...
- IT行业所面临的问题
在阅读了“2015 IT行业大学生就业分析报告”和“2014年十大最热门行业和职业排行榜 IT行业最吃香_联展新闻”两则新闻后,我决定用一篇和老师对话的形式来表达我的感受. dym:人潮汹涌的招聘市场 ...
- HDU 2164 Rock, Paper, or Scissors?
http://acm.hdu.edu.cn/showproblem.php?pid=2164 Problem Description Rock, Paper, Scissors is a two pl ...
- Win2019 + Oracle18c SQLPLUS 命令行出现乱码的解决
1. Win2019 中文版 安装了 Oracle数据库, dbca 建库时选择的 的字符集是 ZHS16GBK 然后发现使用sqlplus 时有乱码的现象如图示: 2. csdn 上面有一个博客有解 ...
- [LeetCode] [LeetCode] Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- 第99天:CSS3中透视perspective
CSS3中透视perspective 透视原理: 近大远小 . 浏览器透视:把近大远小的所有图像,透视在屏幕上. 理解浏览器的坐标系:浏览器平面为 Z=0的平面,坐标原点默认为图片的中心,可以通过更改 ...
- POJ 3276 Face The Right Way(前缀和优化)
题意:有长度为N的01串,有一个操作可以选择连续K个数字取反,求最小的操作数和最小的K使得最后变成全1串.(N<=5000) 由于K是不定的,无法高斯消元. 考虑枚举K,求出最小的操作数. 显然 ...
- 多线程---handlerthread
当我们需要工作线程来操作的时候,很多时候会有同步问题,UI更新问题. Handle机制就是为了解决这个问题而产生的. android允许每个线程都有自己的消息队列,同时也可以是主线程消息队列. 但是很 ...
- OSPF协议介绍及配置 (下)
4.特殊区域详解 为了让我们的讲解更加的通俗易懂,我们看上面这个拓扑,这是一个根据客户业务逻辑结构所涉及的OSPF网络,共有三个区域(实际上远远不止),骨干区域area0为一级行及二级行所部署,*** ...
- QT样式表
QT样式表 一.QT样式表简介 1.QT样式表简介 QSS的主要功能是使界面的表现与界面的元素分离,使得设计皮肤与界面控件分离的软件成为可能. QT样式表是允许用户定制widgets组件外观的强大机制 ...