通过这道模板题学了一种新的模型,记录一下。

稳定婚姻匹配

至于这道题,显然是一个二分图博弈的模型。考虑选择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的更多相关文章

  1. Codeforces Round #557 题解【更完了】

    Codeforces Round #557 题解 掉分快乐 CF1161A Hide and Seek Alice和Bob在玩捉♂迷♂藏,有\(n\)个格子,Bob会检查\(k\)次,第\(i\)次检 ...

  2. [LeetCode] Zigzag Iterator 之字形迭代器

    Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...

  3. [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 ...

  4. [LeetCode] ZigZag Converesion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  5. 【leetcode】ZigZag Conversion

    题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  6. 整数压缩编码 ZigZag

    在分析Avro源码时,发现Avro为了对int.long类型数据压缩,采用Protocol Buffers的ZigZag编码(Thrift也采用了ZigZag来压缩整数). 1. 补码编码 为了便于后 ...

  7. No.006:ZigZag Conversion

    问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  8. ZigZag Conversion leetcode java

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  9. 【leetcode❤python】 6. ZigZag Conversion

    #-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object):    def convert(self, s, numRow ...

随机推荐

  1. 【CDN+】 Hive 入门 以及Handoop 系统认知

    前言 本文主要介绍Hive 的基础概念,以及Handoop的大体架构,组件依赖,对于大数据有个总体的认识 Hive 基础概念 官网:https://hive.apache.org/ The Apach ...

  2. 初次打开jenkins页面一片空白的解决办法

    安装完成jenkins后,点击[使用admin账号继续]后,页面一片空白的解决办法: step1: Jenkins插件管理-高级设置界面==> http://localhost:8080/plu ...

  3. html基础与表格的理解·

    1.静态网页与动态网页的区别:是否访问数据库 2.超文本:超文本是指超出文本的范围,可以插入声音视频,表格图片等 3.标记语言与网页结构:标记语言就是标签,网页结构包含<html>< ...

  4. 16/7/11_PHP-文件系统

    读取文件内容 PHP具有丰富的文件操作函数,最简单的读取文件的函数为file_get_contents,可以将整个文件全部读取到一个字符串中. $content = file_get_contents ...

  5. vue的概述

    一.Vue的概述 Vue的开发模式 和 之前接触的jQuery.原生JSDOM操作是不同的,之前要想修改试图,首先找元素:在使用Vue时,专心把精力放在修改数据.DOM驱动 ---> 数据驱动. ...

  6. python字符串常见操作

    字符串常见操作 如有字符串mystr = 'hello world itcast and itcastcpp',以下是常见的操作 <1>find 检测 str 是否包含在 mystr中,如 ...

  7. Linux——文件打包与压缩

    Linux 下常见常用的压缩包文件格式有*.zip,*.rar,*.7z*.gz,*.xz,*.bz2,*.tar,*.tar.gz,*.tar.xz,*tar.bz2等后缀的压缩文件 文件后缀名 说 ...

  8. python3标准库总结

    Python3标准库 操作系统接口 os模块提供了不少与操作系统相关联的函数. ? 1 2 3 4 5 6 >>> import os >>> os.getcwd( ...

  9. 项目使用Kafka镜像报错处理记录:this server does not host this topic-partition

    背景 项目使用docker swarm部署 服务之间使用消息中间件 kafka 通信 Kafka 使用 star 3.7k 的 wurstmeister/kafka:2.12-2.2.1 镜像 Zoo ...

  10. nginx各版本全自动编译安装脚本

    #!/bin/bash #作者:星云法师(头条号:西西图图---专注美食领域的研究) #环境:centos7,如果是其它的系统可以相应做调整.#--------选择安装方式,网络晚装还是本地安装--- ...