AtCoder Regular Contest 120 C - Swaps 2 线段树模拟

传送门

文章目录

题意:

给你两个序列 a , b a,b a,b,每次可以执行一个操作:将 a [ i ] a[i] a[i] a [ i + 1 ] a[i+1] a[i+1]交换,且让交换后的 a [ i ] + 1 , a [ i + 1 ] − 1 a[i]+1,a[i+1]-1 a[i]+1,a[i+1]1,问将 a a a变成 b b b的最小操作数。不能变成的话输出 − 1 -1 1

思路:

考虑将 a i = a i + i , b i = b i + i a_i=a_i+i,b_i=b_i+i ai=ai+i,bi=bi+i,如果有解的话那么这两个序列必须相同。证明不是很明白,待补。
之后就是裸题了,用线段树维护一下位置求出来就好了。

// Problem: E. String Reversal
// Contest: Codeforces - Educational Codeforces Round 96 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1430/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n;
int a[N],b[N];
map<int,vector<int>>v;
struct Node {
	int l,r;
	LL cnt,lazy;
}tr[N<<2];

void pushdown(int u) {
	if(!tr[u].lazy) return;
	LL lazy=tr[u].lazy; tr[u].lazy=0;
	tr[L].lazy+=lazy; tr[R].lazy+=lazy;
	tr[L].cnt+=lazy; tr[R].cnt+=lazy;
}

void build(int u,int l,int r) {
	tr[u]={l,r,1,0};
	if(l==r) {
		tr[u].cnt=l;
		return;
	}
	build(L,l,Mid); build(R,Mid+1,r);
} 

void change(int u,int l,int r,int c) {
	if(tr[u].l>=l&&tr[u].r<=r) {
		tr[u].cnt+=c;
		tr[u].lazy+=c;
		return;
	}
	pushdown(u);
	if(l<=Mid) change(L,l,r,c);
	if(r>Mid) change(R,l,r,c);
}

int query(int u,int l,int r) {
	if(tr[u].l>=l&&tr[u].r<=r) return tr[u].cnt;
	int ans=0;
	pushdown(u);
	if(l<=Mid) ans+=query(L,l,r);
	if(r>Mid) ans+=query(R,l,r);
	return ans;
}

LL solve() {
	LL ans=0;
	build(1,1,n);
	for(int i=1;i<=n;i++) {
		int pos=v[b[i]].back(); v[b[i]].pop_back();
		LL now=query(1,pos,pos);
		ans+=now-i;
		change(1,1,pos,1);
	}
	return ans;
}

bool check() {
	map<int,int>mp1,mp2;
	for(int i=1;i<=n;i++) mp1[a[i]]++;
	for(int i=1;i<=n;i++) mp2[b[i]]++;
	for(int i=1;i<=n;i++) if(mp1[a[i]]!=mp2[a[i]]) return false;
	return true;
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);
	
	int _=1;
	while(_--) {
		cin>>n;
		for(int i=1;i<=n;i++) scanf("%d",&a[i]),a[i]+=i;
		for(int i=1;i<=n;i++) scanf("%d",&b[i]),b[i]+=i;
		for(int i=n;i>=1;i--) v[a[i]].pb(i);
		if(!check()) {
			puts("-1");
			return 0;
		}
		printf("%lld\n",solve());
	}



	return 0;
}
/*

*/









  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值