Educational Codeforces Round 101 (Rated for Div. 2)A-D题解

A. Regular Bracket Sequence

题目链接:点击此处

题目给你问号和一对左右括号,看能否成功匹配。

这边给了一般情况的答案,即不限于左右括号的数目的情况的代码。主要是先将问号全变为左括号,之后从右向左将括号变为右括号。

#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<stdio.h>
#include<map>
using namespace std;
#define check(x) cout<<"x:"<<x<<" "
typedef long long ll;
char ch[1005];
int main() {
    int t;
    cin >> t;
    while (t--) {
        cin >> ch;
        int cnt = 0;
        int len = strlen(ch);
        for (int i = 0;i < len;i++) {
            if (ch[i] == '(' || ch[i] == '?')cnt++;
            else cnt--;
        }
        if (cnt < 0||cnt%2) { cout << "NO" << endl;continue; }
        for (int i = len - 1;i >= 0;i--) {
            if (cnt == 0)break;
            if (ch[i] == '?') {
                ch[i] = ')';
                cnt -= 2;
                if (cnt == 0)break;
            }
        }
        bool f = true;
        for (int i = 0;i < len;i++) {
            if (ch[i] == '(' || ch[i] == '?')cnt++;
            else cnt--;
            if (cnt < 0) {
                f = false; break;
            }   
        }
        if (f)cout << "YES" << endl;
        else cout << "NO" << endl;
    }
        
}
    

B. Red and Blue

题目链接:点击此处

求两数组前缀和最大的和。很好理解。


#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<stdio.h>
#include<map>
#include<queue>
using namespace std;
#define Check(x) cout<<"x:"<<x<<" "
#define Min(x,y,z) min(x,min(z,y))
#define Max(x,y,z) max(x,max(z,y))
typedef long long ll;
const int MAXN = 1e2 + 5;
const int MAXM = 1e6 + 5;
const ll mod = 1e9 + 7;
ll n, m;
int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int a[MAXN];
        for (int i = 1;i <= n;i++)cin >> a[i];
        int maxn = 0,s = 0;
        for (int i = 1;i <= n;i++) {
            s += a[i];
            maxn = max(s, maxn); 
        }
        int m;
        cin >> m;
        int b[MAXN];
        int maxn2 = 0,s2=0;
        for (int i = 1;i <= m;i++)cin >> b[i];
        for (int i = 1;i <= m;i++) {
            s2 += b[i];
            maxn2 = max(maxn2, s2);
        }
        cout << maxn + maxn2 << endl;
    }

}

C. Building a Fence

题目链接:点击此处

k k k为栅栏高度, a r r [ i ] arr[i] arr[i]表示土地高度。

我们先列出题目的3个条件:

1、两个栅栏之间至少有1长度的连接
2、两边的栅栏要连接土地
3、栅栏最高能高出土地 k − 1 k-1 k1,最低要等于土地高度 a r r [ i ] arr[i] arr[i]

我们令 l o w [ i − 1 ] , h i g h [ i − 1 ] low[i-1],high[i-1] low[i1],high[i1]表示上一个状态的栅栏的下底在满足前面所有的条件后最低和最高能到的高度。

那么这一个状态的low值怎么转移过来呢?首先根据条件1,low[i]可以到达low[i-1]-k+1的高度,根据条件3, l o w [ i ] low[i] low[i]可以达到 a r r [ i ] arr[i] arr[i],所以 l o w [ i ] = m a x ( l o w [ i − 1 ] − k + 1 , a r r [ i ] ) low[i]=max(low[i-1]-k+1,arr[i]) low[i]=max(low[i1]k+1,arr[i])。至于为何max,读者自己想一下就可以知道。

h i g h [ i ] high[i] high[i]值根据条件1可以转移为 h i g h [ i − 1 ] + t − 1 high[i-1]+t-1 high[i1]+t1,根据条件3可以转移为 a r r [ i ] + t − 1 arr[i]+t-1 arr[i]+t1,所以 h i g h [ i ] = m i n ( h i g h [ i − 1 ] + t − 1 , a r r [ i ] + t − 1 ) high[i]=min(high[i-1]+t-1,arr[i]+t-1) high[i]=min(high[i1]+t1,arr[i]+t1)

得到这两个后,我们就判断这个 a r r [ i ] arr[i] arr[i]和他们的关系,是否符合上述条件。


