CodeForces - 884F :Anti-Palindromize(贪心&费用流)
A string a of length m is called antipalindromic iff m is even, and for each i (1 ≤ i ≤ m) ai ≠ am - i + 1.
Ivan has a string s consisting of n lowercase Latin letters; n is even. He wants to form some string t that will be an antipalindromic permutation of s. Also Ivan has denoted the beauty of index i as bi, and the beauty of t as the sum of bi among all indices i such that si = ti.
Help Ivan to determine maximum possible beauty of t he can get.
Input
The first line contains one integer n (2 ≤ n ≤ 100, n is even) — the number of characters in s.
The second line contains the string s itself. It consists of only lowercase Latin letters, and it is guaranteed that its letters can be reordered to form an antipalindromic string.
The third line contains n integer numbers b1, b2, ..., bn (1 ≤ bi ≤ 100), where bi is the beauty of index i.
Output
Print one number — the maximum possible beauty of t.
Examples
8
abacabac
1 1 1 1 1 1 1 1
8
8
abaccaba
1 2 3 4 5 6 7 8
26
8
abacabca
1 2 3 4 4 3 2 1
17
题意:给定长长度为N的字符串,现在求一个重排列,使得对称位置不相同。如果重排后i位置的字母和原来相同,就得到对应位置的得分,求最大得分。
保证N是偶数,保证有满足条件的重排。
思路:最大费用最大流。
建图:
S-->26个字母:(字母个数,0);
字母-->N/2个位置:(1,val);val尽可能大就行,三种情况讨论。
N/2个位置-->T:(2,0)
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=<<;int To[maxn],Laxt[maxn],Next[maxn],cap[maxn],cost[maxn];
int S,T,cnt=,dis[maxn],ans;
bool inq[maxn],vis[maxn];
deque<int>q;
void add(int u,int v,int c,int cc)
{
Next[++cnt]=Laxt[u];Laxt[u]=cnt; To[cnt]=v;cap[cnt]=c;cost[cnt]=-cc;
Next[++cnt]=Laxt[v];Laxt[v]=cnt; To[cnt]=u;cap[cnt]=;cost[cnt]=cc;
}
bool spfa()
{
for(int i=;i<=T;i++) inq[i]=;
for(int i=;i<=T;i++) dis[i]=inf;
inq[T]=; dis[T]=; q.push_back(T);
while(!q.empty())
{
int u=q.front(); q.pop_front();
inq[u]=;
for(int i=Laxt[u];i;i=Next[i])
{
int v=To[i];
if(cap[i^]&&dis[v]>dis[u]-cost[i])
{
dis[v]=dis[u]-cost[i];
if(!inq[u]){
inq[v]=;
if(q.empty()||dis[v]>dis[q.front()]) q.push_back(v);
else q.push_front(v);
}
}
}
}
return dis[S]<inf;
}
int dfs(int u,int flow)
{
vis[u]=;
if(u==T||flow==) return flow;
int tmp,delta=;
for(int i=Laxt[u];i;i=Next[i])
{
int v=To[i];
if((!vis[v])&&cap[i]&&dis[v]==dis[u]-cost[i])
{
tmp=dfs(v,min(cap[i],flow-delta));
delta+=tmp; cap[i]-=tmp; cap[i^]+=tmp;
}
}
return delta;
}
char c[maxn]; int v[maxn],num[maxn];
int main()
{
int N,i,j;
scanf("%d",&N); scanf("%s",c+);
for(i=;i<=N;i++) scanf("%d",&v[i]),num[c[i]-'a'+]++;
S=; T=+N/+;
for(i=;i<=;i++) add(S,i,num[i],);
for(i=;i<=;i++)
for(j=;j<=N/;j++){
if(c[j]-'a'+==i&&c[N+-j]-'a'+==i) add(i,j+,,max(v[j],v[N+-j]));
else if(c[j]-'a'+==i) add(i,j+,,v[j]);
else if(c[N+-j]-'a'+==i) add(i,j+,,v[N+-j]);
else add(i,j+,,);
}
for(i=;i<=N/;i++) add(i+,T,,);
while(spfa()){
vis[T]=;
while(vis[T]){
for(i=;i<=T;i++) vis[i]=;
ans-=dis[S]*dfs(S,N);
}
}
printf("%d\n",ans);
return ;
}
CodeForces - 884F :Anti-Palindromize(贪心&费用流)的更多相关文章
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- UVALive 4863 Balloons 贪心/费用流
There will be several test cases in the input. Each test case will begin with a line with three inte ...
- luogu P5470 [NOI2019]序列 dp 贪心 费用流 模拟费用流
LINK:序列 考虑前20分 容易想到爆搜. 考虑dp 容易设\(f_{i,j,k,l}\)表示前i个位置 选了j对 且此时A选择了k个 B选择了l个的最大值.期望得分28. code //#incl ...
- Codeforces 362E Petya and Pipes 费用流建图
题意: 给一个网络中某些边增加容量,增加的总和最大为K,使得最大流最大. 费用流:在某条边增加单位流量的费用. 那么就可以2个点之间建2条边,第一条给定边(u,v,x,0)这条边费用为0 同时另一条边 ...
- 【bzoj2424】[HAOI2010]订货 费用流
原文地址:http://www.cnblogs.com/GXZlegend/p/6825296.html 题目描述 某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di, ...
- Codeforces 280D k-Maximum Subsequence Sum [模拟费用流,线段树]
洛谷 Codeforces bzoj1,bzoj2 这可真是一道n倍经验题呢-- 思路 我首先想到了DP,然后矩阵,然后线段树,然后T飞-- 搜了题解之后发现是模拟费用流. 直接维护选k个子段时的最优 ...
- CodeForces 164C Machine Programming 费用流
Machine Programming 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co One remark ...
- Codeforces Gym 100002 E "Evacuation Plan" 费用流
"Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- [SDOI2016]数字配对(费用流+贪心+trick)
重点是如何找到可以配对的\(a[i]\)和\(a[j]\). 把\(a[i]\)分解质因数.设\(a[i]\)分解出的质因数的数量为\(cnt[i]\). 设\(a[i]\geq a[j]\) 那么\ ...
随机推荐
- VueJS构造器:new Vue({})
构造器 每个 Vue.js 应用都是通过构造函数 Vue 创建一个 Vue 的根实例来启动的: var vm = new Vue({ // 选项 }) 属性与方法 每个 Vue 实例都会代理其 dat ...
- ARM内核和架构
转:深入浅谈,CPU设计原理 CPU的内部架构和工作原理 推荐一本书:编码的奥秘 一.ARM内核和架构 ARM产品越来越丰富,命名也越来越多.很多朋友提问: ARM内核和架构都是什么 ...
- 使用Nginx的proxy_cache缓存功能取代Squid
Nginx从0.7.48版本开始,支持了类似Squid的缓存功能.这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302 ...
- 网络通信数据处理 Xbytestring类
PS_Xbytestring a byte string for store low level data type 文件夹[TOC] PS_Xbytestring 文件夹TOC base info ...
- 封装CLLocationManager定位获取经纬度
创建调用方法,在.h文件里 #import <Foundation/Foundation.h> @interface RMMapLocation : NSObject { void (^s ...
- inflate, findViewById与setContentView的差别与联系
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...
- cesium学习--初识
一.Cesium 官方介绍:CesiumJS是一个开源的JavaScript库,用于世界级的3D地球仪和地图.任务是为静态和时间动态的内容创建领先的3D地球和地图,具有最好的性能.精度.视觉质量.平台 ...
- ASP.NET动态网站制作(7)-- JS(2)
前言:这节课是JS的第二节课,主要是JS中的控制语句. 内容: 1.条件语句: (1)比较操作符:==,!=,>,>=,<,<=.字符串大小写转换:toUpperCase() ...
- String知识点
- Django项目高频使用文件
数据库配置: MySQL数据库 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'HOST': 'localhost' ...