Deltix Round, Autumn 2021 (open for everyone, rated, Div. 1 + Div. 2) Problem B+Problem D
B. William the Vigilant
作为一个号称世界上最懒的作者,我只把重要的圈了出来
You are given a string s of length n only consisting of characters “a”, “b” and “c”. There are q queries of format (pos,c), meaning replacing the element of string s at position pos with character c. After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn’t contain string “abc” as a substring. A valid replacement of a character is replacing it with “a”, “b” or “c”.
意思就是:会给你一个数i还有一个字母c将i位置上的字母更改成c染后在输出最小的大小意思就是把这一串子母中连续的abc都取出(歪果仁说话字数好多)(切记每次更换后保留)
#include<bits/stdc++.h>
using namespace std;
int main(){
int t,n,ans=0,i;
string s;
cin>>n>>t;
cin>>s;//cin string
for(i=0;i<n-2;i++){//delet the substrings abc from string
if(s[i]=='a'&&s[i+1]=='b'&&s[i+2]=='c') ans++;
}
while(t--){
int sum=0,num=0,k=ans,now;
char c;
cin>>now>>c;
for(i=now-3;i<=now+1;i++){
if(s[i]=='a'&&s[i+1]=='b'&&s[i+2]=='c') sum++;//find the amount of strings that can be deleted before the changing
}
s[now-1]=c;//change
for(i=now-3;i<=now+1;i++){
if(s[i]=='a'&&s[i+1]=='b'&&s[i+2]=='c') num++;//find the amount of strings that can be deleted
}
cout<<k-sum+num<<endl;//k means the amount before
ans=k-sum+num;//remember k
}
return 0;
}
D. Social Network
本想更新C,但是发现自己是一个蒟蒻所以来写一下D吧,(并查集还是挺好的)
意思就是:有一个经济团体,然后朋友越多越好(一看就是并查集🐕U•ェ•*U)然后给出t个询问,表示第x个人必须要与第y个人成为朋友
//切记一定要加一个sort否则就可能吧所有小的都加上了,但是忘记大的。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
int n,t,f[1010],k[1010];
int get(int x){
if(x==f[x]) return x;
return f[x]=get(f[x]);
}
bool cmp(int x,int y){
return x>y;
}
int main(){
cin>>n>>t;
for(int i=1;i<=n;i++) {
f[i]=i;
k[i]=1;
}
int cnt=0;
while(t--){
int x,y;
cin>>x>>y;// numbers of people which must have a connection according to condition i.
int fx=get(x);
int fy=get(y);
if(fx==fy) cnt++;//meaning that they are already connected
else {
f[fy]=fx;//add an edge
k[fx]+=k[fy];//friends add up as well
}
vector<int> v;
//v[i] means that for person i he has v[i] friends
for(int i=1;i<=n;i++){
if(f[i]==i) v.push_back(k[i]);
}
sort(v.begin(),v.end(),cmp);
int ans=0;//the total number of friends
for(int i=0;i<=cnt&&i<v.size();i++) ans+=v[i];//it's sorted so that it will add together the biggest first
cout<<ans-1<<endl;//minus yourself
}
return 0;
}
求点赞,有问题问我哦!