题 

  OvO http://codeforces.com/contest/884/problem/F

  (Educational Codeforces Round 31 - F)

  884f

 

  题目标签上的 flows 极大降低了难度……

  做法:

  首先贪心,每个对应的元素对,固定b值比较大的那个元素设为不交换的元素。

  然后费用流,2n+2个点,设源点为0,汇点为2n+1

  源点向 1 ~ n 的点连一条费用为0,流量为1的边

  从 n+1 ~ 2n 向汇点连一条费用为0,流量为1的边

  对于每个固定元素,如果他是字符串中第i个元素,则i点向n+i点连一条费用0,流量1的边

  对于每个非固定元素对(i,j)(i可以等于j),如果i可以放到到j的位置,则i向j连一条流量为1的边,

  如果i=j,则费用为0;否则费用为b[j]

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <string>
#include <queue> using namespace std; const int MAXN = 300;
const int MAXM = 300000;
const int INF = 0x3f3f3f3f; struct Edge
{
int to,next,cap,flow,cost;
}edge[MAXM]; int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1 void init(int n)
{
N = n;
tol = 0;
memset(head,-1,sizeof(head));
} void addedge(int u,int v,int cap,int cost)
{
edge[tol].to = v;
edge[tol].cap = cap;
edge[tol].cost = cost;
edge[tol].flow = 0;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = 0;
edge[tol].cost = -cost;
edge[tol].flow = 0;
edge[tol].next = head[v];
head[v] = tol++;
} bool spfa(int s,int t)
{
queue<int>q;
for(int i = 0;i < N;i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -1;
}
dis[s] = 0;
vis[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -1;i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap > edge[i].flow &&
dis[v] > dis[u] + edge[i].cost )
{
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t] == -1)return false;
else return true;
} int minCostMaxflow(int s,int t,int &cost)
{
int flow = 0;
cost = 0;
while(spfa(s,t))
{
int Min = INF;
for(int i = pre[t];i != -1;i = pre[edge[i^1].to])
{
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for(int i = pre[t];i != -1;i = pre[edge[i^1].to])
{
edge[i].flow += Min;
edge[i^1].flow -= Min;
cost += edge[i].cost * Min;
}
flow += Min;
}
return flow;
} int n,val[MAXN],fix[MAXN],sum;
char str[MAXN];
int ans; int main()
{
int i,j,s,t,tmp;
scanf("%d",&n);
init(2*n+2);
s=0; t=2*n+1;
scanf("%s",str+1);
sum=0;
for(i=1;i<=n;i++)
{
scanf("%d",&val[i]);
sum+=val[i];
}
memset(fix,0,sizeof(fix));
for(i=1;i<=n/2;i++)
if(val[i]>val[n+1-i])
fix[i]=1,addedge(i,n+i,1,0);
else
fix[n+1-i]=1,addedge((n+1-i),n+(n+1-i),1,0);
for(i=1;i<=n;i++)
addedge(s,i,1,0),addedge(n+i,t,1,0);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(fix[i] || fix[j])
continue;
if(str[i]==str[n+1-j])
continue;
if(i==j) tmp=0;
else tmp=val[j];
addedge(i,n+j,1,tmp);
}
int flow=minCostMaxflow(s,t,ans);
ans=sum-ans;
printf("%d\n",ans);
return 0;
}

  

Codeforces 884f F. Anti-Palindromize的更多相关文章

  1. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  2. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  3. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

  4. Codeforces 797 F Mice and Holes

    http://codeforces.com/problemset/problem/797/F F. Mice and Holes time limit per test             1.5 ...

  5. Codeforces 622 F. The Sum of the k-th Powers

    \(>Codeforces \space 622\ F. The\ Sum\ of\ the\ k-th\ Powers<\) 题目大意 : 给出 \(n, k\),求 \(\sum_{i ...

  6. Codeforces 379 F. New Year Tree

    \(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...

  7. Codeforces 538 F. A Heap of Heaps

    \(>Codeforces \space 538 F. A Heap of Heaps<\) 题目大意 :给出 \(n\) 个点,编号为 \(1 - n\) ,每个点有点权,将这些点构建成 ...

  8. codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

    /** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...

  9. [codeforces 618 F] Double Knapsack (抽屉原理)

    题目链接:http://codeforces.com/contest/618/problem/F 题目: 题目大意: 有两个大小为 N 的可重集 A, B, 每个元素都在 1 到 N 之间. 分别找出 ...

随机推荐

  1. VC/MFC 进程间通信方法总结

    摘   要   随着人们对应用程序的要求越来越高,单进程应用在许多场合已不能满足人们的要求.编写多进程 / 多线程程序成为现代程序设计的一个重要特点,在多进程程序设计中,进程间的通信是不可避免的. M ...

  2. Thinkphp+Ajax带关键词搜索列表无刷新分页实例

    Thinkphp+Ajax带关键词搜索列表无刷新分页实例,两个查询条件,分页和搜索关键字,懂的朋友还可以添加其他分页参数. 搜索#keyword和加载内容区域#ajax_lists <input ...

  3. Qt 中使用Java代码获取安卓设备的MAC地址(安卓9.0)

    public String GetDeviceMAC() { String strMacAddr = null; try { // 获得IpD地址 InetAddress ip = getLocalI ...

  4. C# 文件过滤器

    首先说明一个示例,分析一下Filter属性的构成:“ Excel文件|*.xls ”,前面的“Excel文件”成为标签,是一个可读的字符串,可以自定定义,“|*.xls”是筛选器,表示筛选文件夹中后缀 ...

  5. std::list保存大量数据时,类型即是无析构函数,该list析构时会占用大量CPU

    std::list保存大量数据时,类型即是无析构函数,该list析构时会占用大量CPU

  6. java实现spark常用算子之TakeSample

    import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaRDD;import org.apache.spark.a ...

  7. JQ报错:Uncaught SyntaxError: Illegal continue statement: no surrounding iteration statement报错

    今天在写轮播图中,在停止定时器之后想要重新开启定时器,但是不知道为什么脑子抽了竟然想通过continue跳出定时器的本次运行继续下一次运行(当然是不可取的,但是还是试了试2333),然后就报错了.Un ...

  8. 【Git的基本操作五】比较文件差异

    比较文件差异 1. git diff [文件名] 将工作区中的文件和暂存区对应文件进行比较 例:git diff test.txt 2. git diff [本地库中文件历史记录(指针)] [文件名] ...

  9. 输入列号得到excel对应的字母列

    zexcel_cell_column 类型是INT4 FUNCTION ZGET_EXCEL_COL. *"----------------------------------------- ...

  10. Java面向对象(三) 【面向对象深入:抽象类,接口,内部类等】

    面向对象(Object Oriented) 1.抽象类抽象就是将拥有共同方法和属性的对象提取出来.提取后,重新设计一个更加通用.更加大众化的类,就叫抽象类.1)abstract 关键字修饰类.方法,即 ...