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的更多相关文章

  1. Misha and Palindrome Degree CodeForces - 501E (回文串计数)

    大意: 给定字符串, 求多少个区间重排后能使原串为回文串. 先特判掉特殊情况, 对于两侧已经相等的位置之间可以任意组合, 并且区间两端点至少有一个在两侧相等的位置处, 对左右两种情况分别求出即可. # ...

  2. Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp

    题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...

  3. Codeforces Beta Round #7 D. Palindrome Degree hash

    D. Palindrome Degree 题目连接: http://www.codeforces.com/contest/7/problem/D Description String s of len ...

  4. Palindrome Degree(hash的思想题)

    个人心得:这题就是要确定是否为回文串,朴素算法会超时,所以想到用哈希,哈希从左到右和从右到左的key值一样就一定是回文串, 那么问题来了,正向还能保证一遍遍历,逆向呢,卡住我了,后面发现网上大神的秦九 ...

  5. Codeforces Beta Round #7 D. Palindrome Degree —— 字符串哈希

    题目链接:http://codeforces.com/contest/7/problem/D D. Palindrome Degree time limit per test 1 second mem ...

  6. codeforces7D Palindrome Degree(manacher&amp;dp或Hsh&amp;dp)

    D. Palindrome Degree time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. 【CF】7 Beta Round D. Palindrome Degree

    manacher+dp.其实理解manacher就可以解了,大水题,dp就是dp[i]=dp[i>>1]+1如何满足k-palindrome条件. /* 7D */ #include &l ...

  8. CodeForces - 7D Palindrome Degree

    最近接触了一点字符串算法,其实也就是一个简单的最大回文串算法,给定字符串s,求出最大字符串长度. 算法是这样的, 用'#'将s字符串中的每个字符分隔,比如s = "aba",分割后 ...

  9. CodeForces 7D Palindrome Degree 字符串hash

    题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...

随机推荐

  1. ELK采集之nginx 之高德地图出城市IP分布图

    1.采用拓扑: 角色扮演: Agent:采用logstash,IP:192.168.10.7 Redis队列: IP:192.168.10.100 Indexer:logstash,IP:192.16 ...

  2. Unity在安卓的一些路径

    APK安装之后找不到路径 公司的测试机(安卓)基本都是不带SD卡的. APK在安卓手机上安装之后,使用手机助手类的软件打开文件管理,打开 内置SDK卡/Android/data/ 在这个目录下却发现 ...

  3. c# PictureBox 的图像上使用鼠标画矩形框

    C# 中在图像上画框,通过鼠标来实现主要有四个消息响应函数MouseDown, MouseMove, MouseUp, Paint重绘函数实现.当鼠标键按下时开始画框,鼠标键抬起时画框结束. Poin ...

  4. Java 基础知识(一)

    Java基础知识篇: 一.关键字解释 1. final:修饰非抽象类,非抽象方法和属性, 以及修饰方法参数,代表“无法改变的”.出于对设计或者效率的考虑使用该关键字. final类无法被继承,fina ...

  5. NGINX----源码阅读----init配置脚本

    /auto/init init脚本负责初始化各种目录环境变量值. 1.make文件.源文件.头文件.配置头文件路径变量初始化. NGX_MAKEFILE=$NGX_OBJS/Makefile NGX_ ...

  6. 2016-2017 CT S03E02: Codeforces Trainings Season 3 Episode 2

    A HHPaint B Square Root C Interesting Places D Road to Home E Ant and apples F Square G Pair H The F ...

  7. swift3 UIColor扩展

    //返回一个RGB颜色 class func getColor(r:CGFloat,g:CGFloat,b:CGFloat,l:CGFloat = 1) ->UIColor{ let color ...

  8. Thymeleaf 3与Spring MVC 4 整合配置

    Thymeleaf 3与Spring MVC 4 整合配置 Maven 依赖配置 Spring 相关依赖就不说了 <dependency> <groupId>org.thyme ...

  9. iScroll 下 a 标签失效

    遇到个莫名其妙的问题,iScroll 下的 a 标签点击没有反应了,不管怎么调整 z-index 都无效果,很是无语. 查找半天后找到解决方法: $(function(){ new IScroll(' ...

  10. FZU 2238 Daxia & Wzc's problem

    公式. $a×C_{m + i - 1}^m + d×C_{m + i - 1}^{m + 1}$. 推导过程可以看http://blog.csdn.net/queuelovestack/articl ...