Santa Claus and a Palindrome

题目链接:http://codeforces.com/contest/752/problem/D

贪心

很自然地,可以想到,若subS不是回文串,那么只要贪心取subS中的最大值和reverse_subS中的最大值,若他们和大于零,则加入到结果回文序列中;而当subS本身就为回文串时,需要分类讨论(可以放在结果回文序列的最中间):

  为了方便理解,定义:

  • 若subS中的最大值与次大值的和小于零(或subS中只有一个值),那么称该最大值(或该值)为落单值
  • 若subS中最大值与次大值的和大于零,那么称该最大值与次大互为配对值

1.取落单值中最大的值与所有配对值;

2.取所有配对值,并将其中一组配对值拆分成落单值。

最后取这两种情况的最大值即可。

代码如下:

 #include<iostream>
#include<map>
#include<queue>
#include<string>
#include<algorithm>
#define X first
#define Y second
using namespace std;
typedef long long ll;
map<string,priority_queue<ll> >mp;
map<string,priority_queue<ll> >::iterator it;
ll n,k,t,ans;
string s;
int main(void){
std::ios::sync_with_stdio(false);
cin>>n>>k;
for(ll i=;i<n;++i){
cin>>s>>t;
mp[s].push(t);
}
for(it=mp.begin();it!=mp.end();++it){
ll x,y;
s=it->X;
if(it->Y.empty())continue;
reverse(s.begin(),s.end());
if(s.compare(it->X)==){
while(it->Y.size()>=){
x=it->Y.top();
it->Y.pop();
if(it->Y.top()>){
ans+=x+it->Y.top();
it->Y.pop();
}else{
it->Y.push(x);
break;
}
}
continue;
}
if(mp[s].empty())continue;
while(!it->Y.empty()&&!mp[s].empty()){
x=it->Y.top();
y=mp[s].top();
if(x+y>){
ans+=x+y;
it->Y.pop();
mp[s].pop();
}else break;
}
}
ll maxn=-;
for(it=mp.begin();it!=mp.end();++it){
s=it->X;
reverse(s.begin(),s.end());
if(s.compare(it->X)==){
if(it->Y.size()>=){
ll tmp=it->Y.top();
it->Y.pop();
if(it->Y.top()+tmp<=){
if(tmp>maxn)maxn=tmp;
}else it->Y.push(tmp);
}else if(it->Y.size()==){
ll tmp=it->Y.top();
it->Y.pop();
if(tmp>maxn)maxn=tmp;
}
}
}
ll tt1=,tt2=;
ll minn=;
if(maxn!=-){
tt1=maxn;
for(it=mp.begin();it!=mp.end();++it){
s=it->X;
reverse(s.begin(),s.end());
if(s.compare(it->X)==){
if(it->Y.size()>=){
ll t1=it->Y.top();
it->Y.pop();
ll t2=it->Y.top();
it->Y.pop();
if(t2+t1>)tt1+=t1+t2;
it->Y.push(t1);
it->Y.push(t2);
}
}
}
}
for(it=mp.begin();it!=mp.end();++it){
s=it->X;
reverse(s.begin(),s.end());
if(s.compare(it->X)==){
if(it->Y.size()>=){
ll t1=it->Y.top();
it->Y.pop();
ll t2=it->Y.top();
it->Y.pop();
if(t1+t2>=){
tt2+=t1+t2;
minn=min(minn,t2);
}
}
}
}
if(minn!=)tt2-=minn;
cout<<ans+max(tt1,tt2)<<endl;
}

Santa Claus and a Palindrome的更多相关文章

  1. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D. Santa Claus and a Palindrome STL

    D. Santa Claus and a Palindrome time limit per test 2 seconds memory limit per test 256 megabytes in ...

  2. 【Codeforces752D】Santa Claus and a Palindrome [STL]

    Santa Claus and a Palindrome Time Limit: 20 Sec  Memory Limit: 512 MB Description 有k个串,串长都是n,每个串有一个a ...

  3. Codeforces Round #389 Div.2 D. Santa Claus and a Palindrome

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. [CF752D]Santa Claus and a Palindrome(优先队列,贪心乱搞)

    题目链接:http://codeforces.com/contest/752/problem/D 题意:给长度为k的n个字符串,每一个字符串有权值,求构造一个大回文串.使得权值最大. 因为字符串长度都 ...

  5. Codeforces 748D Santa Claus and a Palindrome

    雅礼集训期间我好像考完试就开始划水了啊 给出k个长度相同的字符串,每个串有一个权值,选出一些串连成一个回文串.使得选中的串的总权值最大. 如果选一个串,必须同时选一个对称的串.还有一个特殊情况是可以在 ...

  6. cf 478D.Santa Claus and a Palindrome

    原来set,priority_queue也可以映射..涨姿势2333 比较麻烦的应该就是判断自身回文的串是选2个还是选一个吧. #include<bits/stdc++.h> #defin ...

  7. CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)

    题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...

  8. codeforces 748E Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  9. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) E. Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

随机推荐

  1. nagios 安装和配置(含有nrpe结束)所有 (两)

    二.ndoutils 安装: 1.mysql安装(若未安装) #apt-get install mysql-servermysql-client 2.DBI安装(若未安装) #cd /usr/loca ...

  2. 【转】怎么导出jar包

    如何导出jar包 右键工程->Export->Java->JAR file->Next-> Next 选中工程和工程中你要打包的内容,如果是Android的项目,需要把M ...

  3. Tomcat源码学习一

    这段时间工作不太忙,所以抽时间学习了TOMCAT, TOMCAT实际就是负责保持TCP连接传递到部署的项目中.浏览器实质就是TCP发送器.将用户的请求封装成TCP发送请求.当然格式是双方协定的.使用的 ...

  4. Unit Of Work-工作单元

    Unit Of Work-工作单元 阅读目录: 概念中的理解 代码中的实现 后记 掀起了你的盖头来,让我看你的眼睛,你的眼睛明又亮呀,好像那水波一模样:掀起了你的盖头来,让我看你的脸儿,看看你的脸儿红 ...

  5. Unity学习入门

    文章说明,文本内容基于配置文件进行依赖注入 unity介绍:Unity是由微软的Patterns & Practices团队开发的一个轻量级.可扩展的依赖注入(Dependency Injec ...

  6. C#单元测试工具包:MvcContrib

    C#单元测试工具包:MvcContrib http://t.cn/hE67d https://mvccontrib.codeplex.com/documentation MVC Contrib Doc ...

  7. 浅谈PHP在各系统平台下的换行符

    <?php echo 'aaa\n';//用于linux.unix平台C的换行也是如此 echo 'bbb\r';//用于mac平台 echo 'ccc\r\n';//用于windows平台 / ...

  8. D0

    刚到长乐就被机房里众大神的气场给压倒了 orz....... 然后默默的感觉到自己貌似已经有一个星期没有打题了...就各种忧伤.... 还是说一下今天的计划吧 嗯傍晚5.30-6.00 &&a ...

  9. ASP.NET页面之间传递值的几种方式(转载)

    页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有QueryString,Session,Cookie ...

  10. JS关闭当前页面的方法

    JS关闭当前页面的方法 一.不带任何提示关闭窗口的js代码 1 <a href="javascript:window.opener=null;window.open('','_self ...