Codeforces Round #736 (Div. 2)部分题解

A

点击此处查看对应的题目.
本题涉及算法:数论知识
  本题考察的是质数的性质,质数是只能被自身和1整除的数。所以必然不能是偶数那么将质数p mod 2 == 1,而另一个数则是p mod p-1 ==1。

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

#include<bits/stdc++.h>
using namespace std;

void solve()
{
    int p;
    cin >> p;

    cout << 2 <<" "<< p -1 <<'\n';
}
int main()
{
    int t;
    cin>>t;

    while(t--){
        solve();
    }
    return 0;
}

B

点击此处查看对应的题目.
本题涉及算法1:贪心
  本题要求求出最大能留在第一行的人数,所以我们应该贪心的想依次枚举左,上,右三种情况,也就是尽量靠左站以保证站的人数最多。

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

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
bool st[N];

void solve()
{
    int n;
    cin >> n;
    string a,b;
    cin >> a >> b;
    for(int i = 0;i <= n;i ++) st[i] = false;

    int res = 0;
    for(int i = 0;i < n;i ++ ){
        if(i - 1 >= 0 && !st[i - 1] && a[i - 1] == '1' && b[i] == '1'){
            res ++;
            st[i - 1] = true;
        }else if(a[i] == '0' && !st[i] && b[i] == '1'){
            res ++;
            st[i] = true;
        }else if(i + 1 <= n - 1 && !st[i + 1] && a[i + 1] == '1' && b[i] == '1'){
            res ++;
            st[i + 1] = true;
        }
    }

    cout << res <<'\n';
}
int main()
{
    int t;
    cin >> t;
    while(t --){
        solve();
    }
    return 0;
}

C

点击此处查看对应的题目.
本题涉及算法:贪心
  本题要维关键就是每次添加和删除我们只需要较强的那个贵族,较弱的那个贵族是不需要的.。
  我的第一反应是用map来存较弱的那个贵族的出现次数,然后再遍历得到最后剩下的那个强者贵族。但这样会TLE ~~QAQ
  所以我做了优化,每次出现一个较弱者就将从cnt++,这样求能取得一样的效果,然后用n - cnt即可。

时间复杂度 O ( q + m ) O(q + m) O(q+m)

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> st(N,0);
int n,m,q,cnt;

void add(int a,int b)
{
    if(st[a] == 0 && a < b) cnt ++;
    else if(st[b] == 0 && a > b) cnt ++;

    if (a < b) st[a] ++;
    else if(a > b) st[b] ++;
}
void remove_edge(int a,int b)
{
    if(st[a] - 1 == 0 && a < b) cnt --;
    else if(st[b] - 1 == 0 && a > b) cnt --;

    if(a < b) st[a] --;
    else if(a > b) st[b] --;
}
int main()
{
    cin >> n >>m;

    while(m -- ){
        int a,b;
        cin >> a >> b;
        add(a,b);
    }

    cin >> q;
    while(q -- ){
        int t;
        cin >> t;
        if(t == 1){
            int a,b;
            cin >> a >>b;
            add(a,b);
        }else if(t == 2){
            int a,b;
            cin >> a >> b;
            remove_edge(a,b);
        }else{
            cout << n - cnt <<'\n';
        }
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

marvel121

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

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

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

打赏作者

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

抵扣说明:

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

余额充值