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. Java中Animal b = new Dog();Dog c = new Dog();的区别

    由于在编译阶段,只是检查参数的引用类型.然而在运行时,Java虚拟机(JVM)指定对象的类型并且运行该对象的方法.因此在下面的例子中,b.move()之所以能编译成功,是因为Animal类中存在mov ...

  2. phpwamp单身狗模式的详解与分析,单身狗模式/即霸体模式的作用讲解。

    最近有学生在使用PHPWAMP的时候,问我霸体模式的作用. 学生问:老师,PHPWAMP里面的霸体模式是什么意思,有什么用? 回答:额...就是以前我发布测试版的时候,模式切换里面的单身狗模式. 学生 ...

  3. unity 内置的CG结构解析

    一.Cg顶点程序必须在结构中传递顶点数据.几种常用的顶点结构定义在文件UnityCG.cginc中.在大部分情况下仅仅使用它们就够了.结构如下: 1.appdata_base: 包含顶点位置,法线和一 ...

  4. CodeForces 645A Amity Assessment

    简单模拟. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #incl ...

  5. git stash让bug来的更猛烈些吧

    git stash可以用来暂存当前正在进行的工作,比如想pull最新的代码,又不想加新commit, 或者有一个紧急的bug需要修复,但是这个bug又与你已经在做的工作(还没完成)有关联.这个时候有的 ...

  6. Haskell Seq函数和严格计算

    参考链接http://stackoverflow.com/questions/11046590/the-seq-function-and-strictness https://wiki.haskell ...

  7. nginx如何配置网页错误页面

    首先要在http模块中加入 fastcgi_intercept_errors on; 其次要在server模块中加入 error_page 403 404 /40x.html; location = ...

  8. Linux网络常用头文件说明

    sys/types.h:数据类型定义 sys/socket.h:提供socket函数及数据结构 netinet/in.h:定义数据结构sockaddr_in arpa/inet.h:提供IP地址转换函 ...

  9. mybatis学习笔记三(关联关系)

    学习mybatis的关联关系,主要注解在代码上,这里不做解释.配置文件一样的就不贴了 1.关联关系表创建(学生对应老师 多对一) 学生老师表 2.表对应的实体类 package com.home.en ...

  10. linuxlab下虚拟板与主机通信