[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 ...
随机推荐
- 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree
1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...
- js面向对象程序设计之属性和对象
写在博客之前的话,这是我这个刚毕业的菜鸟的第一篇博客.一口吃不成一个胖子,我也希望写的第一篇东西就让读的人醍醐灌顶.我会抱着怀疑的态度来看自己写的文章,如果有写错的地方,请大家不要被误导,如果有大神提 ...
- C# 加密解密类
一. MD5 1 防止看到明文 数据库密码,加盐(原密码+固定字符串,然后再MD5/双MD5) 2 防篡改 3 急速秒传(第一次上传文件,保存md5摘要,第二次上传检查md5摘要) 4文件 ...
- Docker安装Web前端性能测试工具Sitespeed.io
一.Sitespeed.io概述 1.Sitespeed.io简介 Sitespeed.io:是一款开源的Web性能测试工具,用来衡量Web网站的综合性能,帮助开发和测试人员分析网页的加载速度和渲染性 ...
- [7期]美少妇(msf)和独角兽(unicorn)
MSF 全称:metasploit-framework 渗透大杀器,黑客工具排名第一(靠前),尤其在内网中.在校期间大学生先跟本校安全部沟通好,一起实验. 得不到校方理解与认可的上SRC或者自己 ...
- C#将字符串Split()成数组
string str="aaajbbbjccc";string[] sArray=str.Split('j');foreach(string i in sArray) Respon ...
- Linux忘记密码怎么办
重启 Linux 系统主机并出现引导界面时,按下键盘上的 e 键进入内核编辑界面 在 linux16 参数这行的最后面追加"rd.break"参数,然后按下 Ctrl + X 组合 ...
- 06 CAS的原理和AQS
CAS的原理 CAS(compareAndSwap),比较交换,是一种无锁的原子算法. Cas(value,expect,newValue),如果vaule和ecpect一样,就更新为newValue ...
- python学习第十三天元组创建和操作方法
有人问,有了列表,为什么还要有元组呢,到底元组是什么,元组是不可变的有序的列表,一旦创建不能改变,那些地方用到元组呢,小编知道可以应用到数据库连接. 1,元组的创建 n1 = () 元组用的是小括号 ...
- java 日期工具
package com.neuxa.is.workflow.utils; import java.sql.Timestamp;import java.text.DateFormat;import ja ...