A. Three Doors
There are three doors in front of you, numbered from 11 to 33 from left to right. Each door has a lock on it, which can only be opened with a key with the same number on it as the number on the door.
There are three keys — one for each door. Two of them are hidden behind the doors, so that there is no more than one key behind each door. So two doors have one key behind them, one door doesn't have a key behind it. To obtain a key hidden behind a door, you should first unlock that door. The remaining key is in your hands.
Can you open all the doors?
答
做一个小深搜?
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mo = 1e3;
const int N = 1e5 + 5;
void solve()
{
int x, a[5];
cin >> x;
cin >> a[1] >> a[2] >> a[3];
int k = x;
int cnt = 0, c = 0;
bool b[4];
b[1]=b[2]=b[3]=0;
while (k && c <= 4)
{
if (b[k] == 0)
{
cnt++;
b[k] = 1;
}
k = a[k];
c++;
}
if (cnt == 3)cout << "YES\n";
else cout << "NO\n";
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
B. Also Try Minecraft
You are beta testing the new secret Terraria update. This update will add quests to the game!
Simply, the world map can be represented as an array of length nn, where the ii-th column of the world has height aiai.
There are mm quests you have to test. The jj-th of them is represented by two integers sjsj and tjtj. In this quest, you have to go from the column sjsj to the column tjtj. At the start of the quest, you are appearing at the column sjsj.
In one move, you can go from the column xx to the column x−1x−1 or to the column x+1x+1. In this version, you have Spectre Boots, which allow you to fly. Since it is a beta version, they are bugged, so they only allow you to fly when you are going up and have infinite fly duration. When you are moving from the column with the height pp to the column with the height qq, then you get some amount of fall damage. If the height pp is greater than the height qq, you get p−qp−q fall damage, otherwise you fly up and get 00 damage.
For each of the given quests, determine the minimum amount of fall damage you can get during this quest.
答
从前扫一遍前缀和,再从后做一遍前缀和
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mo = 1e3;
const int N = 1e5 + 5;
ll sum[N], lsum[N];
ll a[N], n;
void solve()
{
int l, r;
cin >> l >> r;
if (l < r)cout << sum[r] - sum[l] << '\n';
else cout << lsum[r] - lsum[l] << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> n;
cin >> t;
cin >> a[1];
sum[0] = sum[1] = 0;
for (int i = 2; i <= n; i++)
{
cin >> a[i];
sum[i] = sum[i - 1] + max(0ll, a[i - 1] - a[i]);
}
for (int i = n - 1; i >= 1; i--)
{
lsum[i] = lsum[i + 1] + max(0ll, a[i + 1] - a[i]);
//cout << lsum[i] << " ";
}
while (t--)
{
solve();
}
return 0;
}
C. Recover an RBS
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example:
- bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)");
- bracket sequences ")(", "(" and ")" are not.
There was an RBS. Some brackets have been replaced with question marks. Is it true that there is a unique way to replace question marks with brackets, so that the resulting sequence is an RBS?
答
第一步,看看问号?数量和缺少的“)”或“(”是否一样,一样就只有一种选择,不用再考虑。
第二步,填写。先把右括号填满,当仅需要1个右括号时,先填写一个左括号,然后填右括号,剩下的都是右括号,因为如果可以交换(有其他可能),首先会交换最中间的两个问号的左右括号。
为什么要换中间两个:因为题目给的数据是必然有解的,如果对于问号处,先填写左括号(,再填写右括号),必然是答案的解。我们要找第二组解,肯定是交换中间的两个问号?产生的影响最小,如果有多组解,如?(??)?,看中间两个交换能否成立。
如果改中间两个?不能成立,那就不可能有多组解了。
#include<bits/stdc++.h>
using namespace std;
void solve()
{
string s = "";
cin >> s;
int n = s.size();
int cnt = 0, cnt1 = 0, cnt2 = 0, ct = 0;
for (int i = 0; i < n; i++)
{
if (s[i] == '?')
{
cnt++;
}
if (s[i] == '(')
{
cnt1++;
}
if (s[i] == ')')
{
cnt1--;
}
}
if (cnt == abs(cnt1))
{
cout << "YES\n";
return;
}
int cntpos = (cnt - cnt1) / 2; //需要的(数量
// cout << cntpos << endl;
bool flag = false;
for (int i = 0; i < n; i++)
{
if (s[i] == '?')
{
if (cntpos > 1)
{
s[i] = '(';
cntpos--;
}
else if (cntpos == 1)
{
if (flag)
{
s[i] = '(';
cntpos--;
}
else
{
s[i] = ')';
flag = true;
}
}
else
{
s[i] = ')';
}
}
}
cnt2=0;
for (int i = 0; i < n; i++)
{
if (s[i] == '(')
cnt2++;
else
cnt2--;
if (cnt2 < 0)
{
cout << "YES\n";
return;
}
}
cout << "NO\n";
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
方法二
int main() {
#ifdef local
freopen("1.in", "r", stdin);
#endif
T = read();
while (T--) {
scanf("%s", c + 1);
n = strlen(c + 1);
int cnt[2] = { 0, 0 };
len = 0;
for (int i = 1; i <= n; i++) {
if (c[i] == '(') ++cnt[0];
if (c[i] == ')') ++cnt[1];
if (c[i] == '?') seq[++len] = i;
}
if (cnt[0] == n / 2 || cnt[1] == n / 2) {
printf("YES\n");
continue;
}
for (int i = 1; i <= n / 2 - cnt[0]; i++) c[seq[i]] = '(';
for (int i = n / 2 - cnt[0] + 1; i <= len; i++) c[seq[i]] = ')';
swap(c[seq[n / 2 - cnt[0]]], c[seq[n / 2 - cnt[0] + 1]]);
int sum = 0, ans = 0;
rep(i, 1, n) {
sum += (c[i] == '(' ? 1 : -1);
if (sum < 0) {
ans = 1;
break;
}
}
printf("%s\n", ans ? "YES" : "NO");
}