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 ...
随机推荐
- Day 5-5 绑定方法与非绑定方法
绑定方法与非绑定方法: 在类内部定义的绑定方法,分两大类: classmehtod是给类用的,即绑定到类,类在使用时会将类本身当做参数传给类方法的第一个参数(即便是对象来调用也会将类当作第一个参数传入 ...
- 剑指offer(14)
题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 思路: 这里有个细节,我们发现,6节点的子节点在操作之后并没有发生变化,所以等会我们在交换的时候,交换的不是节点的数值,而是整个节点. 另外我们进 ...
- VS code常用快捷方式—转载
http://www.cnblogs.com/weihe-xunwu/p/6687000.html
- 数组中元素累加 reduce
例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...
- php 将数组转换网址URL参数
$array =array ( 'id' =123, 'name' = 'dopost' );echo http_build_query( $array );//得到结果id=123name=dopo ...
- 为linux主机增加file description
在benchmarked写的服务器的时候就遇到了too many file open 这个报错. 由于遇到过很多次了,所以知道应该是单机fd打满了. 首先来看看 机器最多支持多少fd cat /pro ...
- Asp.Net Core 输出 Word
In one of the ASP.NET Core projects we did in the last year, we created an OutputFormatter to provid ...
- Nginx 简单的cpu配置
配置指定CPU Nginx建议进程数和CPU数量一致,这样每个CPU都有自己独立的缓存 worker_processes 4; worker_cpu_affinity 1000 0100 0010 0 ...
- MySQL字段属性NUll的注意点
MySQL字段属性应该尽量设置为NOT NULL 除非你有一个很特别的原因去使用 NULL 值,你应该总是让你的字段保持 NOT NULL.这看起来好像有点争议,请往下看. 空值("&quo ...
- CentOS 部署.net core 2.0 项目
上传项目到服务器 安装Nginx(反向代理服务器),配置文件 https://www.cnblogs.com/xiaonangua/p/9176137.html 安装supervisor https: ...