[CF1161F]Zigzag Game
通过这道模板题学了一种新的模型,记录一下。
至于这道题,显然是一个二分图博弈的模型。考虑选择Bob,我们要找一组匹配使得任何情况下Bob都有匹配边能走。不失一般性假设Alice选择了increase,起点选在左侧,那么一组匹配合法当且仅当不存在匹配\((i,j),(k,l)\)使得\(w_{i,j}<w_{j,k}<w_{k,l}\)。令左到右的权值为原边权,右到左的权值为原边权的相反数,用链接内的算法一定能找到完美匹配。
#include<bits/stdc++.h>
using namespace std;
int gi() {
int x = 0, o = 1;
char ch = getchar();
while((ch < '0' || ch > '9') && ch != '-') {
ch = getchar();
}
if(ch == '-') {
o = -1, ch = getchar();
}
while(ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0', ch = getchar();
}
return x * o;
}
int n, a[60][60], p[110], v[60], x;
vector<int> E[60];
bool cmp(int x, int y) {
return v[x] > v[y];
}
int main() {
int T = gi();
while(T--) {
n = gi();
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
a[i][j] = gi();
}
cout << "B\n", cout.flush();
if((getchar() == 'D') ^ ((x = gi()) > n))
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
a[i][j] = -a[i][j];
}
memset(p, 0, sizeof(p));
for(int i = 1; i <= n; i++) {
E[i].resize(n);
for(int j = 1; j <= n; j++) {
v[j] = a[i][j], E[i][j - 1] = j;
}
sort(E[i].begin(), E[i].end(), cmp);
}
int m = n;
while(m)
for(int i = 1, j; i <= n; i++)
if(!p[i])
while(1) {
j = E[i].back(), E[i].pop_back();
if(!p[j + n]) {
p[i] = j + n;
p[j + n] = i;
--m;
break;
} else if(a[i][j] > a[p[j + n]][j]) {
p[p[j + n]] = 0;
p[i] = j + n;
p[j + n] = i;
break;
}
}
while(1) {
cout << p[x] << '\n';
cout.flush();
x = gi();
if(x < 0) {
break;
}
}
}
return 0;
}
[CF1161F]Zigzag Game的更多相关文章
- Codeforces Round #557 题解【更完了】
Codeforces Round #557 题解 掉分快乐 CF1161A Hide and Seek Alice和Bob在玩捉♂迷♂藏,有\(n\)个格子,Bob会检查\(k\)次,第\(i\)次检 ...
- [LeetCode] Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [LeetCode] ZigZag Converesion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【leetcode】ZigZag Conversion
题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- 整数压缩编码 ZigZag
在分析Avro源码时,发现Avro为了对int.long类型数据压缩,采用Protocol Buffers的ZigZag编码(Thrift也采用了ZigZag来压缩整数). 1. 补码编码 为了便于后 ...
- No.006:ZigZag Conversion
问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
随机推荐
- 131、TensorFlow保存模型
# tf.train.Saver类提供了保存和恢复模型的方法 # tf.train.Saver的构造函数 提供了save和恢复的参数选项 # Saver对象提供了方法来运行这些计算节点,制定了写和读的 ...
- PyTestReport使用
PyTestReport详细介绍: https://testerhome.com/opensource_projects/78 示例代码 #coding:utf-8 import os,unittes ...
- 常见的网络设备:集线器 hub、网桥、交换机 switch、路由器 router、网关 gateway
Repeater 中继器 Hub 集线器 bridge 网桥 switch 交换机 router 路由器 gateway 网关 网卡 参考资料: do-you-know-the-differences ...
- UCenter 与 DIscuz 通信失败的解决方法
问题状况:Discuz 用户无法成功修改头像且帖子中上传的图片无法保存.进入 Discuz 后台检查,一切正常:进入 UCenter 检查后发现在"应用管理"中与 Discuz 论 ...
- Python-位操作 ( &,|,~,^,<<,>>
用于提高运算速度,规避算术运算符 在位操作运算中,不应该试图考虑先得到一个数的二进制码 ,而是应该将这个数看作是一个二进制码,二进制补码与整数之间是一一对应的. 诚然Python中有内置函数bin将一 ...
- (转)https://wiki.debian.org/iwlwifi debian7下wifi intel 5100agn
https://wiki.debian.org/iwlwifi Debian 7 "Wheezy" Add a "non-free" component to ...
- Windows Server2003 关闭 关机信息、开机ctrl+alt+del
取消CTRL+ALT+DEL win+R 或从"开始"打开"运行",输入gpedit.msc打开"组策略编辑器",依次展开"计算机 ...
- 创建配置中心服务端(Spring Cloud Config)
创建配置中心服务端 创建好项目后添加配置文件内容 server.port=9004 spring.application.name=spring-cloud-config-server-01 #git ...
- [Bzoj1014][JSOI2008]火星人prefix(无旋Treap&hash)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 因为涉及到增加和修改,所以后缀数组就被pass掉了,想到的就是平衡树维护hash值 ...
- CodeForces 739B Alyona and a tree (二分+树上差分)
<题目链接> 题目大意: 给定一颗带权树,树的根是1,树上每个点都有点权,并且还有边权.现在给出“控制”的定义:对一个点u,设v为其子树上的节点,且$dis(u,v)≤val[v]$,则称 ...