A. Short Sort
思路:abc别同时不在自己位置上就行
#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m;
//int a[N], b[N];
int gcd(int a, int b){
if(b) while((a %= b) && (b %= a));
return a + b;
}
int main(){
fast();
cin >> T;
while(T --){
string s; cin >> s;
if(s[0] != 'a' && s[1] != 'b' && s[2] != 'c')
cout << "NO" << endl;
else cout << "YES" << endl;
}
}
B. Good Kid
思路:排序,不在首加一就在尾加一
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m;
int a[N], b[N];
int gcd(int a, int b){
if(b) while((a %= b) && (b %= a));
return a + b;
}
signed main(){
fast();
cin >> T;
while(T --){
cin >> n;
for(int i = 0; i < n; i ++)
cin >> a[i];
sort(a, a + n);
int cnt = 1;
a[0] += 1;
for(int i = 0; i < n; i ++)
cnt *= a[i];
a[0] -= 1;
a[n - 1] += 1;
int ans = 1;
for(int i = 0; i < n; i ++)
ans *= a[i];
cout << max(ans, cnt) << endl;
}
}
C. Target Practice
思路:找X点距离最近的边界距离
#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m;
//int a[N], b[N];
int gcd(int a, int b){
if(b) while((a %= b) && (b %= a));
return a + b;
}
int main(){
fast();
cin >> T;
while(T --){
char g[100][100];
for(int i = 0; i < 10; i ++){
string s;
cin >> s;
for(int j = 0; j < 10; j ++)
g[i][j] = s[j];
}
int ans = 0;
for(int i = 0; i < 10; i ++){
for(int j = 0; j < 10; j ++){
if(g[i][j] == 'X'){
//cout << i << " " << j << "+++++" << endl;
ans += min(min((i - 0), (9- i)), min((j - 0), (9 - j))) + 1;
//cout << min(min((i - 0), (10- i)), min((j - 0), (10 - j)))<< "++++" << endl;
}
}
}
cout << ans << endl;
}
}
D. 1D Eraser
思路:每次最多取K个,遇见黑色就开始取就行
#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m, k;
//int a[N], b[N];
int gcd(int a, int b){
if(b) while((a %= b) && (b %= a));
return a + b;
}
int main(){
fast();
cin >> T;
while(T --){
cin >> n >> k;
string s; cin >> s;
int cnt = 0, tt = 0, flag = 0;
for(int i = 0; i < n; i ++){
if(s[i] == 'B') tt ++, flag = 1;
else if(flag) tt ++;
if(tt == k){
cnt ++;
tt = 0;
flag = 0;
}
}
if(tt) cnt ++;
cout << cnt << endl;
}
}
E. Building an Aquarium
思路:直接暴力二分
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m, x, ma, mi, ans;
int a[N], sum[N];
map<int, int> mp;
int gcd(int a, int b){
if(b) while((a %= b) && (b %= a));
return a + b;
}
bool check(int mid){
int sum = 0;
for(int i = 1; i <= n; i ++){
sum += max(mid - a[i], (LL)0);
}
return x >= sum;
}
signed main(){
fast();
cin >> T;
while(T --){
ma = 0, mi = INF;
cin >> n >> x;
for(int i = 1; i <= n; i ++){
cin >> a[i];
}
sort(a + 1, a + n + 1);
int l = 0, r = 3e9;
while(l < r){
int mid = l + r + 1 >> 1;
if(check(mid)) l = mid;
else r = mid - 1;
}
cout << l << endl;
}
}
F. Money Trees
思路:存取每段符合条件的连续子序列,然后双指针分别判断每一段即可,细节处理较多
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, k;
int a[N], h[N];
int gcd(int a, int b){
if(b) while((a %= b) && (b %= a));
return a + b;
}
signed main(){
fast();
cin >> T;
while(T --){
int flag = 0;
cin >> n >> k;
for(int i = 0; i < n; i ++){
cin >> a[i];
if(a[i] < k) flag = 1;
}
for(int i = 0; i < n; i ++)
cin >> h[i];
int l = 0, r = 0;
vector<PII> q;
for(int i = 0; i < n - 1; i ++){
if(h[i] % h[i + 1] == 0) r ++;
else{
q.push_back({l, r});//找出符合条件的连续子序列
l = i + 1, r = i + 1;
}
}
q.push_back({l, r});
int max_cnt = 0;
for(int i = 0; i < q.size(); i ++){
int l = q[i].first, r = l;
int sum = 0, cnt = -1;
while(l <= r && r <= q[i].second){//查询每一段子序列
if(cnt != r) sum += a[r];//右节点不移动
if(sum <= k){//子序列中符合条件的子序列
max_cnt = max(r - l + 1, max_cnt);
r ++, cnt = -1;//固定左节点,移动右节点
}
else{
if(l == r){//节点相遇,跳至下一节点开始尝试移动右节点
sum = 0, cnt = -1;
l ++, r ++;
}
else{
sum -= a[l], cnt = r;//移动左节点,清除sum中的左节点,此时右节点不移动,标记
l ++;
}
}
}
}
cout << max_cnt << endl;
}
return 0;
}
G. ABBC or BACB
思路:观察B的位置,B前后连续的A都可以变成硬币,分情况处理
#include<bits/stdc++.h>
#define endl '\n'
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e5+10, M = 1001;
const int INF = 0x3f3f3f3f, mod = 998244353;
int T, n, m, k;
int gcd(int a, int b){
if(b) while((a %= b) && (b %= a));
return a + b;
}
int main(){
fast();
cin >> T;
while(T --){
string s; cin >> s;
n = s.size();
vector<int> cnt;
int sum = 0;
for(int i = 0; i <n; ){
int j = i;
while(j < n && s[j] == 'A') j ++;
cnt.push_back(j - i);
if(j == n) break;
i = j;
while(j < n && s[j] == 'B') j ++;
sum += j - i;
i = j;
}
int ans = 0;
sort(cnt.begin(), cnt.end());
for(auto i : cnt)
ans += i;
if(cnt.size() > sum) ans -= cnt.front();
cout << ans << endl;
}
return 0;
}
/*
_ooOoo_
o8888888o
88" . "88
(| ^_^ |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑AC,永无bug缠身
*/