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 ...
随机推荐
- Ionic2系列——在Ionic2中使用高德地图
之前讲过了如何在Ionic2中使用第三方库,因为第三方库必须针对TypeScript提供相应的声明文件——即d.ts文件,才能被TypeScript正确识别并编译.好在大多数的第三方库已经有了定义文件 ...
- Santa Claus and a Palindrome
Santa Claus and a Palindrome 题目链接:http://codeforces.com/contest/752/problem/D 贪心 很自然地,可以想到,若subS不是回文 ...
- 批处理+组策略 实现规定时间段无法开机and定时关机
某爱熬夜的人对付自己的东西 1.shutdown命令 shutdown -a #取消现有的shutdown计划 shutdown -s -t [time] #设定时间关机 shutdown -r -t ...
- ng1 http 读取json数据
在前端开发过程中,有时后端还没开发出接口,需要经常自己构造获取本地mock数据. AngularJS XMLHttpRequest $http 是 AngularJS 中的一个核心服务,用于读取远程服 ...
- tomcat部署war包时连接被重置(修改tomcat上传限制)
相对目录:apache-tomcat-7.0.67/webapps/manager/WEB-INF/web.xml 500M的计算:500*1024*1024 <multipart-config ...
- feature2d相关
1.Harris角点检测 是基于灰度图像的角点检测. 灰度变化率函数如下: 其中的w(x,y)为加权函数,可为常数或为高斯函数.之后对E(u,v)进行泰勒级数的展开与化简,最终得到 ,,Ix,Iy是图 ...
- ERRO错误解决方案整理
1.chorm访问本地数据出现跨域问题 Cross origin requests are only supported for protocol schemes: http, data, chrom ...
- C语言 · 分解质因数
问题描述 求出区间[a,b]中所有整数的质因数分解. 输入格式 输入两个整数a,b. 输出格式 每行输出一个数的分解,形如k=a1*a2*a3...(a1<=a2<=a3...,k也是从小 ...
- apache 配置静态文件缓存和开启gzip压缩
1,设置文件静态缓存3天: 在httpd.conf 里添加如下代码: #文件静态缓存配置 <IfModule expires_module> #打开缓存 ExpiresActive on ...
- Php 关于构造函数
子类在继承父类后,会拥有父类的属性和方法,这是继承的特性. 子类在构造函数会首先调用父类的构造函数来实例化父类的属性,然后调用子类的构造函数,一般你不写,并不表示没有调用,而是首先调用了父类的无参构造 ...