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
題解:首先數組中最大的一個數肯定是一個答案,我們將其當做,那麼我們將的因子全都刪去,數組中剩下的最大的肯定就是了。
#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
題解:我們通過觀察就可以發現,最終的排列只會是這樣三個三個的排列,因此我們只需要暴力枚舉的全排列答案取即可。
#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
題解:對於樣例我們觀察即可知道,只有遇到前後兩個字母相同的時候才需要更換字母,爲了更換次數最少,因此我們需要考慮更換的字母是否和後一個是否相同,枚舉一下即可。
#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)
題解:因爲只有區間減一的操作,首先我們可以確定如果想要差值最大,那麼肯定就是讓儘可能多的區間覆蓋值最小的數,這樣可以最大化差值,因此我們可以枚舉,找出區間覆蓋最小值最多的並計算出答案。
#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;
}