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个字符串,每一个字符串有权值,求构造一个大回文串.使得权值最大. 因为字符串长度都 ...
随机推荐
- 将OB86的故障信息保存在DB86中去
出现DP站故障的时候,CPU会自动调用OB86 ,OB86 的20B 局部变量里面有丰富的故障信息,生成数据块DB86 在DB86 中生成5个双字元素的数组ARAY 在OB86中调用 "BL ...
- P1072 开学寄语
P1072 开学寄语 转跳点:
- ThinkPhp3.2.3缓存漏洞复现以及修复建议
小编作为一个php(拍黄片)的程序员,今天早上无意间看到thinkphp的缓存漏洞,小编在实际开发过程中用thinkphp3.2.3挺多的. 我们这里来复现一下漏洞 后面我会提出修复建议 首先我们下载 ...
- 空中网4k/5k月薪挑选大四实习生的线程题
空中网4k/5k月薪挑选大四实习生的线程题 两年前,我们一个大四的学员去应聘空中网的实习生职位,空中网只给他出了三道线程题,拿回家做两天后再去给经理讲解,如果前两题做好了给4k月薪,第三道题也做出来的 ...
- spring#事件发布订阅
1. 如果在应用中发生了某些事件,事件会被拦截和处理就好了,这样就有了很大的灵活性,至少代码不会紧密的耦合在一起, 代码的解耦就是业务的解耦,业务A的代码不用手动的调用业务B的代码,业务B只需要监听相 ...
- uboot源码分析1-启动第一阶段
1.不简单的头文件包含 #include <config.h>:这个文件的内容其实是包含了一个头文件:#include <configs/x210_sd.h>". # ...
- 兼容和Error
兼容 IE兼容 ie没有forEach if(!Array.prototype.forEach) { Array.prototype.forEach = function(fun){ var len ...
- ApplicationListener监听使用ContextRefreshedEvent事件类型会触发多次
@Componentpublic class TestApplicationListener implements ApplicationListener<ContextRefreshedEve ...
- POJ1088:滑雪
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 82112 Accepted: 30706 Description ...
- C++面试常见问题——01预处理与宏定义
C++面试常见问题--------01预编译和宏的使用 C++预处理器 预处理器是一些指令,它将指示编译器在实际编译之前需要完成的预处理.预处理必须要在对程序进行词法与语义分析.代码生成与优化等通常的 ...