[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一下即可 然后就是判断递推求出的序列和目标 ...
随机推荐
- Linux 下应用程序最大打开文件数的理解和修改
运行在Linux系统上的Java程序运行了一段时间后出现"Too many open files"的异常情况. 这种情况常见于高并发访问文件系统,多线程网络连接等场景.程序经常访问 ...
- SpringSecurity操作指南-基于Spring、SpringMVC和MyBatis自定义SpringSecurity权限认证规则
- centos7 搭建GlusterFS
centos7 搭建GlusterFS 转载http://zhaijunming5.blog.51cto.com/10668883/1704535 实验需求:4台机器安装GlusterFS组成一个集群 ...
- 如何使用JDBC链接数据库
1.加载数据库驱动 不同的数据库加载的驱动不一样 Class.forName(com.MySQL.jdbc.Driver) Class.forName(oracle.jdbc.driver.Oracl ...
- hp-pa安装oracle和bash
一.安装oracle数据库 安装之前先进行环境检查: 1.检查系统版本 #uname -a 2.查看内存大小 #/usr/contrib/bin/machinfo | grep -i Memory 3 ...
- 在IE下,如果在readonly的input里面键入backspace键,会触发history.back()
在IE下,如果在readonly的input里面键入backspace键,会触发history.back(), 用以下jQuery代码修正之 $("input[readOnly]" ...
- java 反射机制
1.反射机制是什么: (1)反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能 ...
- CentOS双网卡绑定bond0
a)拷⻉并配置vim /etc/sysconfig/network-scripts/ifcfg-bond0配置⽂件(会自动创建文件) DEVICE=bond0 TYPE=Ethernet ONBOOT ...
- DataList删除操作
<asp:DataList ID="fileList" runat="server" RepeatColumns="1" Repeat ...
- Python实现冒泡排序
array = [1,2,3,6,5,4] for i in range(len(array)): for j in range(i): if array[j] > array[j + 1]: ...