A. Two distinct points
解题思路:只要考虑左右端点即可,在简单讨论一下。代码如下
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
using namespace std;
int main() {
std::ios::sync_with_stdio(0);
int q;
cin >> q;
while (q--) {
int l1, r1, l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
if (l1 == r2)
cout << r1 << " " << l2 << endl;
else
cout << l1 << " " << r2 << endl;
}
return 0;
}
B. Divisors of Two Integers
解题思路:因子中最大的mx1肯定是其中一个数,然后在能被mx1整除且数量>1或者不能被mx1整除中在找最大的数mx2
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const int maxn=130;
ll n,mx1=-inf,mx2=-inf,tp;
map<ll,ll> m;
int main(){
std::ios::sync_with_stdio(0);
cin>>n;
for(int i=1;i<=n;++i){
cin>>tp;
m[tp]++;
mx1=max(tp,mx1);
}
for(auto it=m.begin();it!=m.end();++it){
ll num=it->first,cnt=it->second;
if((cnt==1 && mx1%num!=0) || cnt>1){
mx2=max(num,mx2);
}
}
cout<<mx1<<" "<<mx2<<endl;
return 0;
}
C. Nice Garland
解题思路:相同的字符距离要是3的倍数,可得只要前三个字符固定了之后,后面就必须如此排列,不然不可能满足条件,例如RGB那后面必须是R没有选择,所以只需要"RGB","RBG","BGR","BRG","GBR","GRB"都讨论一下。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
using namespace std;
string tmp[]= {"RGB","RBG","BGR","BRG","GBR","GRB"};
int ans[10];
int main() {
std::ios::sync_with_stdio(0);
int n;
cin>>n;
string s,res;
cin>>s;
map<char,int> vis;
int cnt = 0;
if(n<3) {
if(n==1) res = s;
else {
if(s[0]==s[1]) {
if(s[0]=='R') s[1] = 'B';
if(s[0]=='G') s[1] = 'R';
if(s[0]=='B') s[1] = 'G';
res = s;
cnt = 1;
}
}
cout<<cnt<<endl;
cout<<res<<endl;
return 0;
}
string t;
t = s;
int mn = inf;
for(int k=0; k<6; k++) {
if(t[0]!=tmp[k][0]) {
t[0] = tmp[k][0];
ans[k]++;
}
if(t[1]!=tmp[k][1]) {
t[1] = tmp[k][1];
ans[k]++;
}
if(t[2]!=tmp[k][2]) {
t[2] = tmp[k][2];
ans[k]++;
}
for(int i=0; i<s.size()-3; i++) {
if(t[i+3]!=t[i]) {
ans[k]++;
t[i+3] = t[i];
}
}
if(ans[k]<mn) {
mn = ans[k];
res = t;
}
t = s;
}
cout<<mn<<endl;
cout<<res<<endl;
return 0;
}
D. Diverse Garland
解题思路:把C改成了相邻的不能相同,先处理连续3个相同的,再处理连续两个相同的,代码还是比较繁琐的,
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
const ll inf=0x3f3f3f3f;
const int maxn=2e5+50;
int n;
char st[maxn];
int main() {
scanf("%d%s",&n,st);
int ans=0;
for(int i=1;i<n-1;i++) {
if(st[i]==st[i-1]&&st[i]==st[i+1]) {
if(st[i]=='G') st[i]='B',++ans;
else if(st[i]=='B') st[i]='R',++ans;
else if(st[i]=='R') st[i]='G',++ans;
}
}
for(int i=1;i<n-1;i++) {
if(st[i]==st[i-1]) {
if(st[i-1]=='G'&&st[i+1]=='B') st[i]='R',++ans;
else if(st[i-1]=='G'&&st[i+1]=='R') st[i]='B',++ans;
else if(st[i-1]=='B'&&st[i+1]=='G') st[i]='R',++ans;
else if(st[i-1]=='B'&&st[i+1]=='R') st[i]='G',++ans;
else if(st[i-1]=='R'&&st[i+1]=='G') st[i]='B',++ans;
else if(st[i-1]=='R'&&st[i+1]=='B') st[i]='G',++ans;
}
}
if(st[n-1]==st[n-2]) {
if(st[n-2]=='G') st[n-1]='B',++ans;
else if(st[n-2]=='B') st[n-1]='G',++ans;
else if(st[n-2]=='R') st[n-1]='G',++ans;
}
printf("%d\n%s",ans,st);
return 0;
}