题目:8.14(补)+8.15 codeforces 1185 Exam in BerSU (1200+1700,贪心)

链接:https://codeforces.ml/problemset/problem/1185/C1
         https://codeforces.ml/problemset/problem/1185/C2

题意:

最多花费T的时间来完成整场考试,现有n个人来参加,每个人都需要一定时间来考试;输出每个人最少需要干掉(bushi)前面几个人才能保证自己完成考试;(意会即可)

题解:

因为每个人考试花费的时间不超过100,就用数组存起来,如果轮到自己时间超标了就从100开始遍历到0,时间复杂度最多n*100;

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[200005] = { 0 };
int b[200005] = { 0 };
int c[105] = { 0 };
int main()
{
	int n, t;
	cin >> n >> t;

	int sum = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		sum += a[i];
		if (sum <= t)
		{
			b[i] = 0;
		}
		else
		{
			int x = sum;
			int num = 0;
			for (int j = 100; j >= 0; j--)
			{
				if (c[j] != 0)
				{
					if (x - j * c[j] <= t)
					{
						int cnt = c[j];
						while (x - j * cnt <= t)
						{
							cnt--;
						}
						b[i] = cnt + num+1;
						break;
					}
					else
					{
						x = x - j * c[j];
						num += c[j];
					}
				}
			}
		}
		c[a[i]]++;
	}
	for (int i = 0; i < n; i++)
	{
		cout << b[i] << " ";
	}
	cout << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值