Codeforces Round 910(Div.2)(A-D)

文章讲述了在Codeforces竞赛中解决两个问题的解题思路,分别是通过遍历调整字符序列以达到特定目标(A.MilicaandString)和使用贪心算法最小化数组调整次数使其有序(B.MilenaandAdmirer)。

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值