CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!)(A-C)

//日练

好困。。感觉这几天脑子都钝钝的

vp了一下子

CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!)

A. Indirect Sort

意:每次挑i,j,k (1≤i<j<k≤n).若ai>ak,则ai=ai+aj;若ai<=ak,则swap(aj,ak)

能否得到非降序排列

思路:只需找到一个ai,使它小于其他两个要交换的数:aj,ak即可;这就要求最小的数1一定在1st位置

void solve()
{
	cin >> n;
	for (int i = 1; i <= n; i++)cin >> a[i];
	if (a[1] == 1)cout << "Yes\n";
	else cout << "No\n";
}
B. Maximum Substring

意:串s的子串中x为0个数,y为1个数,s的cost运算规则:

找到s的max_cost

思路:子串为s时x,y一定最大;只取0..0串x^{2}max;只取1...1串y^{2}max

void solve()
{
	cin >> n;
	cin >> s;
	s = " " + s;
	int cnt1 = 0, cnt0 = 0;
	for (int i = 1; i <= n; i++)
		if (s[i] == '1')cnt1++;
		else cnt0++;
		
	int ans = 0;
	if (cnt1&&cnt0)
		ans = cnt1 * cnt0;

 
	cnt1 = 0, cnt0 = 0;

	for (int i = 1; i <= n; i++)
		if (s[i] == '1')
		{
			if (cnt0)ans = max(ans, cnt0 * cnt0);
			cnt0 = 0;
			cnt1++;
		}
		else if(s[i]=='0')
		{
			if (cnt1)ans = max(ans, cnt1 * cnt1);
			cnt1 = 0;
			cnt0++;
		}
	if(cnt1)ans=max(ans,cnt1*cnt1);
	if(cnt0)ans=max(ans,cnt0*cnt0);
	
	cout << ans << endl;
}
C. Complementary XOR

意:每次可选一区间[l,r]使a串在该区间内的部分反转,b串不在该区间内的反转,最多操作n+5次,能否使a,b串全为0

思路:发现要么ai^bi全为1;要么全为0;其他不行。每次操作a串为0的位置:当ai^bi全为1或全为0时,a串的0分奇偶讨论

//注意n=1的情况:a=1,b=0   or a=0,b=0 这两种满足

void solve()
{
	cin >> n;
	cin >> s >> t;
	s = " " + s;
	t = " " + t;
	int cnt = 0, ct = 0, ct1 = 0;
	for (int i = 1; i <= n; i++)
	{
		int a = s[i] - '0', b = t[i] - '0';
		int res = a ^ b;
		if (res)cnt++;
		if (!a)ct++;
		if (!b)ct1++;
	}
	int flag = 0;
	if (cnt == n || !cnt)flag = 1;
 
	vector<pair<int,int>>v;
	if (n == 1)
	{
		if (!ct1)
			flag = 0;
		else if (cnt)v.push_back({ 1,1 });
	}
	else if (cnt == n)
	{
			for (int i = 1; i <= n; i++)
				if (s[i] == '0')
					v.push_back({ i,i });
			if (ct % 2)
			{
				v.push_back({ 1,1 });
				v.push_back({ 2,n });
			}
			else v.push_back({ 1,n });
	}
	else
	{
		for (int i = 1; i <= n; i++)
			if (s[i] == '0')
				v.push_back({ i,i });
		if (ct % 2)v.push_back({ 1,n });
		else
		{
			v.push_back({ 1,1 });
			v.push_back({ 2,n });
		}
	}
	if (flag)
	{
		cout << "YES\n";
		cout << v.size() << endl;
		for (auto x : v)
			cout << x.first << " " << x.second << endl;
	}
	else
		cout << "NO\n";
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值