解:略一思索:网络流啊!(别问我是怎么想到的......)

发现跟志愿者招募有点像。于是把图建一下,在下面开一条通道,但是每个点又都要经过,这时我们就无脑上下界一波。

通道向点连边,有费用。每个点向它下一次出现的点连边,费用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的更多相关文章

  1. Codeforces 132E Bits of merry old England 【最小费用最大流】

    题意: 让你输出长度为n的某个序列,然后给你m个变量. 每次给某个数赋值的代价是 假设赋值a=7那么代价是3,因为7的二进制位中有3个1. 要求最后总代价最小. 输出总共要进行操作的次数,和最小代价. ...

  2. 题解-CF802C Heidi and Library (hard)

    题面 CF802C Heidi and Library (hard) 有一个大小为 \(k\) 的空书架.有 \(n\) 天和 \(n\) 种书,每天要求书架中有书 \(a_i\).每天可以多次买书, ...

  3. 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 ...

  4. A Child's History of England.26

    CHAPTER 9 ENGLAND UNDER WILLIAM THE SECOND, CALLED RUFUS William the Red, in breathless haste, secur ...

  5. A Child's History of England.33

    To strengthen his power, the King with great ceremony betrothed his eldest daughter Matilda, then a ...

  6. 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 ...

  7. [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 ...

  8. [LeetCode] Reverse Bits 翻转位

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  9. 【leetcode】Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...

随机推荐

  1. spring AOP源码分析(一)

    对于springAOP的源码分析,我打算分三部分来讲解:1.配置文件的解析,解析为BeanDefination和其他信息然后注册到BeanFactory中:2.为目标对象配置增强行为以及代理对象的生成 ...

  2. AngularJS 中的 factory、 service 和 provider区别,简单易懂

    转自:http://blog.csdn.net/ywl570717586/article/details/51306176 初学 AngularJS 时, 肯定会对其提供 factory . serv ...

  3. MHA高可用及读写分离

    一.MHA简介 二.工作流程 三.MHA架构图 四.MHA工具介绍 五.基于GTID的主从复制 六.部署MHA 七.配置VIP漂移 八.配置binlog-server 九.MySQL中间件Atlas

  4. prop与attr

    1.都是获取当前元素某个属性的值 2.当获取多选框的状态时,如果没有选中,此时没有checked属性,用attr获取得到undifien prop得到false. 3.html原生属性用prop获取, ...

  5. k8s容器的资源限制

    1.k8s支持内存和cpu的限制 requests:容器运行需求,最低保障limits:限制,硬限制(资源上限) CPU: 1颗逻辑CPU(1核CPU=4个逻辑CPU) 1物理核=1000个微核(mi ...

  6. python易混易乱(1)

    字典 基础操作 <1>keys my_dict = {"name":"zhangsan","age":18} res = my_ ...

  7. Appium之开发环境搭建

    1.下载Appium 去官方网站下载http://appium.io/# : 本次以appium-desktop-setup-1.8.0.exe 文件为例,使用桌面版就不再需要下载server版本: ...

  8. mysql 测试php连接问题

    <?php$servername = "shuhua.dbhost";$username = "shuhua_user";$password = &quo ...

  9. BZOJ2829信用卡凸包——凸包

    题目描述 输入 输出 样例输入 2 6.0 2.0 0.0 0.0 0.0 0.0 2.0 -2.0 1.5707963268 样例输出 21.66 提示 本样例中的2张信用卡的轮廓在上图中用实线标出 ...

  10. Codeforces Round #440 Div. 1

    A:显然应该尽量拆成4.如果是奇数,先拆一个9出来即可. #include<iostream> #include<cstdio> #include<cmath> # ...