首页 > 其他分享 >CodeForces 1237H Balanced Reversals

CodeForces 1237H Balanced Reversals

时间:2024-01-10 15:27:19浏览次数:24  
标签:Reversals typedef int long Balanced && ans 1237H define

洛谷传送门

CF 传送门

容易想到把 \(s, t\) 分成长度为 \(2\) 的段考虑。容易发现 \(00, 11\) 的个数在操作过程中不会改变,所以若两串的 \(00\) 或 \(11\) 个数不相等则无解。

考虑依次对 \(i = 2, 4, \ldots, n\) 构造 \(s[1 : i] = t[n - i + 1 : n]\)。对于 \(s_{j - 1}s_j = yx\),依次操作 \(j - 2, j\) 可以把 \(s\) 变成 \(xy s_1 s_2 \ldots s_{j - 2}\)。

但是上面的方法只在 \(s\) 的 \(01\) 数量等于 \(t\) 的 \(10\) 数量有效。设 \(f(s)\) 为 \(s\) 的 \(01\) 数量减 \(10\) 数量的值,我们的目标是让 \(f(s) = -f(t)\)。那么若 \(|f(s)| \ge |f(t)|\),一定可以找到 \(s\) 的一个前缀,使得翻转它后 \(f(s) = -f(t)\)。因为 \(f(s)\) 对于 \(s\) 的每个前缀一定 \(+1, -1\) 或不变,因此 \(0 \sim |f(s)|\) 的每个数(或其相反数)都能经过。若 \(|f(s)| < |f(t)|\),反过来,一定可以找到 \(t\) 的一个前缀,使得翻转它后 \(f(s) = -f(t)\)。可以全部操作完后再翻转达到翻转 \(t\) 的前缀的效果。

操作次数为 \(n + 1\)。时间复杂度 \(O(n^2)\)。

code
// Problem: H. Balanced Reversals
// Contest: Codeforces - Codeforces Global Round 5
// URL: https://codeforces.com/problemset/problem/1237/H
// Memory Limit: 512 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 mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))

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

const int maxn = 4040;

int n;
char s[maxn], t[maxn];

void solve() {
	scanf("%s%s", s + 1, t + 1);
	n = strlen(s + 1);
	int c0 = 0, c1 = 0;
	for (int i = 1; i < n; i += 2) {
		c0 += (s[i] == '0' && s[i + 1] == '0');
		c0 -= (t[i] == '0' && t[i + 1] == '0');
		c1 += (s[i] == '1' && s[i + 1] == '1');
		c1 -= (t[i] == '1' && t[i + 1] == '1');
	}
	if (c0 || c1) {
		puts("-1");
		return;
	}
	int s1 = 0, s2 = 0;
	for (int i = 1; i < n; i += 2) {
		if (s[i] == '0' && s[i + 1] == '1') {
			++s1;
		} else if (s[i] == '1' && s[i + 1] == '0') {
			--s1;
		}
		if (t[i] == '0' && t[i + 1] == '1') {
			++s2;
		} else if (t[i] == '1' && t[i + 1] == '0') {
			--s2;
		}
	}
	vector<int> ans;
	int p = -1;
	if (abs(s1) >= abs(s2) && s1 != -s2) {
		for (int i = 2; i <= n; i += 2) {
			if (s[i - 1] == '0' && s[i] == '1') {
				s1 -= 2;
			} else if (s[i - 1] == '1' && s[i] == '0') {
				s1 += 2;
			}
			if (s1 == -s2) {
				ans.pb(i);
				reverse(s + 1, s + i + 1);
				break;
			}
		}
	} else if (s1 != -s2) {
		for (int i = 2; i <= n; i += 2) {
			if (t[i - 1] == '0' && t[i] == '1') {
				s2 -= 2;
			} else if (t[i - 1] == '1' && t[i] == '0') {
				s2 += 2;
			}
			if (s1 == -s2) {
				p = i;
				reverse(t + 1, t + i + 1);
				break;
			}
		}
	}
	for (int i = 2, j = n; i <= n; i += 2, j -= 2) {
		for (int k = i; k <= n; k += 2) {
			if (s[k - 1] == t[j] && s[k] == t[j - 1]) {
				if (k > 2) {
					reverse(s + 1, s + k - 1);
					ans.pb(k - 2);
				}
				reverse(s + 1, s + k + 1);
				ans.pb(k);
				break;
			}
		}
	}
	if (p != -1) {
		ans.pb(p);
	}
	printf("%d\n", (int)ans.size());
	for (int i : ans) {
		printf("%d ", i);
	}
	putchar('\n');
}

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

