Codeforces Round 923 (Div. 3)(A-E)

文章介绍了五种编程题目,涉及贪心策略在字符串操作中的应用,如查找B字符,遵循给定字符串,选择不同元素,以及特殊构造问题。还讨论了使用哈希表、滑动窗口等数据结构来解决这些问题。
摘要由CSDN通过智能技术生成

A:Make it White

贪心策略,找最左边的B和最右边的,计算长度

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

#define int long long
typedef pair<int,int>pii;
#define N 300100
int a[N],b[N];
void solve(){
     int n;
     cin>>n;
     string s;
     cin>>s;
     int l=0x3f3f3f3f,r=0;
     for(int i=0;i<n;i++){
     	if(s[i]=='B') l=min(l,i),r=max(r,i);
	 }
	 cout<<r-l+1<<endl;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while(t--){
        solve();
    }
}

B:Following the String

在二十六个字母中选,如果出现次数等于数组值,则输出直接暴力枚举即可

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

#define int long long
typedef pair<int,int>pii;
#define N 300000
int a[N],b[N];
void solve(){
    int n;
    cin>>n;
    string s;
    int cnt=0;
    vector<int>m(30,0);
    for(int i=0;i<n;i++){
    	cin>>a[i];
	}
	int r=0;
    for(int i=0;i<n;i++){
       if(a[i]==0) {
       	   cout<<(char)('a'+r);
       	    m[r]++;
       	   r++;
       	  
       }
	   else{
	   	  for(int j=0;j<26;j++ ){
	   	      if(m[j]==a[i]){
	   	        cout<<(char)('a'+j);
	   	         m[j]++;
	   	        break;
			 } 	
		  }
	   } 
      
       
	}
	cout<<endl;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while(t--){
        solve();
    }
}

C:Choose the Different Ones!

求是能构成序列,用两个hash表记录如果两个中没出现,输出no,然后求出两个数组中独有的

记录,如果都小于等于k/2,则可以,否则不行

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

#define int long long
typedef pair<int,int>pii;
#define N 300100
int a[N],b[N];
void solve(){
    int n,m,k;
    cin>>n>>m>>k;
    map<int,int>ma,mb;
    for(int i=0;i<n;i++) cin>>a[i],ma[a[i]]++;
    for(int i=0;i<m;i++) cin>>b[i],mb[b[i]]++;
    bool flag=false;
    for(int i=1;i<=k;i++) {
    	if(ma[i]==0&&mb[i]==0) {
    		flag=true;
    		break;
		}
	}
	if(flag) cout<<"NO"<<endl;
	else{
		int cnt1=0,cnt2=0,res=0;
		for(int i=1;i<=k;i++){
		      if(ma[i]&&!mb[i]) cnt1++;
		      else if(!ma[i]&&mb[i]) cnt2++;
		      else res++;
		}
		if(cnt1<=k/2&&cnt2<=k/2) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while(t--){
        solve();
    }
}

D:Find the Different Ones!

询问不同元素数量,可以考虑,用一个数组去记录这个元素左边不同的元素是哪一个,如果相等就一直右移,大概是滑窗类似的思想,时间复杂度o(n),预处理完,

输入l,r如果b[r]<l不在区间内,输出-1,否则输出b[r]r

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

#define int long long
typedef pair<int,int>pii;
#define N 300000
int a[N],b[N];
void solve(){
    int n;
    cin>>n;
    
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++){
    	int j=i;
    	while(j+1<=n&&a[j+1]==a[i]) j++;
    	for(int k=i;k<=j;k++) b[k]=i-1;
    	i=j;
	}
    int q;
    cin>>q;
    while(q--){
    	int x,y;
    	cin>>x>>y;
    	if(b[y]<x) cout<<-1<<" "<<-1<<endl;
    	else cout<<b[y]<<" "<<y<<endl;
	}
	cout<<endl;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while(t--){
        solve();
    }
}

E:Microcycle

构造题,仔细观察最后一个样例

7 4

1 4 5 7

2 3 6

发现分为k组,每次奇数组正向加,偶数逆向加即可

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

#define int long long
typedef pair<int,int>pii;
#define N 300100
int a[N],b[N];
void solve(){
    int n,k;
    cin>>n>>k;
    int cur=0;
    for(int i=1;i<=k;i++){
    	if(i%2){
    		for(int j=i;j<=n;j+=k) a[j]=++cur;
		}
		else{
			for(int j=n-(n-i)%k;j>=1;j-=k) a[j]=++cur;
		}
	}
	for(int i=1;i<=n;i++) cout<<a[i]<<' ';
	cout<<endl;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while(t--){
        solve();
    }
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多年以后

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值