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个字符串,每一个字符串有权值,求构造一个大回文串.使得权值最大. 因为字符串长度都 ...
随机推荐
- 编程练习:实现树的层次遍历 (CVTE笔试)
直接层次遍历是比较简单的,但是题目要求的分层打印,这就变得稍微有些麻烦 我是采用两个队列的方法实现. 1.将树结构入队列1. 2.当队列1和队列2都不为空的时候,则一直循环. 3.当队列1不为空的时候 ...
- 侯捷C++学习(一)
//c++学习//标准库非常重要//要规范自己的代码complex c1(2,1);complex c2;complex* pc = new complex(0,1);string s1(" ...
- centos 7安装nodejs
ps: {install_path} 安装目录路径 1.安装wget yum install wget 2. 下载对应文件 wget -c https://nodejs.org/dist/v8.9.1 ...
- 页面的html调试
点击页面按下键盘的F12,或者鼠标右键选择检查(N) 会弹出一个窗口,这个窗口就是调试窗口 如上图所示,第一个图标是标签元素选择器,点击使用后,在页面上移动,会在Elements的区域找到你鼠标选中的 ...
- Day8 - E - The very same Munchhausen CodeForces - 1120E
A positive integer aa is given. Baron Munchausen claims that he knows such a positive integer nn tha ...
- Day7 - G - Divisors POJ - 2992
Your task in this problem is to determine the number of divisors of Cnk. Just for fun -- or do you n ...
- 《高性能MySQL》之EXPLAIN
使用explain关键字获取sql执行性能 语法如下: explain select * from table explain 中的列expain出来的信息有10列,分别是id,select_type ...
- 008、MySQL日期时间格式化输出
#日期格式化 SELECT date_format( '2008/08/08 22:23:01', '%Y-%m-%d-%H--%i--%s' ); 不忘初心,如果您认为这篇文章有价值,认同作者的付出 ...
- 012.Delphi插件之QPlugins,多实例内嵌窗口服务
这个DEMO中主要是在DLL中建立了一个IDockableControl类,并在DLL的子类中写了具体的实现方法. 在主程序exe中,找到这个服务,然后调用DLL的内嵌方法,把DLL插件窗口内嵌到主程 ...
- NPOI读取excel 空行
if (sheet.GetRow(i) != null) 每行判断一下,避免出错.真是蛋疼.