Codeforces Round #826 (Div. 3)

A. Compare T-Shirt Sizes

        题目大意:

                让你比较衣服尺码大小

        分析:

                模拟就行了

#include <bits/stdc++.h>

using namespace std ; 
const int N = 5e5 + 8 ; 

void solve()
{	
	string a , b ; 
	cin >> a >> b ; 
	if(a == b){
		cout << "=\n" ; 
	}else{
		int lena = a.size() , lenb = b.size() ; 
		if(a == "M" || b == "M"){
			if(a == "M"){
				if(b[lenb - 1] == 'S'){
					cout << ">\n" ; 
				}else{
					cout << "<\n" ; 
				}
			}else if(b == "M"){
				if(a[lena - 1] == 'S'){
					cout << "<\n" ; 
				}else cout << ">\n" ; 
			}
		}else {
			if(a[lena - 1] == b[lenb - 1]){
				if(a[lena - 1] == 'S'){
					if(lena > lenb){
						cout << "<\n" ; 
					}else cout << ">\n" ; 
				}else{
					if(lena > lenb){
						cout << ">\n" ; 
					}else cout << "<\n" ; 
				}
			}else{
				if(a[lena - 1] == 'S'){
					cout << "<\n" ; 
				}else cout << ">\n" ; 
			}
		}
	}
}

int main(){	
	std::ios::sync_with_stdio(false) ; 
	std::cin.tie(0) ; 
	int T ; 
	cin >> T ; 
	while(T --){
		solve() ; 
	}
	return 0;
}

B. Funny Permutation

        题目大意

                让你给出一个Permutation使得每个数的相邻位至少有一位与他相差1,并且每一位要满足a[i] != i

        分析:

                可以发现如果不考虑题目的a[i] != i的限制条件,那么1,2,3,...,n就是一个合法序列,那么我们可以由此继续联想,把整个序列倒序不就可以满足大部分的数都可以满足a[i] != i 的条件,如果倒序的话只有最中间的位置还满足a[i] == i,我们先倒序,然后到了中间位置的时候,再从1开始往后排列即可

#include <bits/stdc++.h>

using namespace std ; 
const int N = 5e5 + 8 ; 

void solve()
{	
	int n ; 
	cin >> n ; 
	if(n == 3){
		cout << -1 << '\n' ; 
	}else{
		int len = n / 2 ; 
		int t = n ; 
		for(int i = 1 ; i <= len ; i ++){
			cout << t -- << ' ' ; 
		}
		for(int i = 1 ; i <= t ; i ++){
			cout << i << " \n"[i == t] ; 
		}
	}
}

int main(){	
	std::ios::sync_with_stdio(false) ; 
	std::cin.tie(0) ; 
	int T ; 
	cin >> T ; 
	while(T --){
		solve() ; 
	}
	return 0;
}

C. Minimize the Thickness

        题目大意:

                给出一个数组,问能将整个数组分成若干连续的区间,并且每个区间和相等的最小区间大小

        分析:

                直接对整个数组的和的因数来进行判断即可,因为区间是连续的,能提供很多方便的操作

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std ;

const int N = 2008 ; 
int a[N] ; 
int n ; 

int work(int t){
	int tmp = 0 ;
	int len = 0 ;
	int res = -1 ;  
	for(int i = 1 ; i <= n ; i ++){
		tmp += a[i] ;
		len ++ ;  
		if(tmp == t){
			res = max(res , len) ; 
			len = 0 , tmp = 0 ; 
		}else if(tmp > t) return 0 ; 
	}
	return res ; 
}

void solve()
{	
	cin >> n ; 
	int sum = 0 ; 
	for(int i = 1 ; i <= n ; i ++) {
		cin >> a[i] ; sum += a[i] ;  
	}	
	int ans = inf ; 
	for(int i = 1 ; i * i <= sum ; i ++){
		if(sum % i == 0){
			int tmp = work(i) ; 
			if(tmp) ans = min(tmp , ans) ; 
			tmp = work(sum / i) ; 
			if(tmp) ans = min(tmp , ans) ; 
		}
	}
	cout << ans << '\n' ; 
}

