前缀异或和,异或差分数组

[USACO07MAR]Face The Right Way G (前缀异或和,异或差分数组)

题目描述

Farmer John has arranged his N (1 ≤ N ≤ 5,000) cows in a row and many of them are facing forward, like good cows. Some of them are facing backward, though, and he needs them all to face forward to make his life perfect.

Fortunately, FJ recently bought an automatic cow turning machine. Since he purchased the discount model, it must be irrevocably preset to turn K (1 ≤ K ≤ N) cows at once, and it can only turn cows that are all standing next to each other in line. Each time the machine is used, it reverses the facing direction of a contiguous group of K cows in the line (one cannot use it on fewer than K cows, e.g., at the either end of the line of cows). Each cow remains in the same *location* as before, but ends up facing the *opposite direction*. A cow that starts out facing forward will be turned backward by the machine and vice-versa.

Because FJ must pick a single, never-changing value of K, please help him determine the minimum value of K that minimizes the number of operations required by the machine to make all the cows face forward. Also determine M, the minimum number of machine operations required to get all the cows facing forward using that value of K.

N N N 头牛排成一列 1 ≤ N ≤ 5000 1 \le N \le 5000 1N5000。每头牛或者向前或者向后。为了让所有牛都面向前方,农夫每次可以将 K K K 头连续的牛转向 1 ≤ K ≤ N 1 \le K \le N 1KN,求使操作次数最小的相应 K K K 和最小的操作次数 M M M F F F 为朝前, B B B 为朝后。

请在一行输出两个数字 K K K M M M,用空格分开。

输入格式

Line 1: A single integer: N

Lines 2…N+1: Line i+1 contains a single character, F or B, indicating whether cow i is facing forward or backward.

输出格式

Line 1: Two space-separated integers: K and M

样例 #1

样例输入 #1

7
B
B
F
B
F
B
B

样例输出 #1

3 3

提示

For K = 3, the machine must be operated three times: turn cows (1,2,3), (3,4,5), and finally (5,6,7)


首先我想到了两次二分答案,然后发现一次二分答案就行。知道写完之后只有10分,发现这题没有两段性!

只能暴力枚举,(又因为答案输出反了,直接自闭)。 TLE之后,发现可以用差分来做。

但是我不知道怎么知道当前元素的状态,看了下别人的代码会了,就是维护差分数组,维护前缀和差分,当前元素的状态就是 p r e 异或 a [ i ] pre 异或 a[i] pre异或a[i]

/*
A: 10min
B: 20min
C: 30min
D: 40min
*/ 
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <assert.h>
#include <sstream>
#define pb push_back 
#define all(x) (x).begin(),(x).end()
#define mem(f, x) memset(f,x,sizeof(f)) 
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){for(auto c:v)os<<c<<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>&v){for(auto c:v)os<<c.first<<" "<<c.second<<endl;return os;}
template<typename T>inline void rd(T &a) {
    char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
    while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}

typedef pair<long long ,long long >PII;
typedef pair<long,long>PLL;

typedef long long ll;
typedef unsigned long long ull; 
const int N=1e6+10,M=1e9+7;

int n,a[5010],b[5010],Xor[N];
pair<int,int>res(5010,5010);

bool check(int x){
    int cnt = 0;
    fo(i,1,n)b[i] = a[i],Xor[i] = 0;
    int pre = 0; // 前缀异或和
    for(int i=1;i<=n-x+1;i++){
     	int t = pre ^ a[i];
     	// 每个点当前的状态就是前缀异或和异或原本的状态
    	if(!t){
            cnt++;
            Xor[i] ^= 1;
            Xor[i+x-1] ^= 1;
            // 异或差分
        }
        pre ^= Xor[i];
        if(cnt>res.first)return false;
        
    }

	for(int i=n-x+2;i<=n;i++){
		int t = pre ^ a[i];
		if(!t)return false;
		pre ^= Xor[i];
	}
	
    if(cnt < res.first)res = {cnt,x};
    return true;
}

void solve(){
    cin>>n;
    fo(i,1,n){
        char x;cin>>x;
        if(x == 'F')a[i] = 1;
        else a[i] = 0;
    }
    for(int mid = 1;mid<=n;mid++){
        check(mid);
    }
    cout<<res.second<<" "<<res.first;
}
int main(){
    solve();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值