Problem Statement

There are $N$ cards numbered $1$ through $N$.
Each face of a card has an integer written on it; card $i$ has $A_i$ on its front and $B_i$ on its back.
Initially, all cards are face up.

There are $M$ machines numbered $1$ through $M$.
Machine $j$ has two (not necessarily distinct) integers $X_j$ and $Y_j$ between $1$ and $N$. If you power up machine $j$,
it flips card $X_j$ with the probability of $\frac{1}{2}$, and flips card $Y_j$ with the remaining probability of $\frac{1}{2}$.
This probability is independent for each power-up.

Snuke will perform the following procedure.

  1. Choose a set $S$ consisting of integers from $1$ through $M$.
  2. For each element in $S$ in ascending order, power up the machine with that number.

Among Snuke's possible choices of $S$, find the maximum expected value of the sum of the integers written on the face-up sides of the cards after the procedure.

Constraints

  • $1\leq N \leq 40$
  • $1\leq M \leq 10^5$
  • $1\leq A_i,B_i \leq 10^4$
  • $1\leq X_j,Y_j \leq N$
  • All input values are integers.

\(N\le 40\),结合题目,复杂度应该是 \(2^{\frac n2}\) 相关的。

考虑如何求出期望。如果某一个数可能被选的话,那么他的期望就是 \(\frac {A_i+B_i}2\),否则是 \(A_i\)

看样例发现要特判 \(X_i=Y_i\),如果 \(A_{X_i}<B_{X_i}\),那么交换 \(A_{X_i}\) 和 \(B_{X_i}\)

如果 \(A_i<B_i\),那么他被选后期望会减少,否则期望会增多。设有 \(c\) 个 \(i\) 会变少。

如果 \(X_i\) 和 \(Y_i\) 翻转后期望都会变少,肯定不翻。期望都会变多,肯定,肯定翻。现在就是要看 \(X_i\) 和 \(Y_i\) 期望一个会变多,一个会变少的要不要翻。

肯定要对 \(c\) 大小分治。如果 \(c\le 20\),那么选了集合 \(s\) 中的所有会减少数,肯定会选所有可以选的会变多的数,直接枚举 \(s\) 统计即可。

如果 \(c\ge 20\),那么期望会增多的 \(i\) 不超过 20 个,用一个 dp 求出要得到集合 \(s\) 至少选多少减少的。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int M=45,N=1e5+5,S=1e7+5;;
int n,m,a[M],b[M],x[N],y[N],c,d,p[M],q[M],v[M][M];
LL to[S],s,f[M],ans;
LL calc(LL x,LL s)
{
int ret=0;
for(int i=0;i<d;i++)
{
if(x>>i&1)
ret+=a[q[i]]+b[q[i]];
else
ret+=2*a[q[i]];
}
for(int i=0;i<c;i++)
{
if(s>>i&1)
ret+=a[p[i]]+b[p[i]];
else
ret+=2*a[p[i]];
}
return ret;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d%d",a+i,b+i);
for(int i=0;i<m;i++)
{
scanf("%d%d",x+i,y+i);
--x[i],--y[i];
if(x[i]==y[i])
{
if(a[x[i]]<b[x[i]])
swap(a[x[i]],b[x[i]]);
--i,--m;
}
else
v[x[i]][y[i]]=v[y[i]][x[i]]=1;
}
for(int i=0;i<n;i++)
a[i]<b[i]? p[c++]=i:q[d++]=i;
LL s=0;
for(int i=0;i<c;i++)
for(int j=i+1;j<c;j++)
if(v[p[i]][p[j]])
s|=1LL<<j|1LL<<i;
for(int i=0;i<d;i++)
{
f[i]=s;
for(int j=0;j<c;j++)
if(v[q[i]][p[j]])
f[i]|=1LL<<j;
}
if(d<=c)
{
for(int i=0;i<d;i++)
to[1<<i]=f[i];
ans=calc(0,s);
for(int i=1;i<(1<<d);i++)
{
to[i]=to[i^(i&-i)]|to[i&-i];
ans=max(ans,calc(i,to[i]));
}
}
else
{
memset(to,-0x7f,sizeof(to));
for(int i=0;i<(1<<c);i++)
if((i|s)==s)
to[i]=0;
ans=calc(0,s);
for(int i=1;i<(1<<c);i++)
{
if(!to[i])
continue;
for(int j=0;j<d;j++)
to[i]=max(to[i],to[i^(i&f[j])]+b[q[j]]-a[q[j]]);
ans=max(ans,calc(0,i)+to[i]);
}
}
printf("%.6lf",ans/2.000);
return 0;
}

