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. Java包名命名规则(转载)

    转载自:http://lilinhai548.blog.163.com/blog/static/5847332920155132151359/ 鸣谢原作者  学习Java的童鞋们都知道,Java的包. ...

  2. js get selected text

    js get selected text https://stackoverflow.com/questions/3170648/how-to-get-javascript-select-boxs-s ...

  3. CentOS卸载系统自带的OpenJDK并安装Sun的JDK的方法

    查看目前系统的jdk: rpm -qa | grep jdk 得到的结果: [root@dc-01 java]#  rpm -qa | grep jdk java-1.6.0-openjdk-1.6. ...

  4. 自动化测试断言Assent的使用

    Assent 断言模板包含如下方法: assert.fail(actual, expected, message, operator) assert.ok(value, [message]) asse ...

  5. 【Java】接口开发中关于接受和发送json的相关范例

    接受json package com.suneee.scn.wms.web.rocketmq; import java.util.List; import net.sf.json.JSONArray; ...

  6. 【Java】SVN下载maven项目到eclipse之后,项目红叉,pom.xml出现Missing artifact fakepath:dubbo:jar:2.8.5等缺少jar包情况

    刚入公司,从svn上把代码弄下来之后导入eclipse,一般是maven项目,往往项目都会有红叉.如果排除代码本身问题,一般是jar包没有. 鼠标点开pom.xml文件,在约束那里一般有红叉,鼠标放上 ...

  7. 520的信心赛——点点玩deeeep

                                   3.点点玩 deeeep(deeeep.cpp) 描述 点点最近迷上了 deeeep(此 de 非彼 de),在研究一个特殊的最长树链问题 ...

  8. 【BZOJ4943】【NOI2017】蚯蚓排队(哈希)

    [BZOJ4943][NOI2017]蚯蚓排队(哈希) 题面 BZOJ 洛谷 UOJ 题解 记得去年看网络同步赛的时候是一脸懵逼的. 昨天看到\(zsy\)做了,今天就看了看.. 这不是\(Hash\ ...

  9. 解题:POI 2018 Prawnicy

    题面 网上好像都是堆的做法啊......我这个不算离散化是$O(n)$的说(虽然有一坨vector可能不开O2会爆炸) 题目即是让我们求是否存在一个最长的是不少于$k$个给出区间子集的区间,如果存在输 ...

  10. python基础----列表生成式、生成器表达式

    结论: 1.把列表解析的[]换成()得到的就是生成器表达式 2.列表解析与生成器表达式都是一种便利的编程方式,只不过生成器表达式更节省内存 3.Python不但使用迭代器协议,让for循环变得更加通用 ...