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 (保留)