Educational Codeforces Round 101 (Rated for Div. 2)
题目链接:https://codeforces.com/contest/1469
A.Regular Bracket Sequence
题目大意:给你含有一个( 和一个 ),其他全是 ?的字符串,判断该字符串是否合法
题目思路:特判:①如果开头 )结尾( ,不合法②如果 ? 的个数是奇数,不合法
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <stack>
#include <deque>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
typedef pair<double, double> PDD;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int N = 110, M = 4 * N;
const int base = 1e9;
const int P = 131;
int n, m, t, k;
int main()
{
scanf("%d", &t);
while (t--)
{
string s;
cin >> s;
int len = s.size();
if (s[0] == ')' || s[len - 1] == '(')
printf("No\n");
else if (s.size() % 2 == 0)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
B.Red and Blue
题目大意:给你两个数组a,b,不改变元素的先后顺序,构造数组c,使数组c前缀和的最大值最大
题目思路:求数组a,b前缀的最大值,可能这些最大值也小于0
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <stack>
#include <deque>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
typedef pair<double, double> PDD;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int N = 110, M = 4 * N;
const int base = 1e9;
const int P = 131;
int n, m, t, k;
int main()
{
scanf("%d", &t);
while (t--)
{
int max1 = -INF, max2 = -INF;
int sum1 = 0, sum2 = 0;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) //求数组a前缀和最大值
{
int x;
scanf("%d", &x);
sum1 += x;
max1 = max(max1, sum1);
}
scanf("%d", &m);
for (int i = 1; i <= m; ++i) //求数组b前缀和最大值
{
int x;
scanf("%d", &x);
sum2 += x;
max2 = max(max2, sum2);
}
//可能max1或者max2小于0,需特判
printf("%d\n", max(0, max(max1 + max2, max(max1, max2))));
}
return 0;
}
C.Building a Fence
题目大意:给你一堆高度去建围栏,这些围栏必须有公共边,第一个和最后一个围栏必须挨着地面,其他围栏可以浮空,但不能超过k - 1
题目思路:依次处理每个围栏的区间,假设围栏所在区间最低为low,最高为high,则第一个围栏low = h[1], high = h[1] + k,第二个围栏low = max(low - k + 1, h[i]),因为每个区间需要有公共边,所以max里第二个值还需 + 1,high = min(high + k - 1, h[i] + 2 * k - 1),第一个区间最大值 + 围栏高度 - 1),因为围栏最多浮空k - 1,所以max里第一个值为地面高度 + k - 1 + k,因为每个区间需要有公共边,所以max里第二个值还需 - 1,以此类推。
如果在此期间,出现以下4种情况,则围栏构建失败:①区间最大值 - 围栏高度 < 地面高度,②区间最小值 > 地面高度 + k - 1,③区间最大值 < 区间最小值,④最后一个区间最小值 ≠ 最后一个地面高度
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <stack>
#include <deque>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
typedef pair<double, double> PDD;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10, M = 4 * N;
const int base = 1e9;
const int P = 131;
int n, m, t, k;
int h[N];
int main()
{
scanf("%d", &t);
while (t--)
{
int flag = 1;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i)
scanf("%d", &h[i]);
int low = h[1], high = h[1] + k; //围栏所处区间[low, high]
for (int i = 2; i <= n; ++i)
{
high = min(high + k - 1, h[i] + 2 * k - 1); //处理最大值
low = max(low - k + 1, h[i]); //处理最小值
if (high - k < h[i] || low > h[i] + k - 1 || high < low) //不符合条件的情况
{
flag = 0;
break;
}
}
if (low != h[n]) //如果最后不接地
flag = 0;
if (flag == 1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
D.Ceil Divisions
题目大意:给你至多n + 5步,通过以下操作将给定的数组处理成含有 n - 1 个 1 和 1 个 2 的数组
操作:取两个指标x,y 使 \(a_x = \left\lceil \frac{a_x}{a_y} \right\rceil\) \(\left\lceil x \right\rceil\)是向上取整
题目思路:以n为 2e5 为例,对 2e5 反复开根号操作得下图中的数,称为根号数
从图可知,将 2e5 变为 1 只需 2 步,即输出 200000 448 20000 448,对 448 同理,输出 448 22 448 22,从而最多只需 10 步就可将图中几个数变为 1(保留2)
首先遍历,将所有不是上图的数输出 i 2e5,保留上图中的数,实际上保留了 7 个数字(包括1),所以用了 n - 7 步,在加上上述的10步,可知 n 为 2e5 时最多需要 n + 3 步,其它都比 2e5 的情况小,假设根号数有 k 个,则 总步数为 (n - k - 1) + (2 *(k - 1)) = n + k - 3 步
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <stack>
#include <deque>
#include <queue>
#include <cmath>
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
typedef pair<double, double> PDD;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10, M = 4 * N;
const int base = 1e9;
const int P = 131;
int n, m, t, k;
map<int, int> mp;
int a[10];
int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
mp.clear();
int pos = 0;
k = n;
mp[n] = 1; //预处理根号数
a[++pos] = n;
while (k > 2)
{
double g = sqrt(k);
if (g - (int)g == 0) //如果能除尽则不需要向上取整
a[++pos] = g;
else
a[++pos] = g + 1;
mp[a[pos]] = 1;
k = a[pos];
}
printf("%d\n", n + pos - 3); //输出步数
for (int i = 2; i <= n; ++i)
if (mp[i] == 0)
printf("%d %d\n", i, n); //遍历输出i n
for (int i = 1; i < pos; ++i) //输出根号数
{
printf("%d %d\n", a[i], a[i + 1]);
printf("%d %d\n", a[i], a[i + 1]);
}
}
return 0;
}