Codeforces Round 855

文章包含多个C++程序,分别解决不同的问题:检查字符串是否转化为小写去重后等于meow;计算字符对的数量;使用优先队列求解最大能量问题;以及考虑字符删除策略的问题。这些程序展示了字符串操作、哈希表和优先队列等数据结构在算法设计中的应用。
摘要由CSDN通过智能技术生成

A.Is it a Cat?

可以把字符串全转换为小写,去重再删除,判断是不是meow

#include <bits/stdc++.h>

using namespace std;

int n,l;
string s;
int main() {
	cin>>n;
	while (n--) {
		cin>>l>>s;
		for (int i = 0; i < l; i++) {
			s[i] = tolower(s[i]);
		}
		
	   s.erase(unique(s.begin(),s.end()) , s.end()) ;
	   
		printf("%s\n", s=="meow"?"YES":"NO");
	}
}

B.Count the number of pairs

#include<bits/stdc++.h>
using namespace std;

int main()
{
	ios :: sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	
	int T;
	cin>>T;
	
	while(T--)
	{
		int cnt = 0;
		int n,k;
		cin>>n>>k;
	    string ch;
	    cin>>ch;
	    
	    unordered_map<char,int> m;
		for(int i = 0 ; i < n; i++)
		{
		
			m[ch[i]]++;
		}
		
		for(int i = 0 ; i < 26 ; i++)
		{
			int h = min(m[i+'a'] , m[i+'A']);
			cnt += h;
		    
		    if(k > 0)
		    {
			cnt += min(k,abs(m[i+'a'] - m[i+'A'])/2);
			k -= min(k,abs(m[i+'a']-m[i+'A'])/2);
		    }	
		}
		
	cout<<cnt<<endl;
		
		
	}
	
	return 0;
}

C1. Powering the Hero (easy version)
题目意思是每次从堆顶拿出一张卡,如果是0则可以从能量堆中得到相应数值的能量,如果非0则放入能量堆中或discard?求获得的最大能量。可以用优先队列来维护。

#include<iostream>
#include<queue>
#include<algorithm>
#define ll long long

using namespace std;
int main()
{
	ios :: sync_with_stdio(false);
	cin.tie(0) , cout.tie(0);
	
	int T;
	cin>>T;
	
	while(T--)
	{
		int n ;
		cin>>n;
		ll ans = 0;
	    priority_queue<ll> q;
		
		for(int i = 0 ; i < n ; i++)
		{
		
			ll x;
			cin>>x;
			
			if(x != 0) q.push(x);
			else if(!q.empty())
			{
				ans += q.top();
				q.pop();			
			} 
		}
		
		cout<<ans<<endl;
		
	}
	return 0;
}

C2. Powering the Hero (hard version)
同上

#include<iostream>
#include<queue>
#include<algorithm>
#define ll long long

using namespace std;
int main()
{
	ios :: sync_with_stdio(false);
	cin.tie(0) , cout.tie(0);
	
	int T;
	cin>>T;
	
	while(T--)
	{
		int n ;
		cin>>n;
		ll ans = 0;
	    priority_queue<ll> q;
		
		for(int i = 0 ; i < n ; i++)
		{
		
			ll x;
			cin>>x;
			
			if(x != 0) q.push(x);
			else if(!q.empty())
			{
				ans += q.top();
				q.pop();			
			} 
		}
		
		cout<<ans<<endl;
		
	}
	return 0;
}

D. Remove Two Letters

一开始用哈希表怎么都不行。纯思维,如果一个字符串每个字符都不同的话那么答案就是字符个数-1.如果当前删除的这两个字符的前一个字符和接下来要删除的后一个字符一样的话,那么对答案的贡献度为0。

#include<bits/stdc++.h>

using namespace std;

void solve()
{
    
    int n;
    string  str;
    cin>>n;
    cin>>str;
    
    int ans = n - 1;
    
    for(int i = 1 ; i + 1 < n ; i++)
    {
        if(str[i-1] == str[i+1]) ans--;
        
    }
    
    cout<<ans<<endl;
}

int main()
{
    
    int T;
    cin>>T;
    
    while(T--)
    {
        solve();
    }
    
    return 0;
    
}
Codeforces Round 887是一个程序设计竞赛活动,由Codeforces组织举办。根据引用中的代码,该竞赛的题目要求解决一个序列操作的问题。给定一个长度为n的序列,通过执行一系列操作,使得序列变得非sorted,即非严格递增。具体操作是将序列中[1, i]范围内的数字全部加一,同时将[i+1, n]范围内的数字全部减一。问题要求求解最少需要执行多少次操作才能达到要求。 引用中的代码给出了解决这个问题的实现。代码首先读入序列的长度n和序列a。然后通过判断序列是否已经是非sorted,如果是则直接输出0。接下来,代码遍历序列,求出相邻两个数字的差的最小值。最后,计算出最少需要执行的操作次数,并输出结果。 需要注意的是,引用中的代码只是给出了解决问题的一种实现方式,并不代表Codeforces Round 887的具体题目和解答。要了解该竞赛的具体信息,需要参考Codeforces官方网站或相关资料。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Codeforces Round 887 (Div. 2)](https://blog.csdn.net/qq_36545889/article/details/131905067)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值