Coffee and Coursework

这个问题我一开始没做出来,参考了https://blog.csdn.net/a1097304791/article/details/87874948#t3 和 https://www.cnblogs.com/albert67/p/10414426.html|
之后才会做,我就权当记录一下,并且再理一下思路吧:

收获:1:二分查找在数据量较大的有序数据中的优化作用;

           2:思维题模摸门道

本题思路:枚举天数判断那一天能喝完且最少,可用二分查找优化。

思维上有问题的可能是:咖啡因在一天多次时的损耗-1、-2等不管是先喝咖啡因多得还是咖啡因少的都是一样的,多的-1和少的-1是一样的;所以不管你是正序排列还是逆序排列答案都是一样的;

我加了些注释:

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

typedef  long long LL;
const int maxn = 210000;

int n, m, arr[maxn];
bool cmp(int a, int b) { return a > b; }
bool check(int x)
{ 
	LL sum = 0;
	for (int i = 1, t = 0; i <= n; i++) {
		sum += ( arr[i] - t );
		if (i%x == 0) t++;    //这里用得很好, 表示每组为x天
		if (arr[i] <= t) break;  //剪枝:i再增加,arr[i]也不会增加了,可以加速了
	}
	/*int k = 1;
	int t = 0;
	for (int i = 1; i <= n; ++i)
	{
		if (k == x)break;
		if (arr[i] - t <= 0)
		{
			k++;
			t = 1;
			--i;
		}
		sum += arr[i] - t;
		t++;
	}*/
	return sum >= m;
}
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		cin >> arr[i];
	sort(arr + 1, arr + 1 + n, cmp);

	int l = 1, r = n + 1, ans = 1 << 30;//有可能完不成
	/*
	二分查找的作用在于快速找到最少的可以完成的天数
	*/
	while (l < r) {
		int mid = ( l + r ) / 2;
		if (check(mid))
			r = mid, ans = min(ans, mid); //能完成,所以看看能不能天数再少点
		else
			l = mid + 1; //完不成,所以天数加一天
	}
	if (ans != 1 << 30) cout << ans << endl;
	else cout << "-1" << endl;

	return 0;
}

 

期末作业要求如下: Write a program that will help elementary school pupils practice math. a) The program will first ask the user for his/her ID number (including two letters & 4 digits), e.g. Please input your four digit ID no: AB1234 The program should have input validation. Then the program prompts three choices: (1) Start a test (2) Check scores (3) Exit Test: the program will give 10 math problems, e.g.: 12 * 3 = 36 48 + 32 = 80 … 56 / 28 = 2 Note: i) Pupils will answer each problem before the next one is given. ii) The problems should include addition, subtraction, multiplication and division. They are randomly generated. iii) Randomly generates numbers for problems. However, must ensure that both the problem and the result are no larger than two digits. The problem and the result should be greater than or equal to zero. The divisor cannot be zero. iv) After ten problems are finished, record the time used by the student to do the ten problems. v) Gives a score to each student. Saves this student’s ID, his/her score and the time used into a file named ‘record.txt’. vi) Print the following information on the screen: Prob. | Correct Answ. | Ur Answ c) Check scores: Searches the file ‘record.txt’ and lists all the historical scores for this student, e.g.: Your previous records are: AB1234 80 150 seconds AB1234 50 182 seconds AB1234 90 98 seconds You will be marked based on your program’s: (1) Correctiveness (2) Readability (3) Robustness (4) Conciseness 目前本人的程序可以直接使用,并完全满足要求。还是建议学弟学妹在字里行间多多理解,争取自己写出漂亮的代码! 向本人在完成此作业过程中参考的代码表示衷心的感谢!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值