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

题意:给长度为k的n个字符串,每一个字符串有权值,求构造一个大回文串。使得权值最大。

因为字符串长度都一样,所以想构成回文串,必须两两配对,在中间加或者不加一个本身就是回文的字符串。

1.考虑非回文串的配对,把所有可以构成回文的非回文串凑起来,必须两两权值和>0才有意义。那么就扔到优先队列里配对。

2.考虑回文串的配对,配对成功的话要看下是不是两个回文串都是>0,如果不是,要额外在一个vector里记下,方便后面用。

3.这时候挑一个>0的,备选放在中间。

比较上面的2.3,求最优。

 #include <bits/stdc++.h>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pll;
const int maxn = ;
int n, k, tmp;
char s[maxn];
vector<string> str, rev;
map<string, priority_queue<LL> > wt; int main() {
// freopen("in", "r", stdin);
while(~scanf("%d%d",&n, &k)) {
wt.clear(); str.clear(); rev.clear();
for(int i = ; i < n; i++) {
scanf("%s %d", s, &tmp);
str.push_back(s);
rev.push_back(s);
reverse(rev[i].begin(), rev[i].end());
wt[s].push(tmp);
}
LL ret = ;
for(int i = ; i < str.size(); i++) {
if(str[i] != rev[i]) {
while(wt[str[i]].size() > && wt[rev[i]].size() > ) {
int x = wt[str[i]].top();
wt[str[i]].pop();
int y = wt[rev[i]].top();
wt[rev[i]].pop();
if(x + y > ) ret += x + y;
else {
if(x > ) wt[str[i]].push(x);
if(y > ) wt[rev[i]].push(y);
break;
}
}
}
}
vector<pll> tmp;
for(int i = ; i < str.size(); i++) {
if(str[i] == rev[i]) {
while(wt[str[i]].size() > ) {
int x = wt[str[i]].top();
wt[str[i]].pop();
int y = wt[str[i]].top();
wt[str[i]].pop();
if(x + y > ) {
ret += x + y;
if(x > && y > ) continue;
if(x > y) swap(x, y);
tmp.push_back(pll(x, y));
}
else {
if(x > ) wt[str[i]].push(x);
if(y > ) wt[str[i]].push(y);
break;
}
}
}
}
LL aa = maxn, bb;
for(int i = ; i < tmp.size(); i++) {
if(tmp[i].first < aa) {
aa = tmp[i].first;
bb = tmp[i].second;
}
// cout << tmp[i].first << " " << tmp[i].second << endl;
}
LL qq = ;
for(int i = ; i < str.size(); i++) {
if(str[i] == rev[i]) {
if(wt[str[i]].size() > ) {
if(wt[str[i]].top() > ) {
qq = max(qq, wt[str[i]].top());
}
}
}
}
if(aa == maxn) cout << max(ret + qq, ret) << endl;
else cout << max(max(ret, ret + qq), ret - LL(aa)) << endl;
}
return ;
}

[CF752D]Santa Claus and a Palindrome(优先队列,贪心乱搞)的更多相关文章

  1. Santa Claus and a Palindrome

    Santa Claus and a Palindrome 题目链接:http://codeforces.com/contest/752/problem/D 贪心 很自然地,可以想到,若subS不是回文 ...

  2. 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 ...

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

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

  4. 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 ...

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

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

  6. Codeforces 748D Santa Claus and a Palindrome

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

  7. cf 478D.Santa Claus and a Palindrome

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

  8. CodeForces 509C Sums of Digits(贪心乱搞)题解

    题意:a是严格递增数列,bi是ai每一位的和,告诉你b1~bn,问你怎样搞才能让an最小 思路:让ai刚好大于ai-1弄出来的an最小.所以直接模拟贪心,如果当前位和前一个数的当前位一样并且后面还能生 ...

  9. [luoguP1053] 篝火晚会(贪心 + 乱搞)

    传送门 假设第一个位置是1,那么枚举它的左右两边是谁,有两种情况,然后可以递推求出序列. 然后可以贪心,两个序列有多少个不同的数,答案就是多少,具体为啥,yy一下即可 然后就是判断递推求出的序列和目标 ...

随机推荐

  1. hadoop 性能调优与运维

    hadoop 性能调优与运维 . 硬件选择 . 操作系统调优与jvm调优 . hadoop运维 硬件选择 1) hadoop运行环境 2)  原则一: 主节点可靠性要好于从节点 原则二:多路多核,高频 ...

  2. 手机APP开发:学JAVA转安卓APP开发是不是很容易?

    成都亿合云商小编为您分享:Android开发是以Java语言为基础的,Android 虽然使用Java 语言作为开发工具,但是在实际开发中发现,还是与Java SDK 有一些不同的地方.Android ...

  3. Transaction Save Point (SET XACT_ABORT { ON | OFF })

    ref:http://blog.csdn.net/wym3587/article/details/6940630 ref:http://www.cnblogs.com/jiajiayuan/archi ...

  4. 限制action所接受的请求方式或请求参数

    原文:http://www.cnblogs.com/liukemng/p/3726897.html 2.限制action所接受的请求方式(get或post): 之前我们在HelloWorldContr ...

  5. hibernate的映射类型

    hibernate的映射类型 hibernate MySQL映射类型 1.Hibernate的映射类型 hibernate mysql映射类型 Hibernate 映射类型 Java 类型 标准 SQ ...

  6. STL—Vector简介

    有关C++ STL 中的vector向量的用法(代码示例) 一. 简介 Vector是一个称为向量的顺序容器(不明白顺序容器与关联容器的可以Google). 二. 特点 1. 动态(相当于一个动态数组 ...

  7. Cef 架构

    cef支持各种语言和多种操作系统.在设计的时候充分考虑了性能和易用性.cef核心功能提供了c和c++的接口.cef提供了和主程序之间的通信能力(利用 custom plugins, protocols ...

  8. VS 与JIRA Bamboo的连接

    atlassian-vs-connector 可以百度下地址 一些配置     效果:        

  9. 【金】nginx+uwsgi+django+python 应用架构部署

    网上有很多这种配置,但就是没一个靠普的,费了好大的力气才完成架构部署.顺便记录一下. 一.部署前的说明 先安装好 python,django,uwsgi,nginx软件后.后配置运行的软件是分先后的. ...

  10. C语言 06 指针

    int *p; //第一个*是象征意义. p = &a; 等价于 int *p = &a; //第二个*是不正确的 *p = &a; //第三个*是访问指针变量指向的存储空间. ...