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 ...
随机推荐
- C语言之printf函数
一 基本用法 格式化控制符:%d %c %ld %lf 意思是:相当于在要输出的语句里面挖了一个坑,也就是在内存中开辟空间,然后再那个坑的位置(也就是开辟好的空间),填上你想要显示的值 printf ...
- Python自动化开发-变量、数据类型和运算
一.变量 变量定义:Variables are used to store infomation to referrenced and manipulated in a computer progra ...
- 【转】PYTHON open/文件操作
[注]虽是转载,但会在原文上有些修改! open/文件操作f=open('/tmp/hello','w')#open(路径+文件名,读写模式)#读写模式:r只读,r+读写,w新建(会覆盖原有文件),a ...
- 3-this关键字
1.表示类中的属性和调用方法 package com.example; /** * Created by Y on 16/4/13. */ public class People { private ...
- redis服务器安装-SuSE Linux Enterprise Server 11 SP3
一.下载 官网下载,可自选版本,点击进入下载,这里下载了redis-3.2.4 放到 /root/usr/local/redis/ 目录下 二.编译 1. 执行make编译redis tar -zxz ...
- 【LeetCode】25. Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- Git创建空白新分支
向分支提交一个初始的空commit,保证完全复位. 创建并切换新分支 git branch <new_branch> git checkout <new_branch> git ...
- hibernate内部测试题总结
在Hibernate中,关于脏检查和刷新缓存说法正确的是(ab ). A.当事务提交时,会发生脏检查 B.Session的flush( )方法是刷新缓存的方法 C.在执行Session的commit( ...
- OllyDBG V1.10聆风听雨汉化版
软件名称:OllyDBG V1.10聆风听雨汉化版 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 3.84MB 图片预览: 软件简介: Ollydbg2. ...
- VsVim的快捷键使用
.插入命令(可配合数字使用) i 在当前位置前插入 I 在当前行首插入 a 在当前位置后插入 A 在当前行尾插入 o 在当前行之后插入一行 O 在当前行之前插入一行 ni/a/o/I/A/O<E ...