cf 478D.Santa Claus and a Palindrome

原来set,priority_queue也可以映射。。涨姿势2333

比较麻烦的应该就是判断自身回文的串是选2个还是选一个吧。

 1 #include<bits/stdc++.h>
 2 #define INF 0x7fffffff
 3 #define LL long long
 4 #define N 100005
 5 #define pill pair<int ,int >
 6 using namespace std;
 7 inline int ra()
 8 {
 9     int x=0,f=1; char ch=getchar();
10     while (ch<‘0‘ || ch>‘9‘) {if (ch==‘-‘) f=-1; ch=getchar();}
11     while (ch>=‘0‘ && ch<=‘9‘) {x=x*10+ch-‘0‘; ch=getchar();}
12     return x*f;
13 }
14 struct Node{
15     int val;
16     string s;
17 }node[N];
18 map<string, multiset<int > > dic;
19 LL ans;
20 int n,k,m;
21 bool cmp(Node a, Node b)
22 {
23     return a.val>b.val;
24 }
25 int main()
26 {
27     n=ra(); m=ra();
28     for (int i=0; i<n; i++)
29     {
30         cin>>node[i].s>>node[i].val;
31         dic[node[i].s].insert(node[i].val);
32     }
33     int k=0,x=0,tmp=0;
34     for (int i=0; i<n; i++)
35     {
36         string s1=node[i].s;
37         if (dic[s1].size()==0) continue;
38         int val=(*dic[s1].rbegin());
39         if (val<0) continue;
40         reverse(s1.begin(),s1.end());
41         dic[node[i].s].erase(dic[node[i].s].find(val));
42         if (dic[s1].size()!=0)
43         {
44             tmp=(*dic[s1].rbegin());
45             dic[s1].erase(dic[s1].find(tmp));
46             if (val+tmp>=0)
47             {
48                 ans+=val+tmp;
49                 if (s1==node[i].s)
50                     x=max(-tmp,x);
51             }
52             else if (s1==node[i].s)
53                 x=max(x,val);
54         }
55         else if (s1==node[i].s) x=max(val,x);
56     }
57     cout<<ans+x;
58     return 0;
59 } 
时间: 2024-08-08 13:58:07

cf 478D.Santa Claus and a Palindrome的相关文章

【Codeforces748D】Santa Claus and a Palindrome [STL]

Santa Claus and a Palindrome Time Limit: 20 Sec  Memory Limit: 512 MB Description 有k个串,串长都是n,每个串有一个ai的贡献. 选出若干个串,若它们可以通过任意组合,形成一个回文串,则可以获得它们的贡献之和. 求最大贡献. Input 第一行两个整数k,n. 之后k行,每行分别是一个串si,与贡献ai. Output 一个整数表示答案. Sample Input 7 3 abb 2 aaa -3 bba -1 z

CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)

题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的魅力值分别从大到小排序后,若两者之和大于0,则可以放在回文串的两边. 2.若某字符串是回文串,将其魅力值从大到小排序后,两两依次分析:(mid---可能放在回文串中间的串的最大魅力值) (1)若两个数都是正的,那么就将其放在两边,并将结果计入ans.(ans---回文串两边的串的魅力值之和) (2)

[2015-12-24]OMG美语每日笔记-Santa Claus

坚持学习英语,OMG口语非常长不错,坚持每天整理.学英语坚持最重要,学英语坚持最重要,学英语坚持最重要说三遍! Santa Claus is coming to town 圣诞老人进城来! There are so many Christmas songs about Santa .Santa Claus is coming to town.Is a classic example. 关于圣诞老人的歌很多,圣诞老人进城来,是一个比较经典的歌. Santa Baby 圣诞宝贝 I love lis

291 - The House Of Santa Claus

来源:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=4&problem=227&mosmsg=Submission+received+with+ID+14026069 The House Of Santa Claus In your childhood you most likely had to solve the ri

UVA 291 The House Of Santa Claus DFS

题目: In your childhood you most likely had to solve the riddle of the house of Santa Claus. Do you remember that the importance was on drawing the house in a stretch without lifting the pencil and not drawing a line twice? As a reminder it has to look

CodeForces - 748B Santa Claus and Keyboard Check

题意:给定两个字符串a和b,问有多少种不同的字母组合对,使得将这些字母对替换字符串b后,可以变成字符串a.注意字母对彼此各不相同. 分析:vis[u]记录与u可形成关系的字母,若u与v不同,则形成字母对.若之后该关系被打破,则输出-1. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib>

UVa 291 The House Of Santa Claus 回溯dfs

题意:从左下方的1开始,一笔画出圣诞老人的房子. 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 int edge[6][6]; 5 6 //已画了k条边 准备将端点x加入进来 7 void dfs(int x,int k,string s){ 8 s+=char(x+'0'); 9 if(k==8){ 10 cout<<s<<endl; 11 return; 12 }

Educational Codeforces Round 79 (Rated for Div. 2) D. Santa&#39;s Bot

链接: https://codeforces.com/contest/1279/problem/D 题意: Santa Claus has received letters from n different kids throughout this year. Of course, each kid wants to get some presents from Santa: in particular, the i-th kid asked Santa to give them one of

第5课 Qt Creator工程介绍

1. QT Creator工程管理(一个工程包含不同类型的文件) (1).pro项目文件 (2).pro.user用户配置描述文件 (3).h头文件 (4).cpp源文件 (5).ui界面描述文件 (6)资源文件(图片.音频等) 2. .pro项目描述文件 2.1 .pro项目描述文件的基本构成 (1)#:注释起始符 (2)QT:模块声明 (3)TARGET:可执行文件名 (4)TEMPLATE:程序模板声明 (5)SOURCES:源码文件声明 (6)HEADERS:头文件声明 (7)FORMS