zoukankan      html  css  js  c++  java
  • JXOJ 9.7 NOIP 放松模拟赛 总结

    比赛链接

    T1 数数

    题意:有a个红球,b个黄球,c个蓝球,d个绿球排成一列,求任意相邻不同色的排列的数目

    ​ 1 <= a , b, c, d <= 30 答案对1e9 + 7 取膜

    用的类似数位dp的方法记忆化搜索,复杂度O(a4),我的方法可能常数有点大,但还是挺易懂的。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define ll long long
    ll mol;
    ll f[121][31][31][31][5];
    int a, b, c, d;
    ll dfs(int x, int red, int yellow, int blue, int last)
    {
      //  printf("x = %d red = %d yellow = %d blue = %d last = %d green = %d
    ", x, red, yellow, blue, last, x - 1 - red - yellow - blue);
        if(x == (a + b + c + d))
        {
            if(red != a) if(last != 1) return 1; else return 0;
            if(yellow != b) if(last != 2) return 1; else return 0;
            if(blue != c) if(last != 3) return 1; else return 0;
            if((x - 1 - red - yellow - blue) != d) if(last != 4) return 1; else return 0;
            return 0;
        }
        if(f[x][red][yellow][blue][last] != -1) return f[x][red][yellow][blue][last];
        ll ans = 0;
        for(int i = 1; i <= 4; i++)
        {
            if(i == 1 && red == a)continue;
            if(i == 2 && yellow == b)continue;
            if(i == 3 && blue == c)continue;
            if(i == 4 && (x - 1 - red - yellow - blue) == d)continue;
            if(i != last)ans =(ans + dfs(x + 1, red + ((i == 1) ? 1 : 0) , yellow + ((i == 2) ? 1 : 0) , blue + ((i == 3) ? 1 : 0) , i)) % mol;
        }
        f[x][red][yellow][blue][last] = ans % mol;
        return ans % mol;
    }
    int main()
    {
        mol = 1e9 + 7;
        scanf("%d%d%d%d", &a, &b, &c, &d);
        memset(f, -1, sizeof(f));
        printf("%lld
    " , dfs(1, 0, 0, 0, 0));
        return 0;
    }
    
    

    T2 数组

    题意:有一个大小为n的数组a[],初始值为0,可以分m次让数组的某一位加1,求能生成多少种排列使得恰好有k个位置是奇数。

    1 <= n, m <= 1e5 0 <= k <= n 答案对1e9 + 7 取膜

    这道题的做法比较巧妙,我们需要将问题通过转化,变成一个简单的模型

    首先我们将m-k,问题就转化为了把m-k分成n个偶数,每个偶数可以为0的问题

    然后,因为偶数除以2可以为奇数,可以为偶数,我们再将(m-k)除以2,问题就转化为了将(m-k)/2分成n个正整数,每个整数可以为0的问题,用挡板法加排列组合可以解决,但由于涉及到除数取膜,需要用乘法逆元。

    #include <iostream>
    #include <cstdio>
    using namespace std;
    #define ll long long
    ll mol;
    int n, m, k;
    ll quickpow(ll a, int b)
    {
        ll ret = 1;
        while(b != 0)
        {
            if(b&1) ret = ret * a % mol;
            b >>= 1;
            a = a * a % mol;
        }
        return ret % mol;
    }
    ll C(int x, int y)
    {
        ll ret = 1;
        for(ll i = x; i >= x - y + 1; i--)
        {
            ret = ret * i % mol;
        }
        for(ll i = 1; i <= y; i++)
        {
            ret = ret * quickpow(i, mol - 2) % mol;
        }
        return ret;
    }
    int main()
    {
        mol = 1e9 + 7;
        scanf("%d%d%d", &n, &m, &k);
        if((m - k) % 2 == 1)
        {
            printf("0
    ");
            return 0;
        }
        printf("%lld
    ", C(n , k) * C((m - k) / 2 + n - 1, n - 1) % mol);
        return 0;
    }
    
    
    

    T3 子集

    题意:有一个集合,里面有n个正整数。这个集合有2n个子集,求这个集合的第k小子集

    1 <= n <= 35

    首先,我们通过观察发现,对于40%的数据,我们直接暴力搜索就可以解决问题,但总范围刚好是我们搜索范围的两倍,对于这一类问题,我们可以采用一种叫做meet-in-the-middle的算法,类似于广搜优化技巧中的双向广搜。

    我们每次只搜索总范围的一半,这样时间和空间就不会出现问题,然后对每次搜索出来的数进行排序,使数据有序,然后利用数据的有序,采用二分,用两个指针分别在两个数组中查找即可。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    #define ll long long
    int n;
    ll k;
    int r1, r2;
    ll a[3][1 << 20], b[50], c1[1 << 20], c2[1 << 20];
    int head[3], tail[3];
    ll cnt[3];
    void dfs(int x, ll num, int k)
    {
      //  printf("x = %d num = %lld k = %d
    ", x, num, k);
        if(x == tail[k] - head[k] + 1)
        {
            a[k][++cnt[k]] = num;
            return;
        }
        dfs(x + 1, num + b[x + head[k] - 1 + 1], k);
        dfs(x + 1, num, k);
    }
    int check(ll x)
    {
        ll p1 = 1;
        while(c1[p1 + 1] + c2[1] <= x && p1 + 1 <= r1)p1++;
        ll cnt = 0;
        for(int i = 1; i <= r2; i++)
        {
            while(c1[p1] + c2[i] > x && p1 >= 0) p1--;
            if(p1 < 0)break;
            cnt += (p1);
        }
       // printf("cnt = %lld
    ", cnt);
        if(cnt >= k)return 1; else return 0;
    }
    int main()
    {
        scanf("%d%lld", &n, &k);
        for(int i = 1; i <= n; i++)scanf("%lld", &b[i]);
        head[1] = 1; tail[1] = n / 2; r1 = tail[1];
        head[2] = n / 2 + 1; tail[2] = n; r2 = tail[2] - head[2] + 1;
        dfs(0, 0, 1);dfs(0, 0, 2);
        r1 = cnt[1]; r2 = cnt[2];
        for(int i = 1; i <= r1; i++)c1[i] = a[1][i];
        for(int i = 1; i <= r2; i++)c2[i] = a[2][i];
        sort(c1 + 1, c1 + r1 + 1);
        sort(c2 + 1, c2 + r2 + 1);
      //  for(int i = 1; i <= r1; i++) printf("%d ", c1[i]); printf("
    ");
       // for(int i = 1; i <= r2; i++) printf("%d ", c2[i]); printf("
    ");
       // printf("r1 = %d r2 = %d
    ", r1, r2);
        ll l = 1, r = 35, ans = 0; r *= 1e9;
        while(l <= r)
        {
         //   printf("l = %lld r = %lld
    ", l, r);
            ll mid = (l + r) >> 1;
            if(check(mid) == 1)
            {
                ans = mid; r = mid - 1;
            }
            else l = mid + 1;
        }
        printf("%lld
    ", ans);
        return 0;
    }
    
  • 相关阅读:
    目标检测算法综述
    深度相机原理揭秘--双目立体视觉
    UnderScore.jsAPI记录
    JS基础一
    Angular.js学习范例及笔记
    AngularJS应用,常用数组知识点
    框架开发之——AngularJS+MVC+Routing开发步骤总结——5.14
    Node.JS开发环境准备
    常用的Oracle函数收集
    程序员的修炼之道——从小工到专家
  • 原文地址:https://www.cnblogs.com/Akaina/p/11526624.html
Copyright © 2011-2022 走看看