牛客小白17

算法解析与实现:区间操作、扫雷、异或和、图遍历与计数
这篇博客详细介绍了多种算法的思路和C++实现,包括区间合并问题的优化、扫雷游戏的暴力解法、求前缀异或和的直接遍历、区间求和的基础莫队算法以及图的奇环判断。还探讨了在不同场景下如何应用这些算法,并提供了相应的代码示例。

题目链接

小sun的假期

思路

区间合并

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define dbg(a)  cout<<#a<<" : "<<a<<endl;
#define IOS std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define PAUSE   system("pause")
#define sd(a)       scanf("%d",&a)
#define sll(a)      scanf("%intd",&a)
#define sdd(a,b)    scanf("%d%d",&a,&b)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf(a)       scanf("%lf",&a)
#define sff(a,b)    scanf("%lf%lf",&a,&b)
using namespace std;
typedef pair<int,int> PII;

void merge(vector<PII> &segs)
{
    vector<PII> res;

    sort(segs.begin(), segs.end());

    int st = -2e9, ed = -2e9;
    for (auto seg : segs)
        if (ed < seg.first)
        {
            if (st != -2e9)
                res.push_back({st, ed});
            st = seg.first, ed = seg.second;
        }
        else ed = max(ed, seg.second); 

    if (st != -2e9) res.push_back({st, ed});

    segs = res;
}

int main() {
    int n, m;
    sdd(n, m);
    vector<PII> segs;
    for(int i = 0; i < m; i ++) {
        int l, r;
        sdd(l, r);
        segs.push_back({l, r});
    }
    
    merge(segs);
    
    int st = 1;
    int ans = -1;
    for(auto it:segs) {
        ans = max(it.first - st, ans);
        st = it.second;
    }
    ans = max(ans, n - st);
    cout << ans << endl;
    
    return 0;
}

扫雷

思路

暴力

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define dbg(a)  cout<<#a<<" : "<<a<<endl;
#define IOS std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define PAUSE   system("pause")
#define sd(a)       scanf("%d",&a)
#define sll(a)      scanf("%intd",&a)
#define sdd(a,b)    scanf("%d%d",&a,&b)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf(a)       scanf("%lf",&a)
#define sff(a,b)    scanf("%lf%lf",&a,&b)
#define rep(i, a, b) for(int i = a; i <= b; i ++)

using namespace std;
const int ddx[] = { -1, -1, -1, 0, 0, 1, 1, 1 }, ddy[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
string str[1010];

int main() {
    int n, m;
    sdd(n, m);
    for(int i = 0; i < n; i ++)
        cin >> str[i];
    rep(i, 0, n - 1){
        rep(j, 0, m - 1) {
            int cnt = 0;
            for(int a = i - 1; a <= i + 1; a ++)
                for(int b = j - 1; b <= j + 1; b ++) {
                    if(a < 0 || a >= n || b < 0 || b >= m) continue;
                    if(str[a][b] == '*') cnt ++;
                }
            if(str[i][j] == '*') cout << '*';
            else cout << cnt;
        }
        
        cout << endl;
    }
    
    return 0;
}

C异或和

思路

直接遍历一遍求前缀异或和即可
因为出现偶数的情况会消掉(我吐了)

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define dbg(a)  cout<<#a<<" : "<<a<<endl;
#define IOS std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define PAUSE   system("pause")
#define sd(a)       scanf("%d",&a)
#define sll(a)      scanf("%intd",&a)
#define sdd(a,b)    scanf("%d%d",&a,&b)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf(a)       scanf("%lf",&a)
#define sff(a,b)    scanf("%lf%lf",&a,&b)
#define rep(i, a, b) for(int i = a; i <= b; i ++)

using namespace std;

int main() {
    int n;
    sd(n);
    int res = 0;
    rep(i, 0, n - 1) {
        int x;
        sd(x);
        res ^= x;
    }
    
    printf("%d\n", res);
    
    return 0;
}

区间求和

思路

基础莫队

每个数字的某一个位置权值为x * cnt[x], 总共有cnt[x]个,所以每个数字的权值为 cnt[x]*cnt[x]*x

代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

typedef long long LL;

const int N = 100010, M = 100010, S = 100010;

int n, m, len;
LL w[N], ans[M];
LL cnt[S];
LL ans1;

struct Query
{
    int id, l, r;
}q[M];


int get(int x)
{
    return x / len;
}

bool cmp(const Query& a, const Query& b)
{
    int i = get(a.l), j = get(b.l);
    if (i != j) return i < j;
    return a.r < b.r;
}

void add(int x, int v)
{
    ans1 -= (LL)cnt[x] * cnt[x] * x;
    cnt[x] += v;
    ans1 += (LL)cnt[x] * cnt[x] * x;
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);
    
    len = max(1, (int)sqrt((double)n * n / m));
    for (int i = 0; i < m; i ++ )
    {
        int l, r;
        scanf("%d%d", &l, &r);
        q[i] = {i, l, r};
    }
    
    sort(q, q + m, cmp);
    
    for (int k = 0, i = 0, j = 1; k < m; k ++ )
    {
        int id = q[k].id, l = q[k].l, r = q[k].r;
        while (i < r) add(w[ ++ i], 1);
        while (i > r) add(w[i -- ], -1);
        while (j < l) add(w[j ++ ], -1);
        while (j > l) add(w[ -- j], 1);
        ans[id] = ans1;
    }

    for (int i = 0; i < m; i ++ ) printf("%lld\n", ans[i]);
    return 0;
}