#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<stdio.h>
#include<map>
#include<queue>
using namespace std;
#define Check(x) cout<<"x:"<<x<<" "
#define Min(x,y,z) min(x,min(z,y))
#define Max(x,y,z) max(x,max(z,y))
typedef long long ll;
const int MAXN = 2e5 + 5;
const int MAXM = 1e6 + 5;
const ll mod = 1e9 + 7;
ll n, m;
int arr[MAXN], high[MAXN], low[MAXN];
int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, t;
        cin >> n >> t;
        for (int i = 1;i <= n;i++) {
            cin >> arr[i];
        }
        high[1] = low[1] = arr[1];
        bool f = true;
        for (int i = 2;i <= n-1;i++) {
            high[i] = min(high[i - 1] + t - 1, arr[i] + t - 1);
            low[i] = max(low[i - 1] + 1 - t, arr[i]);
            if (arr[i] > high[i] || arr[i] < low[i]-t+1) { f = false;break; }
        }
        high[n] = min(high[n - 1] + t - 1, arr[n] + t - 1);
        low[n] = max(low[n - 1] + 1 - t, arr[n]);
        if (arr[n]<low[n] || arr[n]>high[n])f = false;//最后的要特判,因为必须接触地面
        if (f) { cout << "YES" << endl; }
        else cout << "NO" << endl;
    }

}

D. Ceil Divisions

题目链接:点击此处

这题意思就是给你一个 1 1 1 ~ n n n的数组,每次选择2个位置 x , y x,y x,y 1 ⩽ x , y ⩽ n    ( x ≠ y ) 1\leqslant x,y\leqslant n~~(x\ne y) 1x,yn  (x=y),使得数组中的 x x x位置的 a x a_x ax元素变为 ⌈ a x a y ⌉ \lceil\frac{a_x}{a_y} \rceil ayax,最多操作 n + 5 n+5 n+5次使得数组中只有1个2,其余都是1

刚开始的思路肯定很简单,就是二倍的往下,比如给你一个数组
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 1,2,3,4,5,6,7,8,9,10,11,12,13 1,2,3,4,5,6,7,8,9,10,11,12,13,先设 a = ⌈ 13 2 ⌉ = 7 a=\lceil\frac{13}{2}\rceil=7 a=213=7,然后让 [ 8 , 12 ] [8,12] [8,12]的数都与13除,使得 [ 8 , 12 ] [8,12] [8,12]的数都为1,然后令 ⌈ 13 7 ⌉ = 2 \lceil\frac{13}{7}\rceil=2 713=2,再 ⌈ 2 7 ⌉ = 1 \lceil\frac{2}{7}\rceil = 1 72=1,使得13的位置变为1,这很明显就是节省了很多步骤,但是仔细想,我每次除2,在商的那个位置,我要操作2次,才能变为1,其余的都操作1次,最后的结果就是 n + log ⁡ 2 n n+\log_{2}n n+log2n,很明显不符合题意。

我们继续往下想,我们每次除2,得到 n + log ⁡ 2 n n+\log_{2}n n+log2n,那我们每次多除一点,假如除 t t t,那么我们得到的便是 O ( n + log ⁡ t n ) O(n+\log_{t}n) O(n+logtn),但事实不是这样, t t t如果很大,在一个位置为 k k k的数 a k a_k ak ⌈ a k t ⌉ = 1 \lceil\frac{a_k}{t}\rceil=1 tak=1时,就转为了 O ( k ) O(k) O(k)的次数,所以 t t t作为常数,我们找不到(即我们不能每次除固定的数),所以我们就继续想,要比2多,但是不是常数,要随着我们现在处理的 a k a_k ak大小有关,不断尝试,最后会得到 log ⁡ 2 a k , a k \log_{2}a_k,\sqrt{a_k} log2ak,ak 等数,然后验算,发现 a k \sqrt{a_k} ak 适合。

下面是代码:

#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<stdio.h>
#include<map>
using namespace std;
#define check(x) cout<<"x:"<<x<<" "
typedef long long ll;
int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int a = n;
        int cnt = 0;
        int pre = n;
        while (a!=2) {
            int b = ceil(sqrt(a*1.0));
            cnt += 2;
            cnt += pre - b - 1;
            pre = b;
            a = b;
        }
        cout << cnt << endl;
        a = n;
        pre = n;
        while (a != 2) {
            int b = ceil(sqrt(a * 1.0));
            for (int i = b + 1;i <= a - 1;i++) {
                cout << i << " " << a << endl;
            }
            cout << a << " " << b << endl;
            cout << a << " " << b << endl;
            a = b;
        }
    }
}
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值