UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)
题意:
给出每个男的心目中的女神排序,给出每个女的心目中的男神排序,即两个n*n的矩阵,一旦任意两个非舞伴的男女同学觉得对方都比现任舞伴要好,他们就会抛弃舞伴而在一起。为了杜绝这种现象,求每个男的最后所搭配的女伴是谁。
思路:
怎么感觉题意有点问题,输出的是第i行的男人所搭配的女人,即输出的是女人!
每个男人依次选择最喜欢的女人求婚,若同一个女的没人抢,则临时抢亲成功,若需要抢,明显女的都是喜欢挑长腿oba啦,矮冬瓜自动加入单身狗队列继续求婚!当单身狗所求的女神已订婚,只要女神更爱单身狗,没有什么可以阻挡他们,女神果断抛弃未婚夫,未婚夫加入单身狗队列。直至地球上没有单身狗为止!
复杂度O(n2),每个男的最多只会向n个女的都求婚一次,而女的没有选择权,只有接受和拒绝。著名的Gale-Shapley算法。
#include <bits/stdc++.h>
#define max(X,Y) ((X) > (Y) ? (X) : (Y))
#define min(X,Y) ((X) < (Y) ? (X) : (Y))
#define pii pair<int,int>
#define INF 0x7f7f7f7f
#define LL long long
using namespace std;
const int N=; deque<int> que[N]; //心目中的女神排名队列
int girl[N], boy[N];
int imp[N][N]; int cal(int n)
{
memset(girl, , sizeof(girl) );
deque<int> single;
for(int i=; i<=n; i++) single.push_back(i); while(!single.empty())
{
int x=single.front(); single.pop_front(); //取出单身狗
int op=que[x].front(); que[x].pop_front(); //取出x的暗恋对象 if(!girl[op]) girl[op]=x,boy[x]=op; //女神初恋
else if( imp[op][x] < imp[op][girl[op]] ) //女神抛弃现任
{
single.push_back(girl[op]);
girl[op]=x;
boy[x]=op;
}
else single.push_back(x); //单身狗被拒绝
}
for(int i=; i<=n; i++) printf("%d\n",boy[i]);
} int main()
{
//freopen("input.txt", "r", stdin);
int t, n, b, j=;
cin>>t;
while(t--)
{
if(j) printf("\n");
j++;
scanf("%d", &n);
for(int i=; i<=n; i++)
{
que[i].clear();
for(int j=; j<=n; j++)
{
scanf("%d",&b);
que[i].push_back(b);
}
}
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
scanf("%d",&b);
imp[i][b]=j; //b对于i来说,排在第几。
}
} cal(n);
}
return ;
}
AC代码
UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)的更多相关文章
- UVA 1175 - Ladies' Choice
		
1175 - Ladies' Choice 链接 稳定婚姻问题. 代码: #include<bits/stdc++.h> using namespace std; typedef long ...
 - UVA 1175 Ladies' Choice 稳定婚姻问题
		
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
 - Ladies' Choice UVALive - 3989 稳定婚姻问题 gale_shapley算法
		
/** 题目: Ladies' Choice UVALive - 3989 链接:https://vjudge.net/problem/UVALive-3989 题意:稳定婚姻问题 思路: gale_ ...
 - 【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
		
Ladies' Choice Teenagers from the local high school have asked you to help them with the organizatio ...
 - UVALive 3989 Ladies' Choice(稳定婚姻问题:稳定匹配、合作博弈)
		
题意:男女各n人,进行婚配,对于每个人来说,所有异性都存在优先次序,即最喜欢某人,其次喜欢某人...输出一个稳定婚配方案.所谓稳定,就是指未结婚的一对异性,彼此喜欢对方的程度都胜过自己的另一半,那么这 ...
 - 【LA 3989 训练指南】女士的选择 【稳定婚姻问题】
		
我们先来学一下稳定婚姻问题 什么是稳定婚姻问题? 有n个女士和n个男士,他们要一一进行配对.每个男士心中对这n个女士都有一个排名,同理,每个女士心里对n个男性也有一个排名.我们要做的是,在他们配对完成 ...
 - LA 3989 - Ladies' Choice 稳定婚姻问题
		
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
 - UVALive-3989 Ladies' Choice (稳定婚姻问题)
		
题目大意:稳定婚姻问题.... 题目分析:模板题. 代码如下: # include<iostream> # include<cstdio> # include<queue ...
 - UVALive 3989 Ladies' Choice
		
Ladies' Choice Time Limit: 6000ms Memory Limit: 131072KB This problem will be judged on UVALive. Ori ...
 
随机推荐
- SPOJ:Another Version of Inversion(二维数组的逆序对)
			
DCE Coders admins are way much geekier than they actually seem! Kartik has been following that tradi ...
 - CodeForces979D:Kuro and GCD and XOR and SUM(Trie树&指针&Xor)
			
Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...
 - POJ2689:Prime Distance(大数区间素数筛)
			
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
 - 棋盘问题(dp)
			
棋盘问题 传送门 题目描述 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个 ...
 - 浏览器加载显示html页面内容的顺序
			
我们经常看到浏览器在加载某个页面时,部分内容先显示出来,又有些内容后显示.那么浏览器加载显示html究竟是按什么顺序进行的呢 其实浏览器加载显示html的顺序是按下面的顺序进行的:1.IE下载的顺序是 ...
 - UI:多线程 、用GCD创建线程
			
什么是应用(程序):就是我们编写的代码编译后生成的app文件 进程:凡是一个运行的程序都可以看作为一个进程,如打开的多个 word,就可以认为是一个进程的多个线程. 线程:至少有一个线程就是主线程,网 ...
 - 泛型Class<T>和 T. <T>
			
private T product; private Class<T> product; 这两个有什么区别呢,查了资料才知道,单独的T 代表一个类型 而 Class<T>代表这 ...
 - A. Hulk
			
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
 - In-App Purchase Programming Guide----(五) ----Delivering Products
			
Delivering Products In the final part of the purchase process, your app waits for the App Store to p ...
 - Python网络爬虫之BeautifulSoup模块
			
一.介绍: Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮 ...