A2. Alternating Deck (hard version) codeforces 1786A2

Problem - A2 - Codeforces

题目大意:有一长度为n的由0,1逐一交替组成的数组,0始终在第一个,A先从前面取一个数字,之后B从前边取2个、3个数字、然后A取4个、5个数字,问AB最后分别取了几个0,几个1

1<=n<=1e6

思路:因为每人取的数字总数是奇数个,且B是从第二个数开始取的,所以B每次取得的第一个数都是偶数位置上的数,也就是1,那么它取得数里面一定有一半(向上取整)个1,和一半个0,A取得数里面有一半(向上取整)和0,和一半个1,模拟即可

//#include<__msvc_all_public_headers.hpp>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		bool flag = 0;//当前是A取还是B取
		int ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0;
		int now = 0;//当前取的第一堆的数字的数量
		while (n)
		{
			if (n < now * 2 + 1)
			{//剩下的数字数量少于要取的数量
				if (flag)
				{//对于B是1的数量向上取整
					ans4 += (n - 1) / 2 + 1;
					ans3 += n / 2;
				}
				else
				{//对于A是0的数量向上取整
					ans1 += (n - 1) / 2 + 1;
					ans2 += n / 2;
				}
				break;
			}
			else
			{
				if (flag)
				{
					ans3 += now;
					ans4 += now + 1;//因为是奇数,向上取整直接+1即可
				}
				else
				{
					ans1 += now + 1;
					ans2 += now;
				}
			}
			n -= now * 2 + 1;
			now += 2;
			flag = 1 - flag;
		}
		cout << ans1 << " " << ans2 << " " << ans3 << " " << ans4 << endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

timidcatt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值