Educational Codeforces Round 162 (Rated for Div. 2) A - C 题解

A - Moving Chips

题意:问给你n个数字,0表示该位置为空,1表示该位置有人,如果有人的地方想移动,只能向自己左边最近的空位置移动,问能否将所有的人排成一列,中间没有空的位置。
题解:多手模几组样例,很容易发现,其实就是找到最左边的有人的地方,和最右边有人的地方,看看中间有几个0即可。
代码:
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int maxn = 1e6 + 7 ;
inline int read() {
	int x = 0, f = 1 ;
	char c = getchar() ;
	while (c > '9' || c < '0') {
		if (c == '-')
			f = -1 ;
		c = getchar() ;
	}
	while (c >= '0' && c <= '9') {
		x = x * 10 + c - '0' ;
		c = getchar() ;
	}
	return x * f ;
}
int t , n , a[maxn] ;
void solve(){
	n = read() ;
	for(int i = 1 ; i <= n ; i ++){
		a[i] = read() ;
	}
	int first = n , last = 1 ;
	for(int i = 1 ; i <= n ; i ++){
		if(a[i] == 1){
			first = i ;
			break ;
		}
	}
	for(int i = n ; i >= 1 ; i --){
		if(a[i] == 1){
			last = i ;
			break ;
		}
	}
	ll ans = 0 ;
	for(int i = first ; i <= last ; i ++){
		if(a[i] == 0){
			ans ++ ;
		}
	}
	cout << ans << endl ;
}
int main(){
	t = read() ;
	while(t --){
		solve() ;
	}
	return 0 ;
}

B - Monsters Attack!

题意:给你n个数字代表他们的血量,n个数字代表他们离你的距离,你的位置在0,k表示每秒攻击的伤害值,问,能否在怪物到达你的位置时,将它击败。
题解:先按照他们离你的距离进行从小到大排序,再按照他们的血量值从大到小排序,然后贪心枚举即可。
代码:
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int maxn = 1e6 + 7 ;
inline int read() {
	int x = 0, f = 1 ;
	char c = getchar() ;
	while (c > '9' || c < '0') {
		if (c == '-')
			f = -1 ;
		c = getchar() ;
	}
	while (c >= '0' && c <= '9') {
		x = x * 10 + c - '0' ;
		c = getchar() ;
	}
	return x * f ;
}
ll t , n , k ;
struct Node{
	ll x , y ;
	bool friend operator < (const Node a , const Node b){
		if(a.y == b.y)
			return a.x > b.x ;
		return a.y < b.y ;
	}
}a[maxn] ;
void solve(){
	n = read() ;
	k = read() ;
	for(int i = 1 ; i <= n ; i ++){
		a[i].x = read() ;
	}
	for(int i = 1 ; i <= n ; i ++){
		a[i].y = read() ;
		a[i].y = abs(0 - a[i].y) ;
	}
	sort(a + 1 , a + n + 1) ;
	ll sum = 0 , rt = 0 ;
	for(int i = 1 ; i <= n ; i ++){
		sum += k * (a[i].y - rt) ;
		rt = a[i].y ;
		if(a[i].x <= sum){
			sum -= a[i].x ;
		}
		else{
			printf("NO\n") ;
			return ;
		}
	}
	printf("YES\n") ;
}
int main(){
	t = read() ;
	while(t --){
		solve() ;
	}
	return 0 ;
}

C - Find B

题意:给你n个数字为数组a,看看能否找到数组b,满足三个条件,\sum a[i] == \sum b[i]并且所有a[i] != b[i],所有b[i] > 0
题解:很容易想到,只有多个1才不能满足条件,因为如果有多个2,那么将所有的2变成1,最后一个数字无限大,即可满足条件,那么大于2的数字也是如此,所以统计有多少个1,看看剩下的数字的和减去1的数量能否小于1,判断即可。
代码:
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int maxn = 1e6 + 7 ;
inline int read() {
	int x = 0, f = 1 ;
	char c = getchar() ;
	while (c > '9' || c < '0') {
		if (c == '-')
			f = -1 ;
		c = getchar() ;
	}
	while (c >= '0' && c <= '9') {
		x = x * 10 + c - '0' ;
		c = getchar() ;
	}
	return x * f ;
}
ll t , n , k ;
ll a[maxn] , sum[maxn] , s[maxn] ;
void solve(){
	n = read() ;
	k = read() ;
	for(int i = 1 ; i <= n ; i ++){
		sum[i] = s[i] = 0 ;
	}
	for(int i = 1 ; i <= n ; i ++){
		a[i] = read() ;
	}
	for(int i = 1 ; i <= n ; i ++){
		if(a[i] == 1){
			sum[i] = 1 ;
		}
		else{
			s[i] = a[i] - 1 ;
		}
	}
	for(int i = 1 ; i <= n ; i ++){
		sum[i] = sum[i - 1] + sum[i] ;
	}
	for(int i = 1 ; i <= n ; i ++){
		s[i] = s[i - 1] + s[i] ;
	}
	for(int i = 1 ; i <= k ; i ++){
		ll u , v ;
		u = read() ;
		v = read() ;
		if(sum[v] - sum[u - 1] > s[v] - s[u - 1] || v - u == 0){
			printf("NO\n") ;
		}
		else{
			printf("YES\n") ;
		}
	}
}
int main(){
	t = read() ;
	while(t --){
		solve() ;
	}
	return 0 ;
}

喜欢作者的点赞收藏加关注哦~

  • 15
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小小许愿瓶

我的成长我做主

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

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

打赏作者

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

抵扣说明:

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

余额充值