【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 ...
随机推荐
- XHTML语法规范
<head> <meta charset="utf-8" /> <title>xhtml语法规范</title> </head ...
- SpringCloud——服务网关
1.背景 上篇博客<SpringCloud--Eureka服务注册和发现>中介绍了注册中心Eureka.服务提供者和服务消费者.这篇博客我们将介绍服务网关. 图(1) 未使用服务网关的做法 ...
- phpcms 思维导图
原文地址 : https://www.cnblogs.com/fuyunbiyi/archive/2012/03/12/2391253.html
- QP(Quote-Printable) 编码
QP(Quote-Printable) 方法,通常缩写为“Q”方法,其原理是把一个 8 bit 的字符用两个16进制数值表示,然后在前面加“=”.所以我们看到经过QP编码 后的文件通常是这 ...
- 第97天:CSS3渐变和过渡详解
一.渐变 渐变是CSS3当中比较丰富多彩的一个特性,通过渐变我们可以实现许多炫丽的效果,有效的减少图片的使用数量,并且具有很强的适应性和可扩展性. 可分为线性渐变.径向渐变 1. 线性渐变 (grad ...
- bzoj1007-水平可见直线
题目 在平面直角坐标系上以\(y=kx+b\)的形式给出\(n (n\le 50000)\)条直线,求从无限高的地方能看到多少条直线. 分析 举几个例子发现我们要求的直线组成一个下凸的形状.所以我们只 ...
- 【bzoj4311】向量 线段树对时间分治+STL-vector维护凸包
题目描述 你要维护一个向量集合,支持以下操作: 1.插入一个向量(x,y) 2.删除插入的第i个向量 3.查询当前集合与(x,y)点积的最大值是多少.如果当前是空集输出0 输入 第一行输入一个整数n, ...
- C++ Win系统下的调试
有的时候我们找不出错误在哪里,这时候我们需要调试一遍看看到底是哪里出了问题:我们需要分布查看程序运行情况. 这时候我们用到了调试这样一个神奇的东西. 一.基于Dev cpp环境下的调试 Dev cpp ...
- [AT2304] [agc010_c] Cleaning
题目链接 AtCoder:https://agc010.contest.atcoder.jp/tasks/agc010_c 洛谷:https://www.luogu.org/problemnew/sh ...
- 洛谷 P2261 [CQOI2007]余数求和 解题报告
P2261 [CQOI2007]余数求和 题意: 求\(G(n,k)=\sum_{i=1}^n k \ mod \ i\) 数据范围: \(1 \le n,k \le 10^9\) \(G(n,k)\ ...