首页 > 代码库 > cf 478D.Santa Claus and a Palindrome

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 } 

 

cf 478D.Santa Claus and a Palindrome