[CF752D]Santa Claus and a Palindrome(优先队列,贪心乱搞)
题目链接: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(优先队列,贪心乱搞)的更多相关文章
- Santa Claus and a Palindrome
Santa Claus and a Palindrome 题目链接:http://codeforces.com/contest/752/problem/D 贪心 很自然地,可以想到,若subS不是回文 ...
- 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 ...
- 【Codeforces752D】Santa Claus and a Palindrome [STL]
Santa Claus and a Palindrome Time Limit: 20 Sec Memory Limit: 512 MB Description 有k个串,串长都是n,每个串有一个a ...
- 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 ...
- CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)
题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...
- Codeforces 748D Santa Claus and a Palindrome
雅礼集训期间我好像考完试就开始划水了啊 给出k个长度相同的字符串,每个串有一个权值,选出一些串连成一个回文串.使得选中的串的总权值最大. 如果选一个串,必须同时选一个对称的串.还有一个特殊情况是可以在 ...
- cf 478D.Santa Claus and a Palindrome
原来set,priority_queue也可以映射..涨姿势2333 比较麻烦的应该就是判断自身回文的串是选2个还是选一个吧. #include<bits/stdc++.h> #defin ...
- CodeForces 509C Sums of Digits(贪心乱搞)题解
题意:a是严格递增数列,bi是ai每一位的和,告诉你b1~bn,问你怎样搞才能让an最小 思路:让ai刚好大于ai-1弄出来的an最小.所以直接模拟贪心,如果当前位和前一个数的当前位一样并且后面还能生 ...
- [luoguP1053] 篝火晚会(贪心 + 乱搞)
传送门 假设第一个位置是1,那么枚举它的左右两边是谁,有两种情况,然后可以递推求出序列. 然后可以贪心,两个序列有多少个不同的数,答案就是多少,具体为啥,yy一下即可 然后就是判断递推求出的序列和目标 ...
随机推荐
- Resolving SQL Server Disk IO bottlenecks
网上看到这篇文章挺不错的,直接翻译过来.在尝试诊断SQL Server性能时,不要仅仅依赖某个单一的诊断数据,比如CPU的使用率.SQL Server磁盘性能,就得出结论却忽略的问题的根源.实际上,使 ...
- Ubuntu Dev Box Setup
Editor VIM Sublime Atom Visual Studio Code SSH Client PAC Manager File Manager Double Commander Imag ...
- .NET 扩展方法(Extention Method)的要点
扩展方法Extention Method的主要介绍在:http://msdn.microsoft.com/zh-cn/library/bb383977(v=vs.100).aspx. 扩展方法的意义在 ...
- LL基本姿势
在说怎么练习之前,先说说LL这个游戏里面的一些基本概念: 谱面元素(element) 谱面由一个个的音符组成,在LL中,音符(Note)分以下三类: 单点(下简称S,Single),同一时刻只有一个圆 ...
- 如何将页面的<br/>在Excel中正确换行
在页面的<br />导致导出Excel中是会以多行的方式显示,达不到页面在一个单元格中进行换行,为此我们有以下两种方式: 1.CSS样式方式 <br style='mso-data- ...
- iptables四个表与五个链间的处理关系
转载自:http://www.linuxidc.com/Linux/2012-08/67505.htm netfilter/iptables IP 信息包过滤系统是一种功能强大的工具,可用于添加.编辑 ...
- 基于MDK的mbed工程建立
个人更喜欢mdk作为IDE来编写代码,而mbed作为一个开源项目,有大量优秀代码可以借鉴使用,今后一段时间都会主要看mbed平台的代码以及国内ebox平台代码 1 首先登陆mbed在 ...
- Oracle Partition By 的使用
1.概述 Parttion by 关键字是Oracle中分析性函数的一部分,它和聚合函数不同的地方在于它能够返回一个分组中的多条记录,儿聚合函数一般只有一条反映统计值的结果. 2.使用方式 场景:查询 ...
- kthread_run【转】
转自:http://blog.csdn.net/zhangxuechao_/article/details/50876397 头文件 include/linux/kthread.h 创建并启动 /** ...
- Spring实现IOC
目录 一.使用XML配置的方式实现IOC 二.使用Spring注解配置IOC 三.自动装配 四.零配置实现IOC 五.示例下载 控制反转IoC(Inversion of Control),是一种设计思 ...