题目大意
给定两个长度为
n
的序列
每次操作,选择
A
中的一个区间加上1,再对4取模。
求把
Data Constraint
n≤100000
题解
首先可以求出每个
Ai
变为
Bi
所需的最少操作次数,记为
ai
。
现在问题转化为,求将这个序列
a
全部变为0的最少操作次数。
那么
但是,可能会有
ai+4
使得答案更优。
假设存在一个位置
j
满足
时间复杂度:
SRC
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std ;
#define N 100000 + 10
int A[N] , a[N] , tax[5] ;
int T , n , ans ;
int main() {
scanf( "%d" , &T ) ;
while ( T -- ) {
ans = 0 ;
memset( tax , 0 , sizeof(tax) ) ;
scanf( "%d" , &n ) ;
for (int i = 1 ; i <= n ; i ++ ) scanf( "%d" , &A[i] ) ;
for (int i = 1 ; i <= n ; i ++ ) {
int B ;
scanf( "%d" , &B ) ;
a[i] = (B - A[i] + 4) % 4 ;
ans += max( a[i] - a[i-1] , 0 ) ;
}
for (int i = 2 ; i <= n ; i ++ ) {
int now = a[i] - a[i-1] ;
if ( now > 0 ) {
if ( tax[1] && now > 1 ) tax[1] -- , tax[now] ++ , ans -= now - 1 ;
else if ( tax[2] && now > 2 ) tax[2] -- , tax[now] ++ , ans -= now - 2 ;
} else tax[now+4] ++ ;
}
printf( "%d\n" , ans ) ;
}
return 0 ;
}
以上.