T1 小田的宝石镇
题意分析
每条路径都有对应的时间,问经过所有点所需的最短时间。
思路
路线只会有两种:
11*a 或者 a+5*b
两者取最小值。
代码
#include<bits/stdc++.h>
using namespace std;
long long a,b;
int main(){
cin>>a>>b;
if(a*2>=b){
cout<<a+b*5;
}
else {
cout<<a*2*5+a;
}
}T2 小田的省钱计划
题意分析
个商品,原价是 ,现在的价格 ,对于每件商品,省下的钱
选一个区间,使得这个区间的省钱的总和是最大的
思路
枚举区间,[i, j] ,求出区间的总和,找最大值。
可以用前缀和来快速求去区间和。
代码(老师代码)
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5005;
int n, x;
int a[N], s[N];
signed main() {
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
cin >> n >> x;
for (int i=1; i<=n; i++) {
cin >> a[i];
a[i] -= x;
s[i] = s[i-1] + a[i];
}
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
using namespace std;
int x, cnt[10];
signed main() {
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
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 公交换乘
题意分析
坐地铁:给钱,并且获得一张优惠券
坐公交: 判断是否有优惠券可以使用,有的话就是免费做。没有的话给钱。
算出总共的花费。
思路
关键在于对优惠券的处理。
一个优惠券会有哪些信息呢?
价格,时间,是否被使用
结构体构造优惠券,
坐地铁:给钱,优惠券数组,加入数组中
坐公交,扫描优惠券数组,看看是否有券是可用的,有的话,用掉;否则就给钱。
代码(老师代码)
#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 = 1e5+5;
struct P {
int p, t, used;
};
int n, op, p, t, sum = 0;
int hh = 1, tt;
P q[N];
void op1() {
sum += p;
q[++tt] = P{p, t, 0};
}
void op2() {
while (t-q[hh].t > 45) hh++;
for (int i=hh; i<=tt; i++) {
if (q[i].p >= p and q[i].used == 0) {
q[i].used = 1;
return ;
}
}
sum += p;
}
signed main() {
qwq;
cin >> n;
for (int i=1; i<=n; i++) {
cin >> op >> p >> t;
if (op == 0) op1();
else op2();
}
cout << sum;
}T5小田喂猫
题目分析
最大限度的让猫吃最多的猫粮,在这个时候可以使用二分来找,将吃的分为两个区间,在用前缀和进行统计
思路
我们要对于猫粮按照数量进⾏排序,找第⼀个数量⼤于等于 的位置,那 么前半段区间都是吃已有数量,后半段区间都是吃个。
代码(老师代码)
#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 = 1e5+5;
struct P {
int a, b;
};
bool cmp(P a1, P a2) {
return a1.b < a2.b;
}
int n, q, k;
int s1[N], s2[N];
P a[N];
int bs(int x) {
int l = 1, r = n+1, mid;
while (l < r) {
mid = (l + r) >> 1;
if (a[mid].b >= x) r = mid;
else l = mid+1;
}
return r;
}
signed main() {
qwq;
cin >> n;
for (int i=1; i<=n; i++) cin >> a[i].a;
for (int i=1; i<=n; i++) cin >> a[i].b;
sort(a+1, a+1+n, cmp);
for (int i=1; i<=n; i++) {
s1[i] = s1[i-1] + a[i].a*a[i].b;
s2[i] = s2[i-1] + a[i].a;
}
cin >> q;
while (q--) {
cin >> k;
int idx = bs(k);
cout << s1[idx-1] + (s2[n]-s2[idx-1])*k << '\n';
}
return 0;T6取数(Easy Version)
题目分析
这题其实很简单,补药复杂化了,就是枚举子串而已,只不过要注意是挑着选,不能连着选
代码(老师代码)
#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 n, maxx;
int a[15];
void dfs(int x, int sum) {
if (x > n) {
maxx = max(maxx, sum);
return ;
}
dfs(x+1, sum);
dfs(x+2, sum+a[x]);
}
signed main() {
qwq;
cin >> n;
for (int i=1; i<=n; i++) cin >> a[i];
dfs(1, 0);
cout << maxx;
return 0;