牛客小白月赛83
第一次参加牛客上的周赛,也是第一次写题解
链接:https://ac.nowcoder.com/acm/contest/72041
A. 小天的金银铜铁
第一题很简单,直接计算判断即可,唯一注意的一点是 最终得分超过 e 分则通过考核 这一个要求。
代码如下:
点击查看代码
#include <iostream>
using namespace std;
int main() {
	int a, b, c, d, e;
	scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
	int A, B, C, D;
	scanf("%d%d%d%d", &A, &B, &C, &D);
	long long r = a * A + b * B + c * C - d * D;
	if(r > e) {
		cout << "YES" << endl;
	} else {
		cout << "NO" << endl;
	}
	
	return 0;
}
B. 小天的魔法
题目的大概意思是每次可以从两个数组里面取一个数字,如果取的是第一个数组里面的数字的话,即为 ai, 那么下一次取第二个数组里面的数字是bi,此时对怪物造成的伤害是 ai * bi,同时数组里面的数字只能用一次,求一共为 x 血量的怪物至少需要取最少次数字才能击败怪物(当怪物的血量 < 1时算做击败)。
大概思路
根据最少需要取多少次数字才能使得 x 小于 1,所以每次取数据的话肯定取两个数组里面最大的元素,但是需要注意,如果 bi > x 的话,这个时候不用取 ai。
由此可以两个指针依次从数组最大的元素开始往前面走,每次先判断 bi 是不是 < x,如果真,则将 bi * ai 判断,如果依旧 < x,则 x-= bi * ai,依次判断,直到 x < 0时退出循环。
根据以上思路,可以写出如下代码:
点击查看代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int q[N];
int qa[N];
int ans;
int main() {
	int n, m, x;
	cin >> n >> m >> x;
	for(int i = 0; i < n; i ++)
		scanf("%d", &q[i]);
	for(int i = 0; i < m; i ++)
		scanf("%d", &qa[i]);
	int i = n - 1, j = m - 1;
	int flag = 1;
	sort(q, q + n);
	sort(qa, qa + m);
	while(1) {
		if(j < 0) {
			flag = 0;
			break;
		}
		if(x < 1)
			break;
		if(qa[j] >= x) {
			ans ++;
			break;
		}
		if(qa[j] < x) {
			ans += 2;
			x -= qa[j] * q[i];
			j --;
			i --;
			continue;
		}
		if(qa[j] * q[i] >= x) {
			ans += 2;
			break;
		}
	}
	if(!flag) {
		cout << -1 << endl;
	} else
		cout << ans << endl;
	
	return 0;
}
开始写的有点啰嗦,现在精简一下:
点击查看代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int q[N];
int qa[N];
int ans;
int main() {
	int n, m, x;
	cin >> n >> m >> x;
	for(int i = 0; i < n; i ++)
		scanf("%d", &q[i]);
	for(int i = 0; i < m; i ++)
		scanf("%d", &qa[i]);
	int t = 0;
	sort(q, q + n);
	sort(qa, qa + m);
	for(int i = n - 1, j = m - 1; i >= 0 && j >= 0; i --, j --) {
		t += qa[j];
		ans ++;
		if(t >= x) {
			cout << ans << endl;
			return 0;
		}
		t += qa[j] * (q[i] - 1);
		ans ++;
		if(t >= x) {
			cout << ans << endl;
			return 0;
		}
	}
	
	cout << -1 << endl;
	return 0;
}
D. 小天的子序列
写完B就去写D,因为看了C好久没看出来概率怎么算... 高中概率都不会算了...

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号