Codeforces Round #639 (Div. 2)萌新题解

Codeforces Round #639 (Div. 2)萌新题解

Because of the long queue, the round is unrated. I’m trying to investigate it. Just solve problems for fun. I hope you liked them. Mike. 可惜因为服务器这次不算rating了
A. Puzzle Pieces
题意:给出若干一样的拼图。问可否用这些拼图拼成n * m的块。
在这里插入图片描述
tips:显然1行或者1列肯定可以。
在这里插入图片描述
2 * 2也可以。
在这里插入图片描述
而3 * 2就不行了:
在这里插入图片描述
而所有大于3 * 2的块必定包含3 * 2的子块。所以都不行。
代码异常简单:(害怕错误我还检查了好长时间hhh)

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int n, m, t;

void solve(){
	scanf("%d%d", &n, &m);
	if(n == 1 || m == 1)	printf("YES\n");
	else if(n == 2 && m == 2)	printf("YES\n");
	else	printf("NO\n");
} 

int main(){
	scanf("%d",&t);
	while(t--){
		solve();
	}
	return 0;
}

B. Card Constructions
题意:用poker搭扑克塔,如下图所示。问:给出n张扑克牌,每次都尽肯能摆最大的塔,最多能完整摆多少个。
在这里插入图片描述
tips:
我的做法:找规律。
第二个比第一个多两个尖(cards standing up)和一个平放的牌(horizontal cards)。2 * 2 + 1
第三个比第而个多三个尖(cards standing up)和两个平放的牌(horizontal cards)。3 * 2 + 2
以此类推,可以打表求出所有的塔需要的牌数。(因为n不超过1e9,大约30000个就差不多了)。
既然数组是递增的,可以用二分来计算(30000的数据量不二分也可以过)。

#include<bits/stdc++.h>
#define ll long long

using namespace std;

int n,t, book[100005] = {0, 2};

void makebook(){
	for(int i = 2; i <= 30005; ++i)
		book[i] = book[i - 1] + 3 * i - 1;
}

void solve(){				//线性的看,不二分 
	scanf("%d",&n);
	int cnt = 0;
	for(int i = 30005; i >= 1;){
		if(book[i] > n)	--i;
		else{
			cnt++;
			n -= book[i];
		}
		if(n <= 1)	break;
	}
	printf("%d\n",cnt);
} 

void solve2(){					//二分搜索 
	scanf("%d",&n);
	int cnt = 0, right = 30005;
	while(n >= 2){
		int pos = upper_bound(book + 1, book + 1 + right, n) - book - 1;
		right = pos;
		n -= book[pos];
		if(n >= 0)	cnt++;
	}
	printf("%d\n",cnt);
}

int main(){
	scanf("%d",&t);
	makebook();
	while(t--){
		solve2();
	}
	return 0;
}

要注意从小到大的数组,upper_bound()找到的是第一个大于n的位置位置-1才是最后一个小于等于n的位置。
{复习一下:如果数组是从大到小的顺序,要用greater()实现~}

P.S.也可以直接推一个公式~
Let’s count the number of cards in a pyramid of height h. There are 2(1+2+3+⋯+h) cards standing up, and there are 0+1+2+⋯+(h−1) horizontal cards. So, there are 3/2h * h + 1/2 h cards total.

C. Hilbert’s Hotel
题意:希尔伯特旅馆问题~经典离散数学问题。原本k号客人住在k号房间,现在给出一个n长度的数组,按照一定的规则移动后,是否仍然满足每个客人一间房间。
tips:1.“More formally, r = k mod n is the smallest non-negative integer such that k−r is divisible by n.”
e.g. (−1337) mod 3 = 1 ,而不是-2.
2.规则:Then for each integer k, the guest in room k is moved to room number k+a【k mod n】
3.整数集合是一个有无限元素的可数集,模n等价类共有n个(0,1,……n-1).只需要看0-(n-1)这些数字移动之后是否满足条件即可~即:Checked with a boolean array to make sure each number from 0 to n−1 is included.(移动完后是否恰好可以分配在0-(n-1)的等价类中)

#include<bits/stdc++.h>
using namespace std;

int n, t, book[200005];

void solve(){
	scanf("%d",&n);
	int flag[n + 5] = {0};				//标记数组
	for(int i = 0; i < n; ++i)
		scanf("%d", &book[i]);
	for(int i = 0; i < n; ++i){
		int tmp = (i + book[i]) % n;
		if(tmp < 0)	tmp += n;			//按照定义,余数为负数要转化为正的
		flag[tmp] = 1;
	}
	for(int i = 0; i < n; ++i)
		if(!flag[i]){
			printf("NO\n");
			return ;
		}	
	printf("YES\n");
}

int main(){
	scanf("%d",&t);
	while(t--){
		solve();
	}
	return 0;
}

未完待续ing……

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值