[ABC313F] Flip Machines的更多相关文章

  1. Teaching Machines to Understand Us 让机器理解我们 之三 自然语言学习及深度学习的信仰

    Language learning 自然语言学习 Facebook’s New York office is a three-minute stroll up Broadway from LeCun’ ...

  2. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  3. [LeetCode] Flip Game 翻转游戏之二

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  4. [LeetCode] Flip Game 翻转游戏

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  5. poj Flip Game 1753 (枚举)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27005   Accepted: 11694 Descr ...

  6. ARM概论(Advanced RISC Machines)

    简介 ARM7是32 位通用微处理器ARM(Advanced RISC Machines)家族中的一员,具有比较低的电源消耗和良好的性价比, 基于(精简指令)RISC结构,指令集和相关的译码机制与微程 ...

  7. POJ1753 Flip Game(bfs、枚举)

    链接:http://poj.org/problem?id=1753 Flip Game Description Flip game is played on a rectangular 4x4 fie ...

  8. Deep Learning 18:DBM的学习及练习_读论文“Deep Boltzmann Machines”的笔记

    前言 论文“Deep Boltzmann Machines”是Geoffrey Hinton和他的大牛学生Ruslan Salakhutdinov在论文“Reducing the Dimensiona ...

  9. poj1753 Flip Game

    题意:4*4的正方形,每个格子有黑白两面,翻转格子使得4*4个格子显示全黑或全白,翻转要求:选中的那个格子,以及其上下左右相邻的格子(如果存在)要同时翻转.输出最小的达到要求的翻转次数或者Imposs ...

  10. java.nio.ByteBuffer中flip,rewind,clear方法的区别

    对缓冲区的读写操作首先要知道缓冲区的下限.上限和当前位置.下面这些变量的值对Buffer类中的某些操作有着至关重要的作用: limit:所有对Buffer读写操作都会以limit变量的值作为上限. p ...

随机推荐

  1. MyBatis Mapper映射处理CLOB和BLOB类型

    ​Mybatis的MapperXML映射文件应该处理数据库字段类型为CLOB和BLOB类型的数据呢?首先我们先看下CLOB和BLOB这两种数据类型的介绍. 介绍 使用Mybatis时涉及到两种特殊类型 ...

  2. 角度新奇!第一次看到这样使用MyBatis的,看得我一愣一愣的。

    你好呀,我是歪歪. 这期给大家分享一个读者给我分享的一个关于 MyBatis 的"编程小技巧",说真的,这骚操作,直接把我看得一愣一愣的. 我更情愿叫它:坑你没商量之埋雷大法. D ...

  3. CodeForces 1311E Construct the Binary Tree

    题意 给定\(n\)和\(d\),构造一颗\(n\)个节点的二叉树(以\(1\)为根),所有节点到\(1\)的距离和为\(d\),不行输出\(NO\),否则输出\(YES\)和\(2\)-\(n\)的 ...

  4. 我们能从PEP 703中学到什么

    PEP703是未来去除GIL的计划,当然现在提案还在继续修改,但大致方向确定了. 对于实现细节我没啥兴趣多说,挑几个我比较在意的点讲讲. 尽量少依赖原子操作的引用计数 没了GIL之后会出现两个以上的线 ...

  5. .NET6.0实现IOC容器

    .NET6.0实现IOC容器 IOC的作用这里省略-只对如何使用进行说明. 1. 创建一个.NET6应用程序 这里使用 .NET6.0 WebAPI 应用 2. 声明接口 public interfa ...

  6. Vue3中的几个坑,你都见过吗?

    Vue3 目前已经趋于稳定,不少代码库都已经开始使用它,很多项目未来也必然要迁移至Vue3.本文记录我在使用Vue3时遇到的一些问题,希望能为其他开发者提供帮助. 1. 使用reactive封装基础数 ...

  7. 「coci 2021-2022 #1」Logičari

    link. 断环后把断的边所连的两个点特殊标记,作为两个特殊点.这样就是一个树,树的做法很简单吧,把两个特殊点特殊处理带进状态即可. 具体一点就是,设 \(f(x,c_x,c_f,c_{rt_1},c ...

  8. Solution -「HDU 3507」Print Article

    Description Link. 给出 \(N\) 个单词,每个单词有个非负权值 \(C_{i}\),现要将它们分成连续的若干段,每段的代价为此段单词的权值和,还要加一个常数 \(M\),即 \(( ...

  9. Django框架项目之git笔记——版本控制器、git介绍、git使用

    文章目录 版本控制器 git 简介 git与svn比较 git的工作流程 版本库间的通信 git分支管理 git使用 流程(核心总结) 安装 基础命令 将已有的文件夹 - 初始化为git仓库 在指定目 ...

  10. Android项目Library导入的问题整理

    Android项目Library导入的问题整理 本来帮助朋友找寻一下android的一些特效的demo,结果找到了一个,朋友试验可以,自己却是在导入项目需要的library的时候总是出问题,真的很是丢 ...