C2. Exam in BerSU (hard version)

https://codeforces.com/contest/1185/problem/C2

题意:给你n个数字,从左往右有序,问对于每个数字来说,从左往右(到这个数字)取一些数字,如果取得数字一定要包括这个数字,且这些数字的累加和不能超过m,求对于每个数字来说,它最少落下几个数字没取。

思路:题目给的每个数字范围都是1~100,这个是突破口,我们可以开一个数组来记录每个数字出现的次数,那么对于每个数字只需要从左往右扫一遍这个数组,将较小的数字先取了,直到不能取为止。

#include<bits/stdc++.h>
using namespace std;
int book[105];
int main()
{

    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)
    {
        int t;
        scanf("%d",&t);
        int ans=0;
        int man=0;
        int sum=m-t;
        for(int j=1; j<=100; j++)
        {
            int flag=min(sum/j,book[j]);
            man+=flag;
            sum-=flag*j;
        }
        book[t]++;
        printf("%d ",i-1-man);
    }

}

转载于:https://www.cnblogs.com/dongdong25800/p/11063729.html

猜你喜欢

转载自blog.csdn.net/weixin_34259559/article/details/93691487
今日推荐