A. Two distinct points
题解:特判判断一下两个区间的左右关系即可。直接输出边界。
#include<bits/stdc++.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
int q;
cin >> q;
int l1,l2,r1,r2;
while(q--) {
cin >> l1 >> r1 >> l2 >> r2;
int a,b;
if(l1 == r2) {
l1++;
}
cout << l1 << ' ' << r2 << endl;
}
return 0;
}
B. Divisors of Two Integers
题解:首先数组中最大的一个数肯定是一个答案,我们将其当做
x
x
x,那么我们将
x
x
x的因子全都删去,数组中剩下的最大的肯定就是
y
y
y了。
#include<bits/stdc++.h>
using namespace std;
int d[200];
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
int n;
cin >> n;
for(int i = 0; i < n; ++i) {
cin >> d[i];
}
sort(d,d + n);
// int t = 1, k = 1;
long long x = *max_element(d,d+n);
for(int i = 1; i <= x; ++i) {
if(x % i == 0) {
for(int j = 0; j < n; ++j) {
if(i == d[j]) {
d[j] = -1;
break;
}
}
}
}
long long y = 1;
int t1 = *max_element(d,d+n);
//1 1 2 2 4 4 5 8 10 20
//1 2 4 8
//1 2 4 5 10 20
cout << x << ' ' <<t1 << endl;
return 0;
}
C. Nice Garland
题解:我们通过观察就可以发现,最终的排列只会是
a
b
c
a
b
c
a
b
c
.
.
.
abcabcabc...
abcabcabc...这样三个三个的排列,因此我们只需要暴力枚举
R
B
G
RBG
RBG的全排列答案取
m
i
n
min
min即可。
#include<bits/stdc++.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
int n;
cin >> n;
string s;
cin >> s;
//RBRRGG
int min1 = 1e9;
string ans = s, t = s;
string v[6] = {"RGB","RBG","BRG","BGR","GRB","GBR"};
int k = 0;
while(k < 6)
{
int cnt = 0;
s = ans;
for(int i = 0; i < n; ++i) {
if(s[i] != v[k][i % 3]) {
cnt++;
s[i] = v[k][i % 3];
}
}
if(min1 > cnt) {
min1 = cnt;
t = s;
}
//cout<<v[k]<<endl;
k++;
}
cout<<min1<<endl;
cout<<t<<endl;
return 0;
}
D. Diverse Garland
题解:对于样例
R
B
G
R
R
B
R
G
G
RBGRRBRGG
RBGRRBRGG我们观察即可知道,只有遇到前后两个字母相同的时候才需要更换字母,为了更换次数最少,因此我们需要考虑更换的字母是否和后一个是否相同,枚举一下即可。
#include<bits/stdc++.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
int n;
cin >> n;
string s;
cin >> s;
//RBRRGG
//
string ans = s, t = s;
//RBGRGRRGG
//RBGRBRBR
string tt = "RGB";
int cnt = 0;
for(int i = 0; i < n - 1; ++i) {
if(s[i] == s[i + 1]) {
for(int j = 0; j < 3; ++j) {
if(i + 2 < n && s[i + 2] != tt[j] && s[i + 1] != tt[j]) {
s[i + 1] = tt[j];
cnt++;
break;
}
}
}
if(i == n - 2) {
if(s[i] == s[i + 1]) {
for(int j = 0; j < 3; ++j) {
if(s[i] != tt[j]) {
s[i + 1] = tt[j];
cnt++;
break;
}
}
}
}
}
cout<<cnt<<endl;
cout<<s<<endl;
return 0;
}
E1. Array and Segments (Easy version)
题解:因为只有区间减一的操作,首先我们可以确定如果想要差值最大,那么肯定就是让尽可能多的区间覆盖值最小的数,这样可以最大化差值,因此我们可以
O
(
n
2
)
O(n^2)
O(n2)枚举
a
[
i
]
−
a
[
j
]
a[i] - a[j]
a[i]−a[j],找出区间覆盖最小值最多的并计算出答案。
#include<bits/stdc++.h>
using namespace std;
int a[301],l[301],r[301];
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
int n,m;
cin >> n >> m;
for(int i = 0; i < n; ++i) {
cin >> a[i];
}
for(int i = 0; i < m; ++i) {
cin >> l[i] >> r[i];
l[i]--, r[i]--;
}
int ans = 0;
vector<int> v;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(i == j) continue;
int dif = a[j] - a[i], cnt = 0;
int tmp[301] = {0};
for(int k = 0; k < m; ++k) {
if(l[k] <= i && r[k] >= i && (l[k] > j || r[k] < j)) {
dif++;
tmp[cnt++] = k + 1;
}
}
if(dif > ans) {
ans = dif;
v.clear();
for(int k = 0; k < cnt; ++k) {
v.push_back(tmp[k]);
}
}
}
}
cout << ans << endl << v.size() << endl;
for(int i = 0; i < v.size(); ++i) {
printf("%d%c",v[i], i == v.size() - 1 ?'\n' :' ');
}
return 0;
}