# Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D

204人阅读 评论(0)

solution：

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;

typedef unsigned long long UL;
typedef long long LL;
const UL p = 233;
const UL mo = 1000000007;
const int maxn = 1E5 + 10;

UL A[maxn*2],h[maxn][2];
int k,n,tot,cur = 1,a[maxn],Num[maxn][2],G[maxn*2];
LL Ans,sum[maxn*2][2],sl[maxn*2],sr[maxn*2];
char s[maxn];

vector <int> v0[maxn*2],v1[maxn*2];

UL Mul(const UL &x,const UL &y) {return x*y%mo;}
UL Add(const UL &x,const UL &y) {return (x + y) % mo;}

bool cmp(const int &x,const int &y) {return a[x] > a[y];}

int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif

cin >> k >> n;
for (int i = 1; i <= k; i++)
{
scanf("%s%d",s + 1,&a[i]);
for (int j = 1; j <= n; j++)
{
h[i][0] = Mul(h[i][0],p);
}
for (int j = n; j; j--)
{
h[i][1] = Mul(h[i][1],p);
}
A[++tot] = h[i][0]; A[++tot] = h[i][1];
}
sort(A + 1,A + tot + 1);
for (int i = 2; i <= tot; i++)
if (A[i] != A[i-1]) A[++cur] = A[i];
for (int i = 1; i <= k; i++)
{
Num[i][0] = lower_bound(A + 1,A + cur + 1,h[i][0]) - A;
Num[i][1] = lower_bound(A + 1,A + cur + 1,h[i][1]) - A;
v0[Num[i][0]].push_back(i); G[Num[i][0]] = Num[i][1];
v1[Num[i][1]].push_back(i); G[Num[i][1]] = Num[i][0];
}
for (int i = 1; i <= cur; i++)
{
sort(v0[i].begin(),v0[i].end(),cmp);
sort(v1[i].begin(),v1[i].end(),cmp);
}

for (int i = 1; i <= cur; i++)
{
if (G[i] > i) continue;
if (G[i] == i)
{
for (int j = 0; j < v0[i].size()-1; j += 2)
{
int now = a[v0[i][j]] + a[v0[i][j+1]];
if (now < 0) break; sum[i][0] += 1LL*now;
}
sum[i][1] = a[v0[i][0]];
for (int j = 1; j < v0[i].size()-1; j += 2)
{
int now = a[v0[i][j]] + a[v0[i][j+1]];
if (now < 0) break; sum[i][1] += 1LL*now;
}
}
else
{
int siz = min(v0[i].size(),v1[i].size());
for (int j = 0; j < siz; j++)
{
int now = a[v0[i][j]] + a[v1[i][j]];
if (now < 0) break; sum[i][0] += 1LL*now;
}
}
}
for (int i = 1; i <= cur; i++) sl[i] = sum[i][0] + sl[i-1];
for (int i = cur; i; i--) sr[i] = sum[i][0] + sr[i+1];
for (int i = 1; i <= cur; i++)
Ans = max(Ans,sl[i-1] + sum[i][1] + sr[i+1]);
cout << max(Ans,sl[cur]);
return 0;
}

0
0

个人资料
• 访问：200794次
• 积分：8912
• 等级：
• 排名：第2547名
• 原创：733篇
• 转载：1篇
• 译文：0篇
• 评论：16条
文章分类
阅读排行
最新评论