首页 > 其他分享 >AtCoder Regular Contest 119 D Grid Repainting 3

AtCoder Regular Contest 119 D Grid Repainting 3

时间:2023-05-02 14:23:00浏览次数:48  
标签:AtCoder typedef Contest int long Regular define

洛谷传送门

AtCoder 传送门

对每个红格的行和列连边,建出二分图。对于二分图中的每个连通块分别考虑。

大胆猜测对于每个连通块,我们都能够进行适当的操作,使得只有一行/一列没被操作(显然不能使所有行和列都被操作)。对应的方案就是随便取一棵生成树,把不被染白的那一行/列拎出来当根,然后自底向上,每次把儿子的那一行/列染白。容易发现儿子的操作不会让父亲无法操作。

现在问题变成了对于每个连通块选择扔掉一行还是扔掉一列。设 \(a,b\) 分别为染白的行数和列数,那么最终被染白的格子为 \(nm - (n-a)(m-b)\)。根据小奥的和一定,差小积大,我们要让 \(n-a\) 和 \(m-b\) 的差尽量大。显然最优方案中 \(a,b\) 必然有一个是 \(0\),比较一下哪个差大即可。

code
// Problem: D - Grid Repainting 3
// Contest: AtCoder - AtCoder Regular Contest 119
// URL: https://atcoder.jp/contests/arc119/tasks/arc119_d
// Memory Limit: 1024 MB
// Time Limit: 2000 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 = 2510;

int n, m, head[maxn << 1], len;
char s[maxn][maxn];
bool vis1[maxn], vis2[maxn], vis[maxn << 1];

struct edge {
	int to, next;
} edges[maxn * maxn * 5];

inline void add_edge(int u, int v) {
	edges[++len].to = v;
	edges[len].next = head[u];
	head[u] = len;
}

struct node {
	int op, x, y;
	node(int a = 0, int b = 0, int c = 0) : op(a), x(b), y(c) {}
};
vector<node> ans;

void dfs(int u) {
	vis[u] = 1;
	for (int i = head[u]; i; i = edges[i].next) {
		int v = edges[i].to;
		if (!vis[v]) {
			dfs(v);
			if (v <= n) {
				ans.pb(1, v, u - n);
			} else {
				ans.pb(2, u, v - n);
			}
		}
	}
}

void solve() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) {
		scanf("%s", s[i] + 1);
		for (int j = 1; j <= m; ++j) {
			if (s[i][j] == 'R') {
				vis1[i] = vis2[j] = 1;
				add_edge(i, j + n);
				add_edge(j + n, i);
			}
		}
	}
	int e1 = 0, e2 = 0;
	for (int i = 1; i <= n; ++i) {
		e1 += (!vis1[i]);
	}
	for (int i = 1; i <= m; ++i) {
		e2 += (!vis2[i]);
	}
	mems(vis, 0);
	if (e1 > e2) {
		for (int i = 1; i <= n; ++i) {
			if (!vis[i]) {
				dfs(i);
			}
		}
	} else {
		for (int i = n + 1; i <= n + m; ++i) {
			if (!vis[i]) {
				dfs(i);
			}
		}
	}
	printf("%d\n", (int)ans.size());
	for (node u : ans) {
		printf("%c %d %d\n", u.op == 1 ? 'X' : 'Y', u.x, u.y);
	}
}

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

标签:AtCoder,typedef,Contest,int,long,Regular,define
From: https://www.cnblogs.com/zltzlt-blog/p/17367642.html

相关文章

  • AtCoder Regular Contest 117 F Gateau
    洛谷传送门AtCoder传送门差分约束算法:给出\(m\)个不等式形如\(x_{a_i}\lex_{b_i}+y_i\),求是否有解。考虑把不等式看成图上的三角不等式\(dis_v\ledis_u+d\),连边\((b_i,a_i,y_i)\),以\(x_i\)最大的位置跑最短路,如果图中有负环就无解。此时求出来的\(dis_i\)......
  • 2023 Hubei Provincial Collegiate Programming Contest题解 C F H I J K M
    补题链接:https://codeforces.com/gym/104337原文链接:https://www.eriktse.com/algorithm/1136.htmlM.DifferentBilling签到题,写几个柿子然后枚举B或C即可。#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;signedmain(){ ios::sync_with_stdio(......
  • Atcoder Grand Contest 059 E - Grid 3-coloring(转化+思维)
    首先先是一步很猛的操作——将三染色视作构造一个矩阵使得相邻元素相差\(1\)且每个元素\(\bmod3\)的值就等于其颜色。证明是显然的,我们按从上到下从左到右的顺序填数,可以归纳证明,对于一个相邻格子颜色互不相同的矩阵的填数方案,处于斜对角的两个格子上写的数要么差\(2\),要么......
  • Mastering Regular Expressions(精通正则表达式) 阅读笔记:第一章,概念
    RealScenario(现实场景)Here'sthescenario:you'regiventhejobofcheckingthepagesonawebserverfordoubledwords(suchas"thisthis"),acommonproblemwithdocumentssubjecttoheavyediting.任务:检查文本中重复的单词(doubledwords),比如&q......
  • AtCoder Regular Contest 122 D XOR Game
    洛谷传送门AtCoder传送门从高到低按位考虑。设当前位有\(k\)个\(1\)。如果\(k\bmod2=0\),这意味着Alice如果选了一个数,Bob可以选相同的数。发现可以分成\((0,0),(1,1)\)两组,递归下去即可。如果\(k\bmod2=1\),意味着答案这一位一定是\(1\)(因为无论如何都不......
  • AtCoder Regular Contest 119 E Pancakes
    洛谷传送门AtCoder传送门感觉挺典的,为啥有2500啊(观察发现,反转序列对\(\sum\limits_{i=1}^{n-1}|a'_i-a'_{i+1}|\)影响不大。具体而言,设反转了\(a_l\sima_r\),记\(s=\sum\limits_{i=1}^{n-1}|a_i-a_{i+1}|\),那么\(s'=s-|a_{l-1}-a_l|-|a_r-a_{r+1}|......
  • 2023 Hubei Provincial Collegiate Programming Contest
    链接:https://codeforces.com/gym/104337C画个图看看,复杂度\(O(1)\)。C++Code#include"bits/stdc++.h"usingnamespacestd;usingi64=longlong;intmain(){ios::sync_with_stdio(false);cin.tie(nullptr);intn,m;cin>>......
  • AtCoder Regular Contest 117 D Miracle Tree
    洛谷传送门AtCoder传送门第一步就没想到可以考虑化简限制。设所有点按\(E_i\)从小到大排序后顺序是\(p_1,p_2,...,p_n\)。发现只需满足\(E_{p_{i+1}}-E_{p_i}\ge\operatorname{dis}(p_i,p_{i+1})\)。证明是对于任意\(i<j<k\),若\(p_i,p_j\)和\(p_j,p_k\)均满......
  • AtCoder Beginner Contest 231
    A-WaterPressure#include<bits/stdc++.h>usingnamespacestd;intmain(){ intn; cin>>n; printf("%.6lf\n",n/100.0);return0;}B-Election#include<bits/stdc++.h>usingnamespacestd;intmain(){ ios:......
  • Educational DP Contest
    EducationalDPContestATcoder_link夯实基础的好东西I记录一下此时第i个有多少概率小于等于j的就可以了。#include<bits/stdc++.h>usingnamespacestd;constintN=3005;#definedbdoubledbdp[N][N];intn;dbp[N];intmain(){ios::sync_with_stdio(fal......