int main(){	
	std::ios::sync_with_stdio(false) ; 
	std::cin.tie(0) ; 
	int T ; 
	cin >> T ; 
	while(T --){
		solve() ; 
	}
	return 0;
}

D. Masha and a Beautiful Tree

        题目大意:

                给出一颗二叉树的叶节点的值和顺序,每次能操作父节点使得两个叶节点交换值,问能不能操作使得叶节点是一个递增的Permutation,如果能输出最小的操作次数,如果不能输出-1

        分析:

                简单思考可以发现题目类似于二分的思想,一个序列能变成递增Permutation的条件是一边的最小值大于另一边的最大值。dfs每次分别记录左边和右边的最小值和最大值,如果a[l]>a[r],价值就增加1.每次判断是不是一边的最小值大于另一边的最大值

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f

using namespace std ; 
const int N = 262144 + 8 ; 
int a[N] ; 
int n , ans ; 

void dfs(int l , int r){
	if(l == r) return ;
	if(a[l] > a[r]) ans ++ ; 
	int lmin = inf , lmax = -1 , rmin = inf , rmax = -1 ; 
	int mid = l + r >> 1 ; 
	for(int i = l ; i <= mid ; i ++){
		lmin = min(a[i] , lmin) ; 
		lmax = max(a[i] , lmax) ; 
	}
	for(int i = mid + 1 ; i <= r ; i ++){
		rmin = min(a[i] , rmin) ; 
		rmax = max(a[i] , rmax) ; 
	}
	if(a[l] > a[r]) {
		if(lmin < rmax) ans = inf ; 
	}else{
		if(rmin < lmax) ans = inf ; 
	}
	dfs(l , mid) , dfs(mid + 1 , r) ; 
}

void solve(){
	cin >> n ; 
	for(int i = 1 ; i <= n ; i ++) {
		cin >> a[i] ; 
	}
	if(n == 1) {
		cout << 0 << '\n'; 
		return  ; 
	}
	ans = 0 ; 
	dfs(1 , n) ; 
	if(ans >= inf) ans = -1 ; 
	cout << ans << '\n' ; 

}

int main(){
	int T ;  
	cin >> T ;  
	while(T --){
		solve() ;
	}

	return 0 ; 
}

E. Sending a Sequence Over the Network

        题目大意:

                一个序列被分成若干份,并且每个序列的长度在这个序列的最左边或者最右边,判断一个序列能不能这样被分成若干份

        分析:

              dp,用dp[i]来代表到第i个位置是不是满足条件,那么有两个动态转移方程:

                1.如果以第a[i]结尾,那dp[i - a[i] - 1]也满足条件;

                2.如果以a[i+1]为开头,所以dp[i + a[i + 1] + 1]也满足条件

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 8 ; 
int a[N] , f[N] ; 

void solve(){
	int n ; 
	cin >> n ; 
	for(int i = 1;  i <= n ; i ++) {
		cin >> a[i] ; 
		f[i] = 0 ; 
	}
	f[0] = 1 ; 
	for(int i = 0 ; i <= n ; i ++){
		if(i - a[i] - 1 >= 0 &&f[i - a[i] -1]) f[i] = 1 ; 
		if(!f[i]) continue ;
		if(i + a[i + 1] + 1 <= n) f[i + a[i + 1] + 1] = 1 ; 
	}
	if(f[n]) cout << "YES" << '\n' ; 
	else cout << "NO" << '\n' ; 
}
 
 
int main(){
	std::ios::sync_with_stdio(false) ; 
	std::cin.tie(0) ; 
	int T ; 
	cin >> T ; 
	while(T --){
		solve() ; 
	}

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值