CF132E Bits of merry old England
解:略一思索:网络流啊!(别问我是怎么想到的......)
发现跟志愿者招募有点像。于是把图建一下,在下面开一条通道,但是每个点又都要经过,这时我们就无脑上下界一波。
通道向点连边,有费用。每个点向它下一次出现的点连边,费用0。每个点还向通道连边,费用0。
上下界费用流跑一下就出来费用了。然后是输出方案,看哪些边有流量,直接模拟。
#include <bits/stdc++.h> const int N = , INF = 0x3f3f3f3f; struct Edge {
int nex, v, c, len;
Edge(int Nex = , int V = , int C = , int L = ) {
nex = Nex;
v = V;
c = C;
len = L;
}
}edge[]; int tp = ; int X[N]; struct Node {
int p, v;
bool f; /// 0 print 1 change
Node(int P = , int V = , bool F = ) {
p = P;
v = V;
f = F;
}
inline void out() {
if(!f) {
printf("print("); putchar('a' + p - ); printf(")\n");
}
else {
putchar('a' + p - ); printf("=%d\n", X[v]);
}
return;
}
}stk[N]; int top; int xx, Last[N], nex[N], in[N], e[N], tag[N];
int n, m, a[N], val[N], pre[N], vis[N], flow[N], d[N];
std::queue<int> Q; inline void Max(int &a, const int &b) {
a < b ? a = b : ;
return;
} inline void add(int x, int y, int z, int w) {
// printf("add : %d %d %d %d\n", x, y, z, w);
edge[++tp] = Edge(e[x], y, z, w);
e[x] = tp;
edge[++tp] = Edge(e[y], x, , -w);
e[y] = tp;
return;
} inline bool SPFA(int s, int t) {
static int Time = ; ++Time;
memset(d, 0x3f, sizeof(d));
Q.push(s);
vis[s] = Time;
flow[s] = INF;
d[s] = ;
while(!Q.empty()) {
int x = Q.front();
Q.pop();
vis[x] = ;
// printf(" x = %d d[x] = %d \n", x, d[x]);
for(int i = e[x]; i; i = edge[i].nex) {
int y = edge[i].v;
if(d[y] > d[x] + edge[i].len && edge[i].c) {
d[y] = d[x] + edge[i].len;
// printf(" y = %d d[y] = %d \n", y, d[y]);
flow[y] = std::min(flow[x], edge[i].c);
pre[y] = i;
if(vis[y] != Time) {
vis[y] = Time;
Q.push(y);
}
}
}
}
return d[t] < INF;
} inline void update(int s, int t) {
int f = flow[t];
// printf("update : %d ", t);
while(t != s) {
int i = pre[t];
edge[i].c -= f;
edge[i ^ ].c += f;
t = edge[i ^ ].v;
// printf("%d ", t);
}
// printf("\n");
return;
} inline int solve(int s, int t, int &cost) {
int ans = ; cost = ;
// printf("solve : %d %d \n", s, t); int i = 0;
while(SPFA(s, t)) {
// printf("loop : %d flow = %d d = %d \n", ++i, flow[t], d[t]);
ans += flow[t];
cost += flow[t] * d[t];
update(s, t);
}
// printf("ans = %d cost = %d\n", ans, cost);
return ans;
} /*
7 2
1 2 2 4 2 1 2
------------- 11 4
6 3
1 2 3 1 2 3
------------- 9 4
*/ int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
int x = a[i];
while(x) {
x -= x & (-x);
val[i]++;
}
X[i] = a[i];
}
std::sort(X + , X + n + );
xx = std::unique(X + , X + n + ) - X - ;
// printf("xx = %d \n", xx);
for(int i = ; i <= n; i++) {
a[i] = std::lower_bound(X + , X + xx + , a[i]) - X;
// printf("a %d = %d \n", i, a[i]);
}
for(int i = n; i >= ; i--) {
nex[i] = Last[a[i]];
Last[a[i]] = i;
}
/// add edge int s = n * + , t = s + , ss = s + , tt = s + ;
for(int i = ; i <= n; i++) {
// add(i, i + n, [1, 1], 0);
in[i + n]++; in[i]--;
add(i + * n, i, , val[i]);
add(i + n, i + + * n, , );
add(i + * n, i + + * n, INF, );
if(nex[i]) {
add(i + n, nex[i], , );
// printf("nex %d = %d add(%d %d)\n", i, nex[i], i + n, nex[i]);
}
}
add(s, + * n, m, );
add(n + + * n, t, m, );
int Ck = tp;
add(t, s, INF, );
for(int i = ; i <= t; i++) {
if(in[i]) {
if(in[i] > ) {
add(ss, i, in[i], );
}
else {
add(i, tt, -in[i], );
}
}
} int cost1 = , cost2 = ;
solve(ss, tt, cost1);
for(int i = Ck + ; i <= tp; i++) {
edge[i].c = ;
}
solve(s, t, cost2); // get ways
for(int i = ; i <= m; i++) {
Q.push(i);
}
for(int j = ; j <= n; j++) {
int x = j + * n;
for(int i = e[x]; i; i = edge[i].nex) {
int y = edge[i].v;
if(y == j && edge[i ^ ].c) {
/// a num -> a[j]
tag[j] = Q.front();
Q.pop();
stk[++top] = Node(tag[j], a[j], );
break;
}
}
// printf("tag[j] = %d \n", tag[j]);
stk[++top] = Node(tag[j], , );
x = j + n;
for(int i = e[x]; i; i = edge[i].nex) {
int y = edge[i].v;
if(y == nex[j] && edge[i ^ ].c) {
tag[nex[j]] = tag[j];
break;
}
if(y == j + + * n && edge[i ^ ].c) {
/// return queue
Q.push(tag[j]);
break;
}
}
} printf("%d %d\n", top, cost1 + cost2);
for(int i = ; i <= top; i++) stk[i].out();
return ;
}
AC代码
CF132E Bits of merry old England的更多相关文章
- Codeforces 132E Bits of merry old England 【最小费用最大流】
题意: 让你输出长度为n的某个序列,然后给你m个变量. 每次给某个数赋值的代价是 假设赋值a=7那么代价是3,因为7的二进制位中有3个1. 要求最后总代价最小. 输出总共要进行操作的次数,和最小代价. ...
- 题解-CF802C Heidi and Library (hard)
题面 CF802C Heidi and Library (hard) 有一个大小为 \(k\) 的空书架.有 \(n\) 天和 \(n\) 种书,每天要求书架中有书 \(a_i\).每天可以多次买书, ...
- A Child's History of England.1
A Child's History of England, by Charles Dickens (狄更斯) CHAPTER I ANCIENT ENGLAND AND THE ROMANS If y ...
- A Child's History of England.26
CHAPTER 9 ENGLAND UNDER WILLIAM THE SECOND, CALLED RUFUS William the Red, in breathless haste, secur ...
- A Child's History of England.33
To strengthen his power, the King with great ceremony betrothed his eldest daughter Matilda, then a ...
- A Child's History of England.39
He had become Chancellor, when the King thought of making him Archbishop. He was clever, gay, well e ...
- [LeetCode] Number of 1 Bits 位1的个数
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- [LeetCode] Reverse Bits 翻转位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
随机推荐
- vue中的跨域问题
https://segmentfault.com/a/1190000011072725(原文) 使用vue-axios和vue-resource解决vue中调用网易云接口跨域的问题 注(api很重 ...
- docker开启加速(第三篇)
前言: docker的镜像仓库在国外,下载会很慢,启用阿里云加速. 第一步:cd /etc/docker目录下,打开daemon.json 第二步:修改daemon.json文件,添加阿里云加速: ...
- js中style,currentStyle和getComputedStyle的区别以及获取css样式操作方法
用js的style只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的. currentStyle可以弥补style的不足(可获取内联样式,内部样式和外部样式),但是只适用于IE. g ...
- SQL年月日格式化
Select CONVERT(varchar(100), GETDATE(), 23): 2006-05-16
- Junit概述
Junit -> java unit.也就是说Junit是xunit家族中的一员. unit <- unit test case,即单元测试用例. Junit = java uni ...
- Failed to bind properties under 'spring.datasource' to javax.sql.DataSource
这是我的配置文件 # 国际化配置文件(包名.基础名) spring.messages.basename=i18n.login server.tomcat.uri-encoding=UTF- sprin ...
- kubernetes资源类别介绍
类别 名称 资源对象 Pod.ReplicaSet.ReplicationController.Deployment.StatefulSet.DaemonSet.Job.CronJob.Horizon ...
- PLSQL过期:Your trial period for PL/SQL Developer is over .If you want to continue using this software ,you must purchase the retail version.
PLSQL过期:Your trial period for PL/SQL Developer is over .If you want to continue using this software ...
- Deconvolution用法
- 【C/C++】查找(一):静态查找表
{静态查找表 + 动态查找表} 所谓动态,就是,找的时候没有则添加,或者能删除 关键字:primary key:用来表示查找表中的一条记录 {主关键字 + 次关键字} 主关键字是唯一的,用来唯一的标识 ...