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. class 添加样式,删除,开关 【选择】addClass,removeClass,toggleClass

    <1> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>< ...

  2. Android项目---TouchListener

    public static interface View.OnTouchListener android.view.View.OnTouchListener Known Indirect Subcla ...

  3. shell删除指定时间之前的文件

    cat delbak.sh 1 #!/bin/sh 2 location="/root/sqlbak/" 3 find $location -mtime +30 -type f | ...

  4. asp.net mvc请求响应模型原理回顾

    根据讲师所讲总结了一下(可能存在些描述错误) -------------mvc进入asp.net管道原理: (在执行httpapplication管道之前mvc和asp.net是相同的,不同之处在于管 ...

  5. C#函数式程序设计之泛型(下)

    C#函数式程序设计之泛型(下)   每当使用泛型类型时,可以通过where字句对泛型添加约束: + 这个例子直观地声明了一个约束:类型T必须与ListItem<string>相匹配.泛型类 ...

  6. 深入剖析Provider Model

    Membership三步曲之进阶篇 - 深入剖析Provider Model Membership 三步曲之进阶篇 - 深入剖析Provider Model 本文的目标是让每一个人都知道Provide ...

  7. iOS基础 - 数据库-SQLite

    一.iOS应用数据存取的常用方式 XML属性列表 —— PList NSKeyedArchiver 归档 Preference(偏好设置) SQLite3 Core Data(以面向对象的方式操作数据 ...

  8. Binder机制,从Java到C (2. IPC in System Service :AMS)

    1.建立Activity和Service的IPC之前 在上一篇 Binder机制,从Java到C (1. IPC in Application Remote Service)  里面有说到Activi ...

  9. .net中的emit

      .net中的emit .net中的emit命名空间是system.reflection.emit.可见,emit是作为反射的一个比较高级的功能. 反射允许我们在运行时获取对象的相关信息,并执行对象 ...

  10. oauth与openid

    转自http://desert3.iteye.com/blog/1701626 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列 ...