[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 ...
随机推荐
- Python基础学习:operator模块
operator——函数的标准操作 转自:https://blog.csdn.net/zhtysw/article/details/80510113 代码资源:Lib/operator.py oper ...
- 2017/2/27-Laravel_资源控制器命令
用于处理关于图片存储的 HTTP 请求,使用 Artisan 命令 make:controller,我们可以快速创建这样的控制器 : php artisan make:controller Photo ...
- postman的下载和使用
postman的下载 官网:https://www.getpostman.com/downloads/ 创建账号或者用谷歌浏览器账号登录 登录之后,进行接口测试,这里请求百度为例,然后点击send,就 ...
- Kakuro Extension【最大流】
HDU-3338 这道题真的处理起来好复杂啊,题意就是个简单的方格填数问题,但是每个白点至少放1,那么最后的可能解是怎样的呢?我们是不是要把x轴上的和y轴上的统一起来,然后就是每个点都被对应的x和y匹 ...
- mooc-IDEA 编写高质量代码--009
十五.IntelliJ IDEA -编写高质量代码 1.重构 [1]重构变量 选中某个变量,按住 shift+F6,修改变量名,则所有该变量名均会被重构为新变量名 [2]重构方法[ctrl+F6 | ...
- C#静态变量总结
1.初始化 全局static变量的初始化在编译的时候进行,并且只初始化一次 . 函数static变量在函数中有效,第一次进入函数初始化.以后进入函数将沿用上一次的值. 2.生命期 全局static变 ...
- activity知识点
一:activity的理解 1.活动:四大应用组件之一 2.作用:提供能让用户操作并与之交互的界面 3.组件的特点: 它的类必须实现特定接口或继承特定类 需要在配置文件中配置全类名 它的对象不是通过n ...
- bjsxt学习笔记:Dubbo
一.Dubbo诞生背景(摘自Dubbo官网-入门-背景) 二.Dubbo架构图(摘自Dubbo官网-入门-架构) 三.Dubbo核心依赖(jar包):dubbo.zkclient 四.Dubbo项目搭 ...
- C# Base64编码解码 ,Md5、Rsa加密解密
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Clas ...
- go中布尔类型bool的用法
示例 // bool布尔类型的用法 package main import ( "fmt" "unsafe" ) func main() { // bool类型 ...