这场比赛A掉l两道题,速度还是太慢,接近两个小时的时候才A掉了B题。感觉1200~1400的题写的差不多了,再写三星期就做1500到1700的题。
A. Déjà Vu
题意:往字符串中插入字符啊,使其不为回文字符串。无法完成此操作的只有一种可能,就是字符串全为a组成的时候。
构造一个非回文字符串,遍历查找到一个不是a的位置,如果此位置离头部距离为x,那我们只要在距尾部为x的距离插入a
就能构造出非回文字符串。我手模了几组,位置在后面一半和前面一半的不一样。
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
int t;
cin >> t;
while (t -- )
{
cin >> s;
int l = s.size() - 1;
int i;
for (i = 0; i <= l; i ++ )
{
if (s[i] != 'a') break;
}
if (i > l)
{
cout << "NO" << endl;
continue;
}
cout << "YES" << endl;
int ed;
ed = l - i;
if (ed <= l / 2)
{
for (int i = 0; i <= l; i ++ )
{
if (i == ed) cout << 'a';
cout << s[i];
}
}
else
{
for (int i = 0; i <= l; i ++ )
{
cout << s[i];
if (i == ed) cout << 'a';
}
}
puts("");
}
return 0;
}
但是赛后我乱交了几发,不分类也能过,难道是数据太弱了。
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
int t;
cin >> t;
while (t -- )
{
cin >> s;
int l = s.size() - 1;
int i;
for (i = 0; i <= l; i ++ )
{
if (s[i] != 'a') break;
}
if (i > l)
{
cout << "NO" << endl;
continue;
}
cout << "YES" << endl;
int ed;
ed = l - i;
for (int i = 0; i <= l; i ++ )
{
cout << s[i];
if (i == ed) cout << 'a';
}
puts("");
}
return 0;
}
看一下大佬优美简短的代码。a只会有插尾部和头部两种情况,真没想到。
#include <bits/stdc++.h>
using namespace std;
bool palindrome(const string &s) {
int n = s.length();
for(int i = 0; i < n; i++) {
if(s[i] != s[n - i - 1]) return false;
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int te;
cin >> te;
while(te--) {
string s;
cin >> s;
if(!palindrome(s + 'a')) {
cout << "YES\n" << s << 'a' << "\n";
}else if(!palindrome('a' + s)) {
cout << "YES\n" << 'a' << s << '\n';
}else {
cout << "NO\n";
}
}
}
B. Flip the Bits
题意:你可以将a字符串中0,1数量相等的前缀进行一个翻转,你需要判断能否翻转为b。
思路:遍历,存储0,1数量相等的位置,每个区间和b数组的相等或翻转一次和b数组中的区间相等。
注意要额外判断尾部,我因为漏掉了尾部wal两发。我是代币
#include <bits/stdc++.h>
using namespace std;
const int N = 300010;
int pos[N];
int main()
{
int t, n;
string a, b;
cin >> t;
while (t -- )
{
cin >> n >> a >> b;
int idx = 0;
int a1 = 0, a2 = 0;
memset(pos, 0, sizeof pos);
pos[0] = -1;
for (int i = 0; i < n; i ++ )
{
if (a[i] == '1') a1 ++;
if (a[i] == '0') a2 ++;
if (a1 == a2) pos[++ idx] = i;
}
// 判断能否成功
bool f = 1;
// 特判尾部
for (int i = pos[idx] + 1; i < n; i ++ )
{
if (a[i] != b[i])
{
f = 0;
break;
}
}
if (!f)
{
cout << "NO" << endl;
f = ~f;
continue;
}
for (int j = idx; j > 0; j -- )
{
int st = pos[j - 1] + 1, ed = pos[j];
for (int i = st; i < ed; i ++ )
{
if ((a[i] - '0' + b[i] - '0') % 2 != (a[i + 1] - '0' + b[i + 1] - '0') % 2)
{
f = 0;
break;
}
}
if (!f) break;
}
if (f) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
C. Balance the Bits
题意;构造出合法的a, b两个括号字符串。给出一个01字符串。0代表a,b括号不同,1反之。
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
char a[N], b[N], s[N];
int t, n;
int main()
{
cin >> t;
while (t -- )
{
cin >> n >> s + 1;
// 是否合法
int cnt = 0;
for (int i = 1; i <= n; i ++ )
if (s[i] == '1' ) cnt ++;
if (s[1] == '0' || s[n] == '0' || cnt & 1)
{
puts("NO");
continue;
}
else puts("YES");
// 构造两个序列
int cnt1 = 0, cnt2 = 0;
a[1] = '(', a[n] = ')';
b[1] = '(', b[n] = ')';
for (int i = 1; i <= n; i ++ )
{
if (s[i] == '1' && i != 1 && i != n)
{
cnt1 ++;
if (cnt1 & 1) a[i] = '(', b[i] = '(';
else a[i] = ')', b[i] = ')';
}
else if (s[i] == '0')
{
cnt2 ++;
if (cnt2 & 1) a[i] = '(', b[i] = ')';
else a[i] = ')', b[i] = '(';
}
}
for (int i = 1; i <= n; i ++ ) putchar(a[i]);
puts("");
for (int i = 1; i <= n; i ++ ) putchar(b[i]);
puts("");
}
return 0;
}
D题明天再补
来补D题了
D. 3-Coloring
1 2 1 2 1
2 1 2 1 2
1 2 1 2 1
2 1 2 1 2
1 2 1 2 1
如果禁1,在2的位置填2,如果位置填满,在1的位置填3
禁2,在1的位置填1,如果填满,在2的位置填3
禁3,1, 2位置空的随便挑1个
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
int n;
int a, b;
stack<PII> s1, s2;
int main()
{
cin >> n;
while (!s1.empty()) s1.pop();
while (!s2.empty()) s2.pop();
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
{
if (i & 1)
{
if (j & 1) s1.push({i, j});
else s2.push({i, j});
}
else
{
if (j & 1) s2.push({i, j});
else s1.push({i, j});
}
}
int t = n * n;
while (t -- )
{
cin >> a;
if (a == 1)
{
if (!s2.empty())
{
cout << "2 " << s2.top().first << ' ' << s2.top().second << endl;
s2.pop();
}
else
{
cout << "3 " << s1.top().first << ' ' << s1.top().second << endl;
s1.pop();
}
}
else if (a == 2)
{
if (!s1.empty())
{
cout << "1 " << s1.top().first << ' ' << s1.top().second << endl;
s1.pop();
}
else
{
cout << "3 " << s2.top().first << ' ' << s2.top().second << endl;
s2.pop();
}
}
else
{
if (!s1.empty())
{
cout << "1 " << s1.top().first << ' ' << s1.top().second << endl;
s1.pop();
}
else
{
cout << "2 " << s2.top().first << ' ' << s2.top().second << endl;
s2.pop();
}
}
}
return 0;
}
E. Travelling Salesman Problem
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PLL;
const int N = 100010;
int n;
vector<PLL> a;
int main()
{
cin >> n;
ll x, y;
for (int i = 0; i < n; i ++ )
{
scanf("%lld%lld", &x, &y);
a.push_back({x, y});
}
sort(a.begin(), a.end());
ll mx = a[0].first + a[0].second;
ll ans = a[0].second;
for (int i = 1; i < n; i ++ )
{
ans += a[i].second;
ans += max(0LL, a[i].first - mx);
mx = max(mx, a[i].first + a[i].second);
}
printf("%lld\n", ans);
return 0;
}