Santa Claus and a Palindrome

Time Limit: 20 Sec  Memory Limit: 512 MB

Description

  有k个串,串长都是n,每个串有一个ai的贡献。
  选出若干个串,若它们可以通过任意组合,形成一个回文串,则可以获得它们的贡献之和。
  求最大贡献。

Input

  第一行两个整数k,n。
  之后k行,每行分别是一个串si,与贡献ai。

Output

  一个整数表示答案。

Sample Input

  7 3
  abb 2
  aaa -3
  bba -1
  zyz -4
  abb 5
  aaa 7
  xyx 4

Sample Output

  12

HINT

  1 ≤ k, n ≤ 100000;  n·k  ≤ 100000;  -10000 ≤ ai ≤ 10000

Solution

  首先,我们先考虑选了偶数个串的情况。显然是每两个互相颠倒的串匹配,尽量选大的值。

  但是现在可能选奇数个串,就是考虑把一个回文串放在中间,显然有两种情况:

    1. 匹配完还剩若干串,在剩下的串单独选了个回文串,显然加上最大的贡献即可;

    2. 把之前某些回文串两两匹配的给拆开改成只选较大的那一个,因为只选一个串可能贡献更大,A > (A + B)

  具体实现我们可以用一个Map指向一个vector,vector存下价值Map[s]=id表示s这个串用第id个vector记录信息

Code

 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
typedef long long s64; const int ONE = ;
const int MOD = 1e9 + ;
const int Base = ; int k, n; int total = ;
map <string, int> id;
vector <int> A[ONE]; int Ans; char s[ONE];
struct power
{
string s;
int val;
}a[ONE];
bool cmp(const power &a, const power &b) {return a.val > b.val;} int get()
{
int res=,Q=; char c;
while( (c=getchar())< || c>)
if(c=='-')Q=-;
if(Q) res=c-;
while((c=getchar())>= && c<=)
res=res*+c-;
return res*Q;
} int main()
{
k = get(); n = get();
for(int i = ; i <= k; i++)
cin>>a[i].s, a[i].val = get(); sort(a + , a + k + , cmp); int maxx = ;
for(int i = ; i <= k; i++)
{
for(int j = ; j < n; j++)
s[n - - j] = a[i].s[j]; int to = id[string(a[i].s)]; if(to && A[to].size())
{
if(A[to][] - Base + a[i].val > )
{
if(to == id[string(s)]) maxx = max(maxx, max(a[i].val, A[to][] - Base) - (A[to][] - Base + a[i].val));
Ans += A[to][] - Base + a[i].val;
A[to].erase(A[to].begin());
continue;
}
} to = id[string(s)];
if(!to) to = id[string(s)] = ++total; A[to].push_back(a[i].val + Base);
} int res = ;
for(int i = ; i <= k; i++)
{
int pd = ;
for(int j = ; j < n; j++)
if(a[i].s[n - - j] != a[i].s[j]) {pd = ; break;}
if(pd == ) continue; int to = id[a[i].s];
if(A[to].size() >= )
res = max(res, A[to][] - Base);
} printf("%d", max(Ans + res, Ans + maxx));
}

【Codeforces752D】Santa Claus and a Palindrome [STL]的更多相关文章

  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. Santa Claus and a Palindrome

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

  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. 【BZOJ4099】Trapped in the Haybales Gold STL

    [BZOJ4099]Trapped in the Haybales Gold Description Farmer John has received a shipment of N large ha ...

  5. [CF752D]Santa Claus and a Palindrome(优先队列,贪心乱搞)

    题目链接:http://codeforces.com/contest/752/problem/D 题意:给长度为k的n个字符串,每一个字符串有权值,求构造一个大回文串.使得权值最大. 因为字符串长度都 ...

  6. 【CF】7 Beta Round D. Palindrome Degree

    manacher+dp.其实理解manacher就可以解了,大水题,dp就是dp[i]=dp[i>>1]+1如何满足k-palindrome条件. /* 7D */ #include &l ...

  7. Codeforces 748D Santa Claus and a Palindrome

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

  8. 【leetcode】564. Find the Closest Palindrome

    题目如下: 解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的.要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设n=1234,1和4对称所以把4变成1 ...

  9. cf 478D.Santa Claus and a Palindrome

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

随机推荐

  1. web登录密码加密

    文章:如何实现登录页面密码加密 文章:用RSA加密实现Web登录密码加密传输 文章:web登录用户名密码加密 知乎文章:Web前端密码加密是否有意义? 文章:记录一次黑客模拟攻击 成功拿到淘宝账号和密 ...

  2. Alpha-6

    前言 失心疯病源6 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 今天完成了那些任务 18:30~20:30 完成blob类下关于预测车辆下一个位置的函数 代码签入github ...

  3. lintcode-477-被围绕的区域

    477-被围绕的区域 给一个二维的矩阵,包含 'X' 和 'O', 找到所有被 'X' 围绕的区域,并用 'X' 填充满. 样例 给出二维矩阵: X X X X X O O X X X O X X O ...

  4. 使用Logstash同步数据至Elasticsearch,Spring Boot中集成Elasticsearch实现搜索

    安装logstash.同步数据至ElasticSearch 为什么使用logstash来同步,CSDN上有一篇文章简要的分析了以下几种同步工具的优缺点:https://blog.csdn.net/la ...

  5. UML之Enterprise Architect使用

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:UML之Enterprise Architect使用     本文地址:http://tech ...

  6. 第94天:CSS3 盒模型详解

    CSS3盒模型详解 盒模型设定为border-box时 width = border + padding + content 盒模型设定为content-box时 width = content所谓定 ...

  7. UVA11653_Buses

    这个题目很有意思,一不小心就会让人坑在里面. 题意是这样的,给你n,k,l.分别表示总共的长度,长度为5和10的车的不同颜色数量现在问你要把n的填满有多少种方案. 很多人一开始都会脑子一根筋地想用排列 ...

  8. Command Network OpenJ_Bailian - 3436(最小有向生成树模板题)

    链接: http://poj.org/problem?id=3164 题目: Command Network Time Limit: 1000MS   Memory Limit: 131072K To ...

  9. TCP的拥塞控制 (二)

    TCP Reno TCP  Reno引入了ssthresh(Slow Start threshold)变量,作为TCP的Slow Start和Congestion Avoidance两个阶段的分界线. ...

  10. SCWS中文分词,向xdb词库添加新词

    SCWS是个不错的中文分词解决方案,词库也是hightman个人制作,总不免有些不尽如人意的地方.有些词语可能不会及时被收入词库中. 幸好SCWS提供了词库XDB导出导入词库的工具(phptool_f ...