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

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

通道向点连边,有费用。每个点向它下一次出现的点连边,费用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. 50分钟学会Laravel 50个小技巧(基于laravel5.2,仅供参考)

    转载请注明:转载自 Yuansir-web菜鸟 | LAMP学习笔记 本文链接地址: 50分钟学会Laravel 50个小技巧 原文链接:< 50 Laravel Tricks in 50 Mi ...

  2. UTF-8编码与GBK编码下的字符长度

    源码: package lsh.java.charset; import java.nio.charset.Charset; public class LengthOfUTF_8 { public s ...

  3. 谈谈对C#中反射的一些理解和认识(上)

    今天就平常用到的非常多的反射这个技术来做一个总结,当然关于反射需要讲解的东西实在是太多的内容,在一片文章中想要讲解清楚是非常难的,本篇博客也是就自己本人对这些内容学习后的一个总结,当然包括看书和自己写 ...

  4. table index & delete array item

    table index & delete array item https://www.iviewui.com/components/table#ZDYLMB 编辑 row = { " ...

  5. Navicat软件安装

    Navicat_10.1.7永久注册码 NAVH-WK6A-DMVK-DKW3

  6. qtp 自动化测试---点滴 获取属性性/修改窗体标题

    1 GetROProperty获取对应属性值 value url (这里出错了) If Window("新增").WinObject("TRzDBEdit_10" ...

  7. Delphi 工具条按钮上的下拉菜单

    制作步骤: 1.添加一个 TImageList: ImageList1, 然后载入些图标; 2.添加两个 TPopupMenu: PopupMenu1.PopupMenu2, 并分别添加些菜单项; 3 ...

  8. 魔术方法:__set、__get

    __set: 在设置对象里边不能直接设置(或没有)的属性值的时候,自动去被调用 class Track { private $track_name; public function __set($na ...

  9. 导出数据到EXL表格中

    项目使用的是SSI框架,通过struts访问到action xml文件: <action name="fabAttributedaochu" class="com. ...

  10. Opencv画图操作

    1. 画矩形 MyRect rect;rect.left = 5;rect.top = 5;rect.right = 100;rect.bottom = 100;IplImage * pColorIm ...