Misha and Palindrome Degree
Misha and Palindrome Degree
题目链接:http://codeforces.com/problemset/problem/501/E
贪心
如果区间[L,R]满足条件,那么区间[L',R'](L'<=L,R<=R')必然满足条件,所以只需要找到满足条件的最小区间即可。首先去除两边相同的区间,剩下的区间为[l,r],因为区间[l,r]的两端不相同,所以要找的最小区间必然包含区间[l,r]的最左端或者最右端。观察到所选区间内的同种元素个数必需大于等于整个区间内同种元素的个数,以此来找到最小区间。
计算区间个数的示意图:

代码如下:
#include<cstdio>
#include<cstring>
#define N 100000
#define LL long long
using namespace std;
LL a[N+];
LL cnt[N+];
LL jud[N+];
LL n,l,r,single,sum;
int main(void){
scanf("%I64d",&n);
for(LL i=;i<n;++i){
scanf("%I64d",a+i);
cnt[a[i]]++;
}
for(LL i=;i<=n;++i)
if(cnt[i]&)single++;
if(single>){
printf("0\n");
return ;
}
for(l=;l<=(n>>);++l){
if(a[l]==a[n--l])cnt[a[l]]-=;
else break;
}
r=n--l;
if(l>=r){
printf("%I64d\n",n*(n+)/);
return ;
}
LL left=r;
for(;left>=l;--left){//缩小(l,left)的区间
jud[a[left]]++;//统计(left,r)中的元素个数
if(jud[a[left]]*>cnt[a[left]]){
if(left>((n-)>>))break;//区间无法继续缩小
if(a[left]!=a[n--left])break;//如果相等,区间可以更小
if(cnt[a[left]]%==&&left==n--left)break;//如果是中间奇数的,继续缩小
}
}
sum+=(r-left)*(l+);
memset(jud,,sizeof(jud));
int right=l;
for(;right<=r;++right){
jud[a[right]]++;
if(jud[a[right]]*>cnt[a[right]]){
if(right<((n-)>>))break;
if(a[right]!=a[n--right])break;
if(cnt[a[right]]%==&&right==n--right)break;
}
}
sum+=(right-l)*(n-r);
sum+=(n-r)*(l+);
printf("%I64d\n",sum);
}
Misha and Palindrome Degree的更多相关文章
- Misha and Palindrome Degree CodeForces - 501E (回文串计数)
大意: 给定字符串, 求多少个区间重排后能使原串为回文串. 先特判掉特殊情况, 对于两侧已经相等的位置之间可以任意组合, 并且区间两端点至少有一个在两侧相等的位置处, 对左右两种情况分别求出即可. # ...
- Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp
题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...
- Codeforces Beta Round #7 D. Palindrome Degree hash
D. Palindrome Degree 题目连接: http://www.codeforces.com/contest/7/problem/D Description String s of len ...
- Palindrome Degree(hash的思想题)
个人心得:这题就是要确定是否为回文串,朴素算法会超时,所以想到用哈希,哈希从左到右和从右到左的key值一样就一定是回文串, 那么问题来了,正向还能保证一遍遍历,逆向呢,卡住我了,后面发现网上大神的秦九 ...
- Codeforces Beta Round #7 D. Palindrome Degree —— 字符串哈希
题目链接:http://codeforces.com/contest/7/problem/D D. Palindrome Degree time limit per test 1 second mem ...
- codeforces7D Palindrome Degree(manacher&dp或Hsh&dp)
D. Palindrome Degree time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【CF】7 Beta Round D. Palindrome Degree
manacher+dp.其实理解manacher就可以解了,大水题,dp就是dp[i]=dp[i>>1]+1如何满足k-palindrome条件. /* 7D */ #include &l ...
- CodeForces - 7D Palindrome Degree
最近接触了一点字符串算法,其实也就是一个简单的最大回文串算法,给定字符串s,求出最大字符串长度. 算法是这样的, 用'#'将s字符串中的每个字符分隔,比如s = "aba",分割后 ...
- CodeForces 7D Palindrome Degree 字符串hash
题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...
随机推荐
- Struts入门(二) 配置文件的讲解
上一章我们演示了Struts项目的搭建 可以看到里面有几个重要的配置文件 下面我们来说明一下这3个配置文件 1.web.xml 2.strtus.xml 3.struts.properties 1 ...
- printf输出格式
tips:#为空格 Printf 格式化字符串 格式代码 A ABC ABCDEFGH %S A ABC ABCDEFGH %5S ...
- git 本地给远程仓库创建分支 三步法
命令如下: 1:本地创建分支dev Peg@PEG-PC /D/home/myself/Symfony (master) $ git branch dev 2:下面是把本地分支提交到远程仓库 Peg@ ...
- Python操作redis、memcache和ORM框架_Day13
一.memcache Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速 ...
- Scala 中的 apply 和 update 方法[转]
原文链接:http://blog.csdn.net/lyrebing/article/details/21696581 Scala 是构建在 JVM 上的静态类型的脚本语言,而脚本语言总是会有些约定来 ...
- dplyr 数据操作 常用函数(1)
上面介绍完dplyr中,几个主要的操作函数后,我们再进一步了解dplyr中那些函数可能我们会经常要用到. 这里主要根据dplyr包作者的书籍目录来把它列出来. 1.add_rownames 添加行名称 ...
- MAMP、wordpress安装
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; font: 12.0px Helvetica } p.p4 { margin: ...
- 线性规划?数学?差分约束?Good Bye 2016 C
http://codeforces.com/contest/750/problem/C 反正我不会这道题...为什么那么多人做出来了...我好菜.jpg 题目大意:cf每个人都有分数,每次都会在div ...
- mssql update from
update b set memo = a.name from a,b where a.id = b.id --mssql的update :from语法 2 --a表 b表 结构分别 id ,name ...
- assembly打包实例
1.先在pom.xml文件中添加assembly打包插件 <build> <plugins> <plugin> <groupId>org.apache. ...