Educational Codeforces Round 125 (Div. 2) 部分题解

本篇题解只是记录我的做题过程代码,不提供最优解
(另外这里所有的时间复杂度都只分析单个样例,不算 t t t的时间复杂度)

A

点击此处查看对应的题目.
本题设计算法:模拟
本题特判一下 x 与 y 都为 0 的情况,是不需要操作的

再用sqrt判断一下是否可以将 s q r t ( x ∗ x + y ∗ y ) sqrt(x * x + y * y) sqrt(xx+yy)是否与其真正的平方数相等即可。

时间复杂度 O ( 1 ) O(1) O(1)

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 50;
map<double,double> m;

int isSquare(int res) 
{
    int x = sqrt(res);
    
    if (x * x == res) return true;
    else return false; 
}
int main()
{
    int t;
    cin >> t;
    while (t -- ) {
        int x,y;
        cin >> x >> y;
        if (!x && !y) cout << 0 << '\n';
        else if (isSquare(x * x + y * y)) cout << 1 << '\n';
        else cout << 2 << '\n';
    }
    //system("pause");
    return 0;
}

B

点击此处查看对应的题目.
本题设计算法:贪心

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

时间复杂度 O ( n ) O(n) O(n)

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll a[N];

void solve()
{
    ll n,b,x,y;
    cin >> n >> b >> x >> y;

    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;
        }
    }

    ll sum = 0;
    for (int i = 1;i <= n;i ++ ) sum += a[i];
    cout << sum << '\n';
}
int main()
{
    int t;
    cin >> t;
    while (t -- ){
        solve();
    }
    //system("pause");
    return 0;
}


C

点击此处查看对应的题目.
本题设计算法:模拟
用ne数组记录其右括号下标,以便于后面右括号快速找到合法括号,而回文括号,只要任意前面有一个左括号就可以触发一个回文串

时间复杂度 O ( n ) O(n) O(n)

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5e5 + 10;
int ne[N];

void solve()
{
    int n;
    string s;
    cin >> n >> s;
    int pre = 0;
 	//预处理ne数组
    for (int i = n - 1; i >= 0; --i) {
        if (s[i] == ')') {
            ne[i] = pre;
            pre = i;
        }
    }
    int c = 0, i = 0;
    for (; i < n; ++i) {
        if (s[i] == '(') {
            if (i == n - 1)break;
            ++i;
        } else {
            if (ne[i] == 0)break;
            i = ne[i];
        }
        ++c;
    }
    cout << c << ' ' << n - i << '\n';
}
int main()
{
    int t;
    cin >> t;
    while (t -- ) solve();
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

marvel121

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值