牛客小白17

题目链接

小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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值