图的遍历

思路

很明显就是判断奇环,如果有奇环,那么从奇环的任何一个点开始,都能到达奇环的所有点,所以我们对每个联通块来考虑即可。

如果某个联通块有奇环,那么我们只需要把所有的联通块连到一起即可,所以答案就是联通块个数减一。
如果不存在奇环,那么我们需要把奇数个联通块连到一起,(相当于一个连通块看成一个点,连奇数环即可,)再连向其他的偶联通块即可。所以答案就是联通块的个数。

代码

注意:不能和之前模板一样,直接return,必须再一个连通块遍历所有点

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define dbg(a)  cout<<#a<<" : "<<a<<endl;
#define IOS std::ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define PAUSE   system("pause")
#define sd(a)       scanf("%d",&a)
#define sll(a)      scanf("%intd",&a)
#define sdd(a,b)    scanf("%d%d",&a,&b)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf(a)       scanf("%lf",&a)
#define sff(a,b)    scanf("%lf%lf",&a,&b)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
using namespace std;

const int N = 1e5 + 10, M = 2 * N;

int h[N], e[M], ne[M], w[M], idx;
int color[N];
int cnt; // 连通块的数量
bool flag;

void add(int a, int b){  
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

void dfs(int u, int c){ //将u点染成c ,c = 0未染, c = 1染第一种, c = 2染第二种
    color[u] = c;

    for(int i = h[u]; i != -1; i = ne[i]){
        int j = e[i];
        if(!color[j]){
            dfs(j, 3 - c);
        }
        else if(color[j] == c) flag = true;
    }
}

int main() {
    memset(h, -1, sizeof h);
    int n, m;
    sdd(n, m);
    while(m --) {
        int a, b;
        sdd(a, b);
        add(a, b);
        add(b, a);
    }
    cnt = 0;
    rep(i, 1, n) {
        if(!color[i]) {
            cnt ++; // 连通块
            dfs(i, 1);
        }
    }
    
    if(flag) cnt --;
    
    printf("%d\n", cnt);
    
    return 0;
}

计数

思路

隔板法
盒子不允许空的情况:将20个相同的小球放入3个不同的盒子的 方法为 C 20 − 1 3 − 1 C_{20-1}^{3-1} C20131 (隔板法)即 C n − 1 m − 1 C_{n-1}^{m-1} Cn1m1
盒子允许空的情况: 映射即可。 C n + m − 1 m − 1 C_{n+m-1}^{m-1} Cn+m1m1

此题将连续的0的个数即丢失的数当成 小球的个数,而可选的数字当成不同的盒子即可。

代码

#include<iostream>
#include<cstring>
#include<algorithm>
#define sd(x) scanf("%d", &x)
#define rep(i, a, b) for(int i = a; i <= b; i ++)

using namespace std;
typedef long long LL;


const int N = 1000010 + 1050;
const int mod = 1000000007;

int fact[N], infact[N];

LL qmi(int a, int b, int p){
    LL res = 1;
    while(b){
        if(b & 1) res = (LL)res * a % p;
        a = (LL)a * a % p;
        b >>=  1;
    }
    return res;
}

LL C(int a, int b) {
   	return (LL)fact[a] * infact[a - b] % mod * infact[b] % mod;
}


void init(){
    fact[0] = infact[0] = 1;
    for(int i = 1; i < N; i ++){
        fact[i] = (LL)fact[i - 1] * i % mod;
        infact[i] = (LL)infact[i - 1] * qmi(i, mod - 2, mod) % mod;
    }
    return;
}

int main() {
    int n;
    init();
    LL ans = 1;
    int cnt = 0;
    sd(n);
    int last = 1000;
    rep(i, 1, n) {
        int x;
        sd(x);
        if(x == 0) cnt ++;
        else {
            int len = last - x + 1;
            ans = (LL)ans * C(len + cnt - 1, len - 1) % mod;
            last = x;
            cnt = 0;
        }
    }
    
    if(cnt) ans = (LL)ans * C(last - 1 + cnt, last - 1) % mod;
    printf("%lld\n", ans);
    
    return 0;
}
11-08
是一个专注于IT领域的在线学习和求职平台。它为广大IT从业者和学习者提供了丰富的资源和多样化的功能。 从引用中可知,有练习赛和算法题目相关内容。练习赛会提供不同类型的算法题目,如练习赛123包含了“A.炸鸡块哥哥的粉丝题”和“B.智乃想考一道鸽巢原理”等题目,用户可以通过编写代码来解决这些题目,以此锻炼自己的算法能力和编程技巧。示例代码使用了C++语言解决“A.炸鸡块哥哥的粉丝题”: ```cpp #include<bits/stdc++.h> using namespace std; int n; string s; int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>n>>s; for(int i=0;i<(n+1)/2;i++) cout<<s[i]; return 0; } ``` 也有算法题的在线评测系统,用户提交代码后,系统会根据预先设定的测试用例对代码进行评测,判断代码是否能正确解决问题。 在功能方面,提供了丰富的算法题库,涵盖了各种难度和类型的题目,帮助用户提升编程和算法能力。它还提供不同语言的支持,例如引用中的C++和Java,方便不同语言背景的用户使用。此外,还有求职相关的板块,为求职者提供企业招聘信息、面经、笔试题等,帮助他们更好地准备求职。 使用方法上,用户首先需要注册并登录账号。之后可以在练习赛板块参与比赛,或者在题库中选择自己感兴趣的题目进行练习。对于算法题目,用户可以在代码编辑区域编写代码,选择合适的编程语言,然后提交代码进行评测。以解决“每日温度”问题的Java代码为例: ```java import java.util.*; /** * NC208 每日温度 * @author d3y1 */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 每日温度 * @param dailyTemperatures int整型一维数组 * @return int整型一维数组 */ public int[] temperatures (int[] dailyTemperatures) { int n = dailyTemperatures.length; int[] results = new int[n]; Stack<Integer> stack = new Stack<>(); // Deque<Integer> stack = new ArrayDeque<>(); // Deque<Integer> stack = new LinkedList<>(); for(int i=n-1; i>=0; i--){ // 单调栈 单调减(从右向左遍历) while(!stack.isEmpty() && dailyTemperatures[stack.peek()]<=dailyTemperatures[i]){ stack.pop(); } // if(stack.isEmpty()){ // results[i] = 0; // }else{ // results[i] = stack.peek()-i; // } results[i] = stack.isEmpty() ? 0 : stack.peek()-i; stack.push(i); } return results; } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值