首页 > 其他分享 >CodeForces 1147F Zigzag Game

CodeForces 1147F Zigzag Game

时间:2023-03-13 10:11:56浏览次数:49  
标签:1147F typedef int CodeForces long while Game maxn define

洛谷传送门

CF 传送门

很有意思的题。

考虑若无边权的限制则 B 必胜,不妨猜想有了限制之后仍然是 B 必胜。

假设 A 选了 I(若 A 选了 D 可以边权取相反数),若 B 走了 \((a,b)\),A 走了 \((b,c)\),则 B 还能走 \((c,d)\)。即 \(w_{b,c} > w_{a,b}\) 且 \(w_{b,c} < w_{c,d}\),即不存在原图的匹配 \((a,b)\),\((c,d)\) 使得 \(w_{a,b} < w_{b,c} < w_{c,d}\)。

将起点的一部(即 \(a,c\) 所在的一部)的所有点按照边权从大到小排序,另一部按照边权从小到大排序,则对于匹配 \((a,b)\),\((c,d)\),若 \(b\) 认为 \(c\) 比 \(a\) 优(\(w_{b,c} > w_{b,a}\)),则 \(c\) 不能认为 \(b\) 比 \(d\) 优(\(w_{c,b} < w_{c,d}\))。这实际上就是稳定婚姻问题。由于稳定婚姻问题一定有解,因此原问题一定有解。

稳定婚姻问题算法流程:每轮一位单身男性表白之前没表白过的且最优的女性,若该女性无伴侣则他们结成伴侣;否则若该男性优于该女性伴侣,则该女性放弃之前的伴侣,与该男性结成伴侣。

code
// Problem: F. Zigzag Game
// Contest: Codeforces - Forethought Future Cup - Final Round (Onsite Finalists Only)
// URL: https://codeforces.com/problemset/problem/1147/F
// Memory Limit: 256 MB
// Time Limit: 5000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;

const int maxn = 110;

int n, a[maxn][maxn], b[maxn][maxn], c[maxn], pos[maxn];
vector<int> G[maxn];

void work(int op) {
	queue<int> q;
	mems(c, 0);
	mems(pos, 0);
	for (int i = 1; i <= n; ++i) {
		q.push(i);
		vector<int>().swap(G[i]);
		for (int j = 1; j <= n; ++j) {
			G[i].pb(j);
		}
		sort(G[i].begin(), G[i].end(), [&](int x, int y) {
			return (a[i][x] < a[i][y]) ^ op;
		});
	}
	for (int i = 1; i <= n; ++i) {
		vector<int> vc;
		for (int j = 1; j <= n; ++j) {
			vc.pb(j);
		}
		sort(vc.begin(), vc.end(), [&](int x, int y) {
			return (a[x][i] > a[y][i]) ^ op;
		});
		for (int j = 0; j < n; ++j) {
			b[i][vc[j]] = j + 1;
		}
	}
	while (q.size()) {
		int u = q.front();
		q.pop();
		while (!c[u]) {
			int v = G[u][pos[u]++];
			if (!c[v + n] || b[v][u] < b[v][c[v + n]]) {
				c[c[v + n]] = 0;
				if (c[v + n]) {
					q.push(c[v + n]);
				}
				c[u] = v + n;
				c[v + n] = u;
			}
		}
	}
}

void solve() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			scanf("%d", &a[i][j]);
		}
	}
	puts("B");
	fflush(stdout);
	char op[9];
	int x;
	scanf("%s%d", op, &x);
	if (op[0] == 'D') {
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j) {
				a[i][j] = -a[i][j];
			}
		}
	}
	work(x > n);
	do {
		printf("%d\n", c[x]);
		fflush(stdout);
		scanf("%d", &x);
		if (x == -2) {
			exit(0);
		}
	} while (x != -1);
}

int main() {
	int T = 1;
	scanf("%d", &T);
	while (T--) {
		solve();
	}
	return 0;
}

标签:1147F,typedef,int,CodeForces,long,while,Game,maxn,define
From: https://www.cnblogs.com/zltzlt-blog/p/17210403.html

相关文章

  • Codeforces Round 857 (Div. 2)
    题目链接A核心思路读懂题目也就不难了。//Problem:A.Likes//Contest:Codeforces-CodeforcesRound857(Div.2)//URL:https://codeforces.com/contest/180......
  • Codeforces比赛规则梳理
    1、排名div选手们按Rating以1700为界划分为Div.1和Div.2两类,Div.1的比赛较难,Div.1的ABC三题会和Div.2的CDE三题相同。每次比赛结束后Rating都会依据此前各个选手的Rating和......
  • Codeforces Round #666 (Div. 2)D. Stoned Game(博弈问题)
    problemT和HL玩游戏,n堆石头,玩家轮流在石堆中选择一个(但不能是上一个人取的那堆)取一个石子一旦有一方不能取石头则判输solution统计所有石头数,如果总数小于mx(最多石头的一堆)......
  • Codeforces Round 857 (Div. 2)
    更好的阅读第一次进入时加载缓慢,请耐心等待。赛时降智,菜是原罪。A.Likes简单题。#include<bits/stdc++.h>usingnamespacestd;intT,n,a[11111],s[11111];intm......
  • FinalHgame wp
    ssti常规的sstiphp-blogadmin12345进入后台,发一篇文章,内容填<?phpeval($_POST['pass']);直接getshell然后在login.php里面加一句file_put_contents('login.txt',......
  • Game On Graph
    最近总是见到在有向图上面移棋子的博弈论题,都是如果把有向图换成DAG就很naive的,核心问题都在于如何处理环,所以来记一记。Alice负责走棋子,在Alice走之前Bob可以......
  • Codeforces Round 857 (Div. 2)(持续更新)
    Preface貌似CF的Div1/Div2分场就有1900的分界线,大号打不了Div2就很难受同时我对自己的水平有清晰的认知,现在打这种纯Div1的场肯定就是纯被虐,所以也不敢去Div1所以索性开......
  • A. Stone Game
    A.StoneGame代码点击查看代码#include<iostream>#include<vector>#include<algorithm>usingnamespacestd;intmain(){ intt; cin>>t; while(t--){ ......
  • A. Computer Game【dfs诈骗】
    A.ComputerGame代码点击查看代码#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue......
  • Games101-Cp1-Transformation
    最近为了求职重新开始把图形学相关的内容重新系统的学习,先把Games101的内容入门,然后把虎书相关的内容补充。Transformation矩阵变换可以对不同坐标系之间进行转换,在这个......