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 6-2简单的socket通信
什么是socket? Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面 ...
- 排查 Maxwell can not find database 并且使用 MySQL binlog 解决相关问题
目前我们在使用 Maxwell 在读线上机器的 binlog 同步我们的离线数据库. 这次错误定位上,首先线要确定问题是发生在生产者 还是队列 还是消费者.经过查看各机器上任务的运行日志,定位到了问题 ...
- Python对elasticsearch的CRUD
一.官网提供的Elasticsearch的Python接口包 1.github地址:https://github.com/elastic/elasticsearch-dsl-py 2.安装:pip i ...
- mysql高可用架构之MHA,haproxy实现读写分离详解
MySQL高可用架构之MHA 一.运维人员需要掌握的MySQL技术: 1.基本SQL语句 2.基本的管理[库表数据的管理 权限的管理] 3.容灾 保证数据不丢失. 二.工作中MySQ ...
- ASP.NET Web.config文件的配置(Configuration API)
本次我们讨论主要聚焦在以下Web.config配置文件的设置值的读取. 1.<connectionString />连接字符串的读取. 2.<appSettings />应用程 ...
- Python——POP3邮件协议
一.POP3协议用于收取邮件 二.POP3协议常用方法 user(login):想服务器发送登录名,并显示服务器的响应,表示服务器正在等待该用户的输入密码 pass_(passwd):在用户使用use ...
- iOS WKWebView全屏浏览网页返回 状态栏问题
问题: 用这个方法隐藏显示状态栏,总是带有残余 过一会才能消失掉 [[UIApplication sharedApplication]setStatusBarHidden:YES]; 可以切换状态栏的 ...
- 自己实现strchr函数与strstr函数
char* my_strchr(char* str, int i) { if (NULL == str) { return NULL; } while ('\0' != *str && ...
- .net core 2.0 配置Session
本文章为原创文章,转载请注明出处 配置Session 在Startup.cs文件的ConfigureServices方法中添加session services.AddSession(); 在Start ...
- [BZOJ 1968] [AHOI 2005] 约数研究
Description Input 只有一行一个整数 \(N\). Output 只有一行输出,为整数 \(M\),即 \(f(1)\) 到 \(f(N)\) 的累加和. Sample Input 3 ...