题目链接: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. C#读取Excel表格数据到DataGridView中和导出DataGridView中的数据到Excel

    其实想在datagridview中显示excel表格中的数据跟读取数据库中的数据没什么差别,只不过是创建数据库连接的时候连接字段稍有差别. private void btnShow_Click(obj ...

  2. css补充、JavaScript、Dom

    css补充: position: fixed:可以将标签固定在页面的某个位置 absolute+relative:通过两者的结合可以让标签在一个相对的位置 代码例子:(通过fixed标签将某些内容固定 ...

  3. React 基础入门,基础知识介绍

    React不管在demo渲染还是UI上,都是十分方便,本人菜鸟试试学习一下,结合阮一峰老师的文章,写下一点关于自己的学习react的学习笔记,有地方不对的地方,希望各位大牛评论指出: PS:代码包下载 ...

  4. workerman centos 7 开机自动启动

    第一步: vim /lib/systemd/system/workerman.service 第二步:复制以下代码保存退出,注意修改你的workerman路径 [Unit] Description=w ...

  5. 在Page_Loaded下删除PivotItem出错的解决方案

    之前我一个例子中出现无法再页面Loaded事件中删除PivotItem的情况,页面会报错 Value does not fall within the expected range. 附图 原因是因为 ...

  6. No compiler is provided in this environment. Perhaps you are running on a JRE ra

    No compiler is provided in this environment. Perhaps you are running on a JRE ra,有需要的朋友可以参考下. 控制台输出的 ...

  7. Android--RecyclerView的封装使用

    1,用了很长一段时间的RecyclerView,在项目中用的频率也越来越频繁(因为踩得坑也越来越多了),或过头来看,感觉一直在写RecyclerView.Adapter中的三个方法和一个内部类,感觉很 ...

  8. 关于header跳转之后的乱码

    http://www.360doc.com/content/11/0603/19/7052474_121495648.shtml 问题:不同网站的跳转出现乱码,不同编码的页面传递参数也出现乱码 搞清楚 ...

  9. mysql 查询行号

    SELECT @rowno:=@rowno+1 as rowno,r.* from t_zg_loanee_apply r,(select @rowno:=0) t;

  10. .net实现webservice简单实例分享

    原理:WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Http协议的网络应用间的交互.作用:主要用 ...