Codeforces Round #658 (Div. 2) C题

C1
暴力可过

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int maxn = 200010;
int a[maxn];

int main(){
	int t;
	cin >> t;
	while(t--){
		int n;
		cin >> n;
		string s1,s2;
		cin >> s1 >> s2;
		if(s1 == s2){
			cout << "0" << endl;
			continue;
		}
		int k = 0;
		for(int i = n-1;i >= 0;i--){
			if(s2[i] == s1[i]) continue;
			if(s2[i] == s1[0]){
				a[k++] = 1;
				if(s1[0] == '0') s1[0] = '1';
				else s1[0] = '0'; 
			}
			for(int j = 0;j <= i;j++){
				if(s1[j] == '0') s1[j] = '1';
				else s1[j] = '0'; 
			}
			for(int j = 0;j <= i/2;j++){
				char tt = s1[j];
				s1[j] = s1[i-j];
				s1[i-j] = tt;
			}
			a[k++] = i+1;
		}
		cout << k;
		for(int i = 0;i<k;i++){
			cout << " " << a[i];
		}
		cout << endl;
	}
	return 0;
}

C2
树状数组优化

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstring> 
using namespace std;
const int maxn = 200010;
int a[maxn];
int c[maxn/2];
int n;

int ask(int x){
	int ans = 0;
	for(;x;x -= x & -x) ans += c[x];
	return ans;
}

void add(int x,int y){
	for(;x <= n;x += x & -x){
		c[x] += y;
	}
}

int main(){
	int t;
	cin >> t;
	while(t--){
		cin >> n;
		vector<int> b(n+10,0);
		memset(c,0,sizeof(int)*(n+10));
		string s1,s2;
		cin >> s1 >> s2;
		if(s1 == s2){
			cout << "0" << endl;
			continue;
		}
		int k = 0;
		int flag = 0;//正
		int l = 0;
		int r = n-1; 
		for(int i = n-1;i >= 0;i--){
			if(flag == 0) {
				char tt = s1[r];
				int ans = ask(r+1);
				if(ans%2 == 1) {
					if(tt == '1') tt = '0';
					else tt = '1';
				}
				if(s2[i] == tt){
					r--;
				    continue;
				}
				
			}
			if(flag == 1) {
				char tt = s1[l];
				int ans = ask(l+1);
				if(ans%2 == 1) {
					if(tt == '1') tt = '0';
					else tt = '1';
				}
				if(s2[i] == tt){
					l++;
				    continue;
				}
			}
			if(flag == 0){
				char tt = s1[l];
				int ans = ask(l+1);
				if(ans%2 == 1) {
					if(tt == '1') tt = '0';
					else tt = '1';
				}
				if(s2[i] == s1[l]){
					a[k++] = 1;
				}
				add(l+1,1);
				add(l+i+2,-1);
			}
			else{
				char tt = s1[r];
				int ans = ask(r+1);
				if(ans%2 == 1) {
					if(tt == '1') tt = '0';
					else tt = '1';
				}
				if(s2[i] == tt){
					a[k++] = 1;
				}
				add(r+2,-1);
				add(r-i+1,1);
			}
			a[k++] = i+1;
			flag ^= 1;//翻转 
			if(flag == 0) r--;
			else l++;
		}
		cout << k;
		for(int i = 0;i<k;i++){
			cout << " " << a[i];
		}
		cout << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值