Codeforces Round #715 (Div. 2)

A Average Height

题意很简单,构造一种排列,使得相邻的两个数相加起来是整数的个数最多

我们小学就知道,偶加偶是偶,奇数加奇数是偶数,奇数加偶数是奇数,只有偶数除二是整数

就是构造一个排列,奇数放一起,偶数放一起就好了

// Problem: A. Average Height
// Contest: Codeforces - Codeforces Round #715 (Div. 2)
// URL: https://codeforces.com/contest/1509/problem/0
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#define ll long long
#define re return

using namespace std;

typedef pair<int, int> PII;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

int T;
int a[200000 + 10];
int n;

int main(){
	cin >> T;
	while(T--){
		cin >> n;
		for(int i = 1; i <= n; i++){
			int x;
			scanf("%d", &x);
			a[x]++;
		}
		
		for(int i = 1; i <= 200000; i += 2){
			while(a[i]){
				printf("%d ", i);
				a[i]--;
			}
		}
		for(int i = 2; i <= 200000; i += 2){
			while(a[i]){
				printf("%d ", i);
				a[i]--;
			}
		}
		printf("\n");
	}
	re 0;
}

B TMT Document

反思:这个题当时想法已经跟题解很接近了,考虑了左右两边,但是却一直在想构成答案应该去取哪几个T,而并没有完全验证有的条件

首先,有解的答案一定是m的个数的三倍是n(显然

那么每个m所需要的至少是他的左边有一个t,右边有一个t,根据这个性质,我们累积每个m的左右两边各有多少个t即可

// Problem: B. TMT Document
// Contest: Codeforces - Codeforces Round #715 (Div. 2)
// URL: https://codeforces.com/contest/1509/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#define ll long long
#define re return

using namespace std;

typedef pair<int, int> PII;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

int T;
int n;
int s[300000 + 10];
int tl[300000 + 10];
int tr[300000 + 10];
int ml[300000 + 10];
int mr[300000 + 10];

int main(){
	cin >> T;
	while(T--){
		scanf("%d", &n);
		
		int cnt = 0; // m
		string ss;
		cin >> ss;
		for(int i = 1; i <= n; i++){
			if(ss[i - 1] == 'M'){
				cnt++;
				s[i] = int('M');
			}
			else{
				s[i] = int('T');
			}
		}
		
		// for(int i = 1; i <= n; i++){
			// printf("%c ", s[i]);
		// }
		// printf("\n");
		
		if(cnt * 3 != n){
			cout << "NO" << endl;
			continue;
		}
		
		int flag = 1;
		
		for(int i = 1; i <= n; i++){
			tl[i] = tl[i - 1];
			ml[i] = ml[i - 1];
			if(s[i] == int('M')) ml[i]++;
			else tl[i]++;
			if(ml[i] > tl[i]) flag = 0;
		}
		
		reverse(s + 1, s + 1 + n);
		
		for(int i = 1; i <= n; i++){
			tr[i] = tr[i - 1];
			mr[i] = mr[i - 1];
			if(s[i] == int('M')) mr[i]++;
			else tr[i]++;
			if(mr[i] > tr[i]) flag = 0;
		}
		
		if(flag) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	re 0;
}

C The Sports Festival

区间dp (保留)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值