Educational Codeforces Round 125 (Rated for Div. 2) A - C

A. Integer Moves

题意:给你一个坐标 ( x , y ) (x, y) (x,y) ,问你从 ( 0 , 0 ) (0, 0) (0,0) 开始,每次走任意整数长的步,问最少需要几步可以到达

做法:特判 ( 0 , 0 ) (0, 0) (0,0),如果可以一步到位就是1,否则是2

#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
const int mod = 1e9 + 7;

int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int x, y;
        cin >> x >> y;
        int s = sqrt(x * x + y * y);
        if (x == 0 && y == 0) cout << 0 << '\n';
        else if (s * s == x * x + y * y) cout << 1 << '\n';
        else cout << 2 << '\n';
    }
    return 0;
}

B. XY Sequence

题意:让你构造一个序列,每个元素的值不能大于 B B B 且前后需要满足关系,问你该序列最大的和

做法:从0开始,如果元素小于 B B B ,则加上 x x x ,反之减去 y y y 即可

#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
const int mod = 1e9 + 7;

int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n, B, x, y;
        cin >> n >> B >> x >> y;
        vector<LL> a(n + 1);
        a[0] = 0;
        for(int i = 1; i <= n; i ++)
        {
            if(a[i - 1] + x <= B) a[i] = a[i - 1] + x;
            else a[i] = a[i-1] - y;
        }
        cout << accumulate(a.begin(), a.end(), 0ll) <<'\n';
    }
    return 0;
}

C. Bracket Sequence Deletion

看到括号序列就可以联想到栈,利用栈模拟下即可

#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
const int mod = 1e9 + 7;

void solve()
{
    int n;
    string s;
    stack<char> stk;
    cin >> n >> s;
    int res = 0, ans = 0;
    bool flag = true;
    for (int i = 0; i < n; i++)
    {
        if (stk.empty()) stk.push(s[i]);
        else
        {
            if (stk.top() == '(') res++, stk.pop(), ans += 2;
            else
            {
                for (int j = i; j < n; j++)
                {
                    if (s[j] == ')')
                    {
                        res ++, ans += j - i + 2;
                        while (!stk.empty()) stk.pop();
                        i = j;
                        break;
                    }
                }
            }
        }
    }
    cout << res << " " << n - ans << '\n';
}

int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--) solve();
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值