Codeforces Round 849 (Div. 4)
蒟蒻的CODEFORCES补题
(能力不够,写不完)
A - Codeforces Checking
#include <bits/stdc++.h>
using namespace std;
string str = "codeforces";
int main(){
int n;
cin >> n;
while(n -- ){
char c;
cin >> c;
int res = str.find(c);
if(res == -1)cout << "NO" << endl;
else cout << "YES" << endl;
}
return 0;
}
B - Following Directions
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
while(t -- ){
int x = 0, y = 0,n;
string move;
bool flag = false;
cin >> n >> move;
for(int i = 0;i < move.length() && !flag;i ++ ){
if(move[i] == 'U')y ++ ;
else if(move[i] == 'D')y -- ;
else if(move[i] == 'L')x -- ;
else x ++ ;
if(x == 1 && y == 1){
flag = true;
}
}
if(flag)cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
C - Prepend and Append
#include <bits/stdc++.h>
using namespace std;
int main(){
int t;
cin >> t;
while(t -- ){
string str;
int n,cnt = 0;
cin >> n >> str;
if(str[0] != str[n - 1]){
cnt += 2;
for(int i = 1,j = n - 2;i < j && str[i] != str[j];i ++ ,j -- ){
cnt += 2 ;
}
cout << n - cnt << endl;
}else {
cout << n << endl;
}
}
return 0;
}
D - Distinct Split
#include <bits/stdc++.h>
using namespace std;
int h1[30] = {0},h2[30] = {0};
int main(){
int t;
cin >> t;
while(t -- ){
memset(h1,0,sizeof h1);
memset(h2,0,sizeof h2);
int n,num_a = 0,num_b = 0,sum = -1;
string str;
cin >> n >> str;
for(int i = 0;i < n;i ++ ){
h1[str[i] - 'a'] ++ ;
if(h1[str[i] -'a'] == 1)num_a ++ ;
}
for(int i = n - 1;i >= 0;i -- ){
h1[str[i] -'a'] -- ;
h2[str[i] -'a'] ++ ;
if(h1[str[i] -'a'] == 0)num_a -- ;
if(h2[str[i] -'a'] == 1)num_b ++ ;
sum = max(sum,num_a + num_b);
}
cout << sum << endl;
}
return 0;
}
E - Negatives and Positives
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int main(){
int t;
cin >> t;
while(t -- ){
int n;
long long sum = 0;
cin >> n;
long long a[n];
for(int i = 0;i < n;i ++ ){
cin >> a[i];
sum += a[i];
}
sort(a,a + n);
for(int i = 0;i < n - 1;i += 2 ){
if(a[i] < 0){
sum -= min(2 * (a[i] + a[i + 1]),(long long)0);
}else break;
}
cout << sum << endl;
}
return 0;
}