【Codeforces752D】Santa Claus and a Palindrome [STL]
Santa Claus and a Palindrome
Time Limit: 20 Sec Memory Limit: 512 MB
Description
Input
Output
一个整数表示答案。
Sample Input
abb 2
aaa -3
bba -1
zyz -4
abb 5
aaa 7
xyx 4
Sample Output
HINT
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]的更多相关文章
- 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 ...
- Santa Claus and a Palindrome
Santa Claus and a Palindrome 题目链接:http://codeforces.com/contest/752/problem/D 贪心 很自然地,可以想到,若subS不是回文 ...
- 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 ...
- 【BZOJ4099】Trapped in the Haybales Gold STL
[BZOJ4099]Trapped in the Haybales Gold Description Farmer John has received a shipment of N large ha ...
- [CF752D]Santa Claus and a Palindrome(优先队列,贪心乱搞)
题目链接:http://codeforces.com/contest/752/problem/D 题意:给长度为k的n个字符串,每一个字符串有权值,求构造一个大回文串.使得权值最大. 因为字符串长度都 ...
- 【CF】7 Beta Round D. Palindrome Degree
manacher+dp.其实理解manacher就可以解了,大水题,dp就是dp[i]=dp[i>>1]+1如何满足k-palindrome条件. /* 7D */ #include &l ...
- Codeforces 748D Santa Claus and a Palindrome
雅礼集训期间我好像考完试就开始划水了啊 给出k个长度相同的字符串,每个串有一个权值,选出一些串连成一个回文串.使得选中的串的总权值最大. 如果选一个串,必须同时选一个对称的串.还有一个特殊情况是可以在 ...
- 【leetcode】564. Find the Closest Palindrome
题目如下: 解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的.要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设n=1234,1和4对称所以把4变成1 ...
- cf 478D.Santa Claus and a Palindrome
原来set,priority_queue也可以映射..涨姿势2333 比较麻烦的应该就是判断自身回文的串是选2个还是选一个吧. #include<bits/stdc++.h> #defin ...
随机推荐
- Scrum Meeting Beta - 1
Scrum Meeting Beta - 1 NewTeam 2017/11/28 地点:主南201 任务反馈 团队成员 完成任务 计划任务 安万贺 详细讨论Beta阶段的任务和具体分工 了解缓存的相 ...
- Qt多线程-QThread
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt多线程-QThread 本文地址:http://techieliang.com/2 ...
- laravel 字段映射问题,表单中提交字段与数据表中字段不一致
在遇到提交表单时,表单中的name属性与数据表中的字段不一致,报错, 解决方法: 参考1:提交表单的时候,表单的name属性和数据表字段名称是一样的,这样有什么不妥么? 你数据库的信息给前端透露得越多 ...
- TP中模型实例化
模型的实例化操作(重点) 模型虽然已经创建完成,但是由于模型的本质是一个类,类在使用的时候需要实例化操作. 5.1.普通实例化方法 普通实例化方法是指通过自己编写代码来new一个对象. $obj = ...
- Post/Redirect/Get
参考地址:https://www.cnblogs.com/TonyYPZhang/p/5424201.html 参考资料:Flask Web开发:基于Python的Web应用开发实战 Post/Red ...
- java 常量 jvm在编译期能确定常量的 所以相等 但不能确定变量的范围 所以变量相加会产生新的
- 包装类 integer 当做 list的参数时候 会出现无法删除成功的现象
- wp开发(三)--赚取收益篇
App开发完毕了,是否有赚取收益的想法呢?下面很浅显地介绍两种常用赚取收益的方法. 一. 收费 在发布应用时,可以对应用进行定价,发布到商城之后,用户付费才可以下载,当然也可以提供试用版.收益状况可以 ...
- 题解 P5015 【标题统计】
既然这个题这么水 大家不如来盘点一下算法呗 首先说一个事:逗号表达式 这玩意的值是最后一个表达式的值 那么我们就可以愉快的放进循环条件里摩擦 话说这个应该是基础吧,大多数代码都可以这样干 具体可以后面 ...
- 3294 [SCOI2016]背单词
题目描述 Lweb 面对如山的英语单词,陷入了深深的沉思,”我怎么样才能快点学完,然后去玩三国杀呢?“.这时候睿智的凤老师从远处飘来,他送给了 Lweb 一本计划册和一大缸泡椒,他的计划册是长这样的: ...