914第一次课
T1 小田的宝石镇
题意分析
每条路径都有对应的时间,问经过所有点所需的最短时间。
思路
路线只会有两种:
11*a 或者 a+5*b
两者取最小值。
代码
#include <bits/stdc++.h>
#define int long long
#define qwq ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
int a, b;
signed main() {
qwq;
cin >> a >> b;
cout << min(11*a, a+5*b);
return 0;
}
T2 小田的省钱计划
题意分析
选一个区间,使得这个区间的省钱的总和是最大的
思路
枚举区间,[i, j] ,求出区间的总和,找最大值。
可以用前缀和来快速求去区间和。
代码
#include <bits/stdc++.h>
#define int long long
#define qwq ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 5005;
int n, x;
int a[N], s[N];
signed main() {
qwq;
cin >> n >> x;
for (int i=1; i<=n; i++) {
cin >> a[i];
a[i] -= x;
s[i] = s[i-1] + a[i];
}
// 枚举区间 i,j
int maxx = 0;
for (int i=1; i<=n; i++) {
for (int j=i; j<=n; j++) {
int sum = s[j] - s[i-1];
maxx = max(maxx, sum);
}
}
cout << maxx;
return 0;
}
错因
T3 熊与象
题意分析
6个数字,如果没有数字出现的次数大于等于4,直接输出 Alien
把这个数字的出现次数减去4,再看剩下两个数字是相等还是不相等,如果相等,输出 Elephant,否则 Bear
思路
用桶来统计数字的出现次数,按照题意处理。
代码
#include <bits/stdc++.h>
#define int long long
#define qwq ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
int x, cnt[10];
signed main() {
qwq;
for (int i=1; i<=6; i++) {
cin >> x;
cnt[x]++;
}
int f = 0;
for (int i=1; i<=9; i++) {
if (cnt[i] >= 4) {
f = 1;
cnt[i] -= 4;
}
}
if (f == 0) {
cout << "Alien";
return 0;
}
for (int i=1; i<=9; i++) {
if (cnt[i] == 2) {
f = 2;
}
}
if (f == 2) cout << "Elephant";
else cout << "Bear";
return 0;
}
T4 公交换乘
题意分析
坐地铁:给钱,并且获得一张优惠券
坐公交: 判断是否有优惠券可以使用,有的话就是免费做。没有的话给钱。
算出总共的花费。
思路
关键在于对优惠券的处理。
一个优惠券会有哪些信息呢?
价格,时间,是否被使用
结构体构造优惠券,
坐地铁:给钱,优惠券数组,加入数组中
坐公交,扫描优惠券数组,看看是否有券是可用的,有的话,用掉;否则就给钱。
代码
第五题题意
有很多猫粮,每种猫粮价值是
吃
- 数量大于等于
k ,吃 k 包 - 数量小于
k ,b[i] 包