zoukankan      html  css  js  c++  java
  • 洛谷P2062 分队问题(dp)

    题意

    题目链接

    给定n个选手,将他们分成若干只队伍。其中第i个选手要求自己所属的队伍的人数大等于a[i]人。

    在满足所有选手的要求的前提下,最大化队伍的总数。

    注:每个选手属于且仅属于一支队伍。

    Sol

    直接dp,$f[i]$表示到第$i$个人最多分成几组

    很显然,一定是从上一个能放的位置转移而来

    // luogu-judger-enable-o2
    // luogu-judger-enable-o2
    #include<cstdio>
    #include<algorithm>
    #define LL long long 
    using namespace std;
    const int MAXN = 1e6 + 10;
    inline int read() {
        char c = getchar(); int x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    int N;
    int a[MAXN], f[MAXN];
    int main() {
        N = read();
        for(int i = 1; i <= N; i++) a[i] = read();
        sort(a + 1, a + N + 1);
        int res = N, ans = 0;
        for(int i = 1; i <= N; i++) {
            if(a[i] <= i) f[i] = 1;
            if((i - a[i] >= 1))
                f[i] = max(f[i], f[i - a[i]] + 1);
            ans = max(ans, f[i]);
        }
        printf("%d", ans);
        return 0;
    }
    /*
    4
    2 3 3 3
    */
  • 相关阅读:
    SpringCloud Gateway使用实例
    Nacos服务注册与发现
    HashMap源码分析——put方法
    Volatile关键字——内存可见性
    Java的JIT编译器
    why spring?
    mysql 锁
    sql server 表变量和临时表
    mysql 存储过程
    mysql 截取字符串
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/9637342.html
Copyright © 2011-2022 走看看