A.遍历一遍计算B的个数,如果要+B就修改A,遍历到A就cnt++,直到cnt==k,如果要-B就修改B,遍历到B就cnt--,同理到cnt==k;
// Problem: A. Milica and String
// Contest: Codeforces - Codeforces Round 910 (Div. 2)
// URL: https://codeforces.com/contest/1898/problem/0
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <iostream>
#include <cstring>
#include <iomanip>
#include <ctime>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
typedef unsigned int ul;
typedef unsigned long long ull;
const int N = 2e6 + 9;
int a[N];
void solve() {
int n,k;
cin>>n>>k;
string s;
cin>>s;
int cnt=0;
for(int i=0;i<n;i++){
if(s[i]=='B'){
cnt++;
}
}
if(cnt==k){
cout<<0<<'\n';
}else if(cnt>k){
for(int i=0;i<n;i++){
if(s[i]=='B'){
cnt--;
if(cnt==k){
cout<<1<<'\n';
cout<<i+1<<" "<<'A'<<'\n';
}
}
}
}else{
for(int i=0;i<n;i++){
if(s[i]=='A'){
cnt++;
if(cnt==k){
cout<<1<<'\n';
cout<<i+1<<" "<<'B'<<'\n';
}
}
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int q;
cin >> q;
while (q--) {
solve();
}
return 0;
}
#############################################################################
以上是比赛时ac的
坐牢了1小时,发现b,c,d都是思维题
#############################################################################补题
B.贪心,求最少的修改次数使得数组顺序为升序或者相等+升序,相等。如果从前面开始遍历,会破坏前面的顺序(升序或者相等+升序,相等状态),因此我们从后往前遍历,如果a[i]>a[i+1],我们就要修改,如果我们修改成很小的数字,还需要把前面再修改,为了修改最少次,我们就应该修改接近于a[i+1],就有更大的概率不用再把前面再修改了(贪心思想)。因此修改次数应该是cnt=a[i]/a[i+1]-1,(通过例子推出结论,如果要分成4个a[i+1],1个修改成2(1次操作)2个修改成4个(2次操作)总和是3次操作),(【8,2】可以把8修改成【2,2,2,2,2】即可,分裂成4个2,操作3次)。又因为我们要保证分出来的都小于a[i+1],所以cnt需要向上取整(【7,2】要修改成【1,2,2,2】才能满足题意)。(ceil()有精度问题,一般用(a[i]+a[i+1]-1/a[i+1])).最后记得开long long ;
// Problem: B. Milena and Admirer
// Contest: Codeforces - Codeforces Round 910 (Div. 2)
// URL: https://codeforces.com/contest/1898/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <iostream>
#include <cstring>
#include <iomanip>
#include <ctime>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
typedef unsigned int ul;
typedef unsigned long long ull;
const int N = 2e6 + 9;
ll a[N];
void solve() {
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
ll ans=0;
ll temp=0;
ll cnt=0;
for(int i=n-1;i>=1;i--){
if(a[i]>a[i+1]){
cnt=(a[i+1]+a[i]-1)/a[i+1];
temp=cnt-1;
ans+=temp;
a[i]/=cnt;
}
}
cout<<ans<<'\n';
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int q;
cin >> q;
while (q--) {
solve();
}
return 0;
}
文章讲述了在Codeforces竞赛中解决两个问题的解题思路,分别是通过遍历调整字符序列以达到特定目标(A.MilicaandString)和使用贪心算法最小化数组调整次数使其有序(B.MilenaandAdmirer)。
566

被折叠的 条评论
为什么被折叠?