标签:Reversals,typedef,int,long,Balanced,&&,ans,1237H,define
From: https://www.cnblogs.com/zltzlt-blog/p/17956533

相关文章

  • 『做题记录』[AGC032B] Balanced Neighbors
    [AGC032B]BalancedNeighborslink:https://atcoder.jp/contests/agc032/tasks/agc032_bDescription  给定整数\(N\),构造一个从\(1\)到\(N\)编号的\(N\)个节点的无向图,使得:该图不含有重边和自环,并且是连通的。每个节点的所有邻接节点的编号之和相同。  \(N\l......
  • Paper Reading: Oversampling with Reliably Expanding Minority Class Regions for I
    目录研究动机研究背景研究目的文章贡献本文方法可靠的扩展少数类区域的过采样方法描述方法分析多分类的OREM-MOREM和Boosting的结合计算复杂度实验结果二分类数据集实验实验设置对比实验消融实验调参实验多分类数据集实验对比实验消融实验OREMBoost实验实验设置对比实验优点......
  • The 2023 ICPC Asia Hefei Regional Contest Test D. Balanced Array
    Preface这题赛场上出了个关键点基本都想到的做法,但因为一个地方卡住了没过去导致不得不选择弃掉这道题赛后补了下发现\(O(n\logn)\)的做法是只差临门一脚了,但\(O(n)\)的做法还是trick性挺强的Solution首先考虑枚举\(k\),不难发现此时合法的前缀一定是个连续的区间,其中区间的......
  • 【刷题笔记】110. Balanced Binary Tree
    题目Givenabinarytree,determineifitisheight-balanced.Forthisproblem,aheight-balancedbinarytreeisdefinedas:abinarytreeinwhichthedepthofthetwosubtreesofeverynodeneverdifferbymorethan1.Example1:Giventhefollowingtree......
  • Maximum Balanced Circle
    here首先根据题意,我们不难有数字是连续的这种感悟。而且限制是值域上的,从下标入手发现难以突破,便从值域上入手。从小到大考虑每个数字,然后dp,可以参考这篇题解。至于方案的输出,有两种情况。只有自己\(i\)和\(i-1\),直接输出即可。有自己和\(i-1\)的环,定义print输出环,且最大......
  • SpringCloud复习:(2)@LoadBalanced注解的工作原理
    @LoadBalanced注解标记了一个RestTemplate或WebClientbean使用LoadBalancerClient来进行负载均衡。LoadBalancerAutoConfiguration类给带注解的@RestTemplate添加了拦截器:LoadBalancerInterceptor.具体流程如下:首先定义一个LoadBalancerInterceptor然后定义了一个RestTemplateC......
  • Paper Reading: Sample and feature selecting based ensemble learning for imbalanc
    目录研究动机文章贡献本文方法基于聚类的分层随机欠采样特征选择样本和特征选择的集成学习基于随机森林的SFSHEL实验结果数据集和实验设置KEEL数据集的比较HeartFailure数据集的比较优点和创新点PaperReading是从个人角度进行的一些总结分享,受到个人关注点的侧重和实力所限......
  • @LoadBalanced注解实现负载均衡功能过程
     基本流程如下:拦截我们的RestTemplate请求http://userservice/user/1RibbonLoadBalancerClient会从请求url中获取服务名称,也就是user-serviceDynamicServerListLoadBalancer根据user-service到eureka拉取服务列表eureka返回列表,localhost:8081、localhost:8082I......
  • CF1852B Imbalanced Arrays 题解
    CF1852BImbalancedArrays题解Links洛谷CodeforcesDescription对于一个给定的长度为\(n\)的数组\(A\),定义一个长度为\(n\)的数组\(B\)是不平衡的当且仅当以下全部条件满足:\(-n\leqB_{i}\leqn\)且\(B_{i}\ne0\)。即每个数在\([-n,n]\)内且不为\(0\)。......
  • 平衡二叉树(Balanced Binary Tree)
    平衡二叉树(BalancedBinaryTree)平衡二叉树是一种特殊的二叉搜索树,它具有以下特点:每个节点的左子树和右子树的高度差不超过1。所有的子树也都是平衡二叉树。通过保持平衡性,平衡二叉树可以在最坏情况下仍然具有较好的性能,保证查找、插入和删除操作的时间复杂度为O(logn)。......