CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)
题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大。
分析:
1、若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的魅力值分别从大到小排序后,若两者之和大于0,则可以放在回文串的两边。
2、若某字符串是回文串,将其魅力值从大到小排序后,两两依次分析:(mid---可能放在回文串中间的串的最大魅力值)
(1)若两个数都是正的,那么就将其放在两边,并将结果计入ans。(ans---回文串两边的串的魅力值之和)
(2)若一正一负,且正数绝对值大,可以放在两边,也可以将正数放在中间同时舍去负数。
那么先假定这两个数是放在两边的,并将结果计入ans。
若该正数放在回文串的中间最终是mid的最大值,那么实际上结果增加了是负数的绝对值,所以将负数的绝对值与mid比较,并更新。
eg:
(i)aaa:5,-3;bbb:4;
aaa放在中间相对于放在两边最终的结果是增加了3,而bbb放在中间最终的结果是增加了4,所以aaa应当放在两边,而且中间的串应取bbb。
(ii)aaa:5,-3;bbb:2;
aaa放在中间相对于放在两边最终的结果是增加了3,而bbb放在中间最终的结果是增加了2,所以aaa放在中间是最好的情况,mid应取3。
(3)若一正一负,且负数绝对值大,那么一定不能放在两边(因为两者之和小于0),但正数可以放在中间,更新mid。
(4)若两数都是负的,那么不放进回文串。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 1000000 + 10;
using namespace std;
map<string, int> mp;
vector<int> v[MAXN];
vector<string> t;
int k, n;
string s;
int cnt;
int vis[MAXN];
void init(){
cnt = 0;
for(int i = 0; i < MAXN; ++i) v[i].clear();
mp.clear();
memset(vis, 0, sizeof vis);
t.clear();
}
int get_id(string x){
if(mp.count(x)) return mp[x];
return mp[x] = ++cnt;
}
bool judge(string x){
for(int i = 0; i < n / 2; ++i)
if(x[i] != x[n - i - 1]) return false;
return true;
}
int main(){
while(scanf("%d%d", &k, &n) == 2){
init();
for(int i = 0; i < k; ++i){
cin >> s;
int id = get_id(s);
int value;
scanf("%d", &value);
v[id].push_back(value);
if(judge(s)) vis[id] = -1;//回文串
else{
t.push_back(s);//非回文串
}
}
int l = t.size();
for(int i = 0; i < l; ++i){
string tt = t[i];
reverse(t[i].begin(), t[i].end());
if(mp.count(t[i])){
vis[mp[tt]] = mp[t[i]];
}
}
int ans = 0;
int mid = 0;
for(int i = 1; i <= cnt; ++i){
if(!vis[i]) continue;
if(vis[i] != -1){//非回文串且有对称串
vis[vis[i]] = 0;
int tmp_id = vis[i];
int len = Min(v[i].size(), v[tmp_id].size());
sort(v[i].begin(), v[i].end(), greater<int>());
sort(v[tmp_id].begin(), v[tmp_id].end(), greater<int>());
for(int j = 0; j < len; ++j){
if(v[i][j] + v[tmp_id][j] > 0) ans += v[i][j] + v[tmp_id][j];
else break;
}
}
else{
int len = v[i].size();
if(len == 1){
mid = Max(mid, v[i][0]);
continue;
}
sort(v[i].begin(), v[i].end(), greater<int>());
for(int j = 0; j < len; j += 2){
if(j + 1 < len){
if(v[i][j] + v[i][j + 1] > 0){
ans += v[i][j] + v[i][j + 1];
if(v[i][j] >= 0 && v[i][j + 1] < 0){
mid = Max(mid, -v[i][j + 1]);
}
else if(v[i][j] < 0 && v[i][j + 1] >= 0){
mid = Max(mid, -v[i][j]);
}
}
else{
if(v[i][j] >= 0){
mid = Max(mid, v[i][j]);
}
else if(v[i][j + 1] >= 0){
mid = Max(mid, v[i][j + 1]);
}
else break;
}
}
else{
if(v[i][j] > 0) mid = Max(mid, v[i][j]);
}
}
}
}
printf("%d\n", ans + mid);
}
return 0;
}
CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)的更多相关文章
- Codeforces 748D Santa Claus and a Palindrome
雅礼集训期间我好像考完试就开始划水了啊 给出k个长度相同的字符串,每个串有一个权值,选出一些串连成一个回文串.使得选中的串的总权值最大. 如果选一个串,必须同时选一个对称的串.还有一个特殊情况是可以在 ...
- 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不是回文 ...
- 【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 752C - Santa Claus and Robot - [简单思维题]
题目链接:http://codeforces.com/problemset/problem/752/C time limit per test 2 seconds memory limit per t ...
- codeforces 748E Santa Claus and Tangerines
E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Codeforces 784B Santa Claus and Keyboard Check
题面: 传送门 B. Santa Claus and Keyboard Check Input file: standard input Output file: standard output Time ...
- [CF752D]Santa Claus and a Palindrome(优先队列,贪心乱搞)
题目链接:http://codeforces.com/contest/752/problem/D 题意:给长度为k的n个字符串,每一个字符串有权值,求构造一个大回文串.使得权值最大. 因为字符串长度都 ...
随机推荐
- DB2常用sql语句
转 DB2 提供了关连式资料库的查询语言sql(structured query language),是一种非常口语化.既易学又易懂的语法.此一语言几乎是每个资料库系统都必须提供的,用以表示关连式的操 ...
- Spring源码深度解析-《源码构建》
1.gradle构建eclipse项目时,gradle-5.0版本构建失败,gradle-3.3构建成功!Why 2.导入spring-framework-3.2.x/spring-beans之前先导 ...
- Django null=True和blank=True的区别
今天遇到一个问题: 在restframework框架中开发,数据库了创建了一个model的属性如下所示: remarks = models.CharField(verbose_name=u" ...
- php的排序算法
*对于算法来说,对于每个小伙伴来说都是比较头疼的,但是,为什么要学习算法? 算法是基础,算法能够提升智力,我想这两点就值得我们花时间去学习了.不要放弃,实在不会,先死记硬背下来,以后慢慢理解,一下是我 ...
- Java 的 String.split 函数,消除空字符串
代码: String str = "the music made it hard to concentrate"; String delims = "[ ]+" ...
- Oracle--sqlplus--常用命令
登陆:win+R输入sqlplus即可 如果前期没有用户可以输入sqlplus /nolog 记得sqlplus后有一个空格 --格式化命令 进行数据查询时,默认的方式排版会很乱,如果我们要解决这个 ...
- Job for nginx.service failed because the control process exited with error code. See “systemctl stat
启动nginx服务时如果遇到这个错误 Job for nginx.service failed because the control process exited with error code. ...
- NO3 cat-xargs-cp-mv-rm-find命令
·cat #查看文件内容 eg:cat oldboy.txt·xargs #从标准输入获取内容创建和执行命令 -n 加数字:分组 ·cp ...
- Xshell远程连接kali,SSH服务拒绝了密码
在kali里面/etc/ssh/目录下,修改sshd_config文件,不是ssh_config,ssh_config是针对客户端的配置文件,而sshd_config是针对服务器端的配置文件. 找到# ...
- UVA - 1451 Average (斜率优化)
题意:由01组成的长度为n的子串,AT由0表示,GC由1表示,求一段长度大于等于L且GC率最高的子串的起始终止坐标,若GC率相同,取长度较小,若长度相同,取起始坐标最小. 分析: 1.一个子串(i+1 ...