zoukankan      html  css  js  c++  java
  • AtCoder Beginner Contest 165C

    前言

    怀疑昨天晚上考试的时候没带脑子。。。

    Solution

    这道题可以直接暴力 (QwQ)。(我是来证一波时间复杂度的

    我们注意到一个很重要的条件 (1<=A[1]<=A[2]<=...<=a[n]<=m)

    由于并不是严格小于,我们数列的个数并非 (C(m,n))。我们可以将此转化为在长度为 (n) 的序列上划分成 (m) 个段,不过段内可以为空。所以数列的个数为 (C(n+m-1,m-1))

    所以总复杂度为 (O(C(n+m-1,m-1)*(n+q)))(心虚

    Code

    #include <cstdio>
    #include <iostream>
    using namespace std;
    
    int n, m, q, a[55], b[55], c[55], d[55], A[15], ans;
    
    int read() {
        int x = 0, f = 1; char s;
        while((s = getchar()) < '0' || s > '9') if(s == '-') f = -1;
        while(s >= '0' && s <= '9') {x = (x << 1) + (x << 3) + (s ^ 48); s = getchar();}
        return x * f;
    }
    
    void dfs(const int now, const int lim) {
        if(now == n + 1) {
            int res = 0;
            for(int i = 1; i <= q; ++ i)
                if(A[b[i]] - A[a[i]] == c[i]) res += d[i];
            ans = max(ans, res);
            return;
        }
        for(int i = lim; i <= m; ++ i)
            A[now] = i, dfs(now + 1, i);
    }
    
    int main() {
        n = read(), m = read(), q = read();
        for(int i = 1; i <= q; ++ i) a[i] = read(), b[i] = read(), c[i] = read(), d[i] = read();
        dfs(1, 1);
        printf("%d
    ", ans);
        return 0;
    }
    
  • 相关阅读:
    [LeetCode] 771. Jewels and Stones
    [LeetCode] 129. Sum Root to Leaf Numbers
    java定时器demo
    Spring Boot与监控管理
    springboot与热部署
    springboot中的web项目不能访问templates中的静态资源
    @Component 和 @Bean 的区别
    springcluoud入门
    Dubbo和Zookerper的关系
    Spring boot配置Dubbo三种方式
  • 原文地址:https://www.cnblogs.com/AWhiteWall/p/12821209.html
Copyright © 2011-2022 走看看