版本比较
20260714雅礼总结
正文
增加 317 行删除 31 行
20 行未改动2122232425262627282930313233343536373839344041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041054410610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514655147148149150151152153154155651561571581591601616916216316416516616716816973741707617117217317417517617717817918018118218318418518618718818919019119219319419519619779808182838485868788198199200201202203204205206972072082092103 行未改动2142152162172182191102202212222231142242252261182272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672681282692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163174 行未改动32232332432532632732832933033133233315633433533616033733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441517041641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646718146846947047147247318747447547647719147847948048119548220 行未改动- **总**用时:$90$分钟左右
### 初始思路
考虑不等式 $a_i · (t_C - x) + b_i · (t_M - y) \le c_i$,所以直接枚举了 $x$,去用不等式反推 $y$。
### 考场解题过程
直接按照制作用时和等待时间的差值从大到小排序,因为如果最不耐心的能等,那其他人肯定都能等。
然后排完序之后,就取 $w_1$(也就是最不耐心的)的差值,然后求出 $x$ 的最小值,枚举,反推 $y$ 的最小值以及最小费用。
:::error[错因]最不耐心的能等,不代表其他人肯定都能等,而且还会 $\color{black} \texttt{TLE}$。:::
### 考场代码
```cpp line-nubers
#include <bits/stdc++.h>#define int long longusing namespace std;const int N = 1e2 + 5;int t, n, bis, cak;struct node{ int a, b, c, d;}w[N];bool cmp(node x, node y){ return x.d > y.d;}void solve(){ cin >> n >> bis >> cak; for(int i = 1; i <= n; i++){ cin >> w[i].a >> w[i].b >> w[i].c; w[i].d = w[i].a * bis + w[i].b * cak - w[i].c; } sort(w + 1, w + n + 1, cmp); if(w[1].d <= 0){ cout << "0\n"; return; } int ans = 1e18; int least = ceil(w[1].d * 1.0 / w[1].a); for(int i = 1; i <= min(least, bis - 1); i++){ int cake = floor((w[1].c - w[1].a * (bis - i)) * 1.0 / w[1].b); ans = min(ans, i + cak - cake); } cout << ans << '\n';} signed main(){ cin >> t; while(t--) solve(); return 0;}```
### 赛后题解
不等式照搬:$a_i · (t_C - x) + b_i · (t_M - y) \le c_i$,不难发现 $x + y$ 有单调性。
考虑用**二分答案**判断 $(t_C - x) + (t_M - y) = mid$ 是否可行,然后求出最小费用。
化简可得:
$$(b_i - a_i) · x \le a_i · t_C + b_i · t_M + b_i · mid$$
然后**解不等式**:
设 $lh_i = (b_i - a_i),rh_i = a_i · t_C + b_i · t_M + b_i · mid$$$\begin{cases}rh_i < 0 & lh_i = 0\\x \le \lfloor \dfrac{rh_i}{lh_i} \rfloor & lh_i > 0 \\x \ge \lceil \dfrac{rh_i}{lh_i} \rceil & lh_i < 0\end{cases}$$
然后就愉快地写完了!
:::info[注意]十年 $\texttt{OI}$ 一场空,不开 $\texttt{long}$ $\texttt{long}$ 见祖宗!:::### 补题代码
```cpp line-numbers
#include <bits/stdc++.h>using namespace std;const int N = 1e2 + 5;long long t, n, tC, tM, a[N], b[N], c[N];//一定,一定,一定,要记得开long long!bool check(long long x){ long long l = max(0ll, x - tM + 1), r = min(x, tC - 1);//取l,r for(int i = 1; i <= n; i++){ long long lh = b[i] - a[i], rh = c[i] - a[i] * tC - b[i] * tM + b[i] * x;//lh,rh if(!lh && rh < 0) return false;//第一种 else if(lh < 0) l = max(l, (long long)ceil(rh * 1.0 / lh));//第二种 else if(lh > 0) r = min(r, (long long)floor(rh * 1.0 / lh));//第三种 } return l <= r;}int main(){ cin >> t; while(t--){ cin >> n >> tC >> tM; for(int i = 1; i <= n; i++) cin >> a[i] >> b[i] >> c[i]; long long l = -1, r = tC + tM - 1; while(l + 1 < r){//二分答案 long long mid = (l + r) / 2; if(check(mid)) r = mid;//这个不用说了吧…… else l = mid; } cout << r << '\n'; } return 0;}```
### 复杂度分析
**时间复杂度**:$O(N$ $log_2(t_c+t_m))$ **空间复杂度**:$O(N)$
### 本题收获
这道题其实是一道相对来讲比较简单的**数学**题,就是解一下**不等式**,然后发现**单调性**,使用**二分答案**,以后考试要舍得推,不要不舍得用草稿纸,看这道题,就是如果用一下草稿纸,很快就能 $\color{green} \texttt{AC}$ 了。
## [T2:P9958 [USACO19JAN] Shell Game B](https://www.luogu.com.cn/problem/P9958)
### 本题基本情况
- 本题**得分**:$100$ 分- **开始**时间:$8:40$ 左右- **结束**时间:$8:50$ 左右- **总**用时:$10$ 分钟左右,比较**水**
### 初始思路### 初始思路(即为补题思路)
考虑到 $1 \le N \le 100$,所以使用暴力枚举。
### 考场解题过程(即为赛后题解)
### 考场解题过程枚举石子的初始位置,在每次枚举的过程中模拟交换操作,判断 $g_j$ 底下是否为鹅卵石,如果是的话,分数加 $1$。
:::warning[需要注意的是]枚举的时候,$nut_i$ **会变化**,所以我们得将 $nut_i$ 拷贝到 $but_i$,然后操作完之后再拷贝回来:::
### 考场代码(即为补题代码)
### 考场代码
```cpp line-nubers
#include <bits/stdc++.h>using namespace std;const int N = 1e2 + 5;int n, ans, a[N], b[N], g[N], nut[4], but[4];int main(){ cin >> n; for(int i = 1; i <= n; i++) cin >> a[i] >> b[i] >> g[i]; for(int i = 1; i <= n; i++) nut[i] = i;//初始位置 for(int i = 1; i <= 3; i++){ int score = 0; for(int j = 1; j <= 3; j++) but[j] = nut[j];//以防冲散,拷贝nut数组 for(int j = 1; j <= n; j++){ swap(nut[a[j]], nut[b[j]]);//交换位置 if(nut[g[j]] == i) score++;//猜对了 } for(int j = 1; j <= 3; j++) nut[j] = but[j];//拷贝回来 ans = max(ans, score);//取最大分数 } cout << ans; return 0;}```
### 赛后题解
### 补题代码
```cpp line-numbers
```
### 复杂度分析
**时间复杂度**:$O(N)$ **空间复杂度**:$O(N)$
### 本题收获
以后可以多多使用这个**拷贝**的方法,虽说这道题没必要,可以直接在后面赋值 $1$ ~ $3$。
## [T3:P9960 [USACO19JAN] Guess the Animal B](https://www.luogu.com.cn/problem/P9960)
### 本题基本情况
3 行未改动- **总**用时:$25$ 分钟左右
### 初始思路
考虑到最后能得到答案,肯定是因为在问完 $2$ 个及以上的动物的相同特征之后,再来问一个特征,锁定对应的动物,模拟。
### 考场解题过程
维护 $problem_i$ 表示对应的动物在问锁定问题之前回答了多少个 $\texttt{Yes}$,则最后需要问 $problem_i + 1$ 个问题,也是闲着没事干维护了一个 $ok_i$ 代表要问出 $i$ 至少回答多少个 $\texttt{Yes}$。
### 考场代码
```cpp line-nubers
#include <bits/stdc++.h>using namespace std;const int N = 1e2 + 5;int n, ans, k[N], problem[N], ok[N];string s[N][N];map <string, int> mp;int main(){ cin >> n; for(int i = 1; i <= n; i++){ string name; cin >> name >> k[i]; for(int j = 1; j <= k[i]; j++){ cin >> s[i][j]; mp[s[i][j]]++;//记录来过 } } for(int i = 1; i <= n; i++) for(int j = 1; j <= k[i]; j++) if(mp[s[i][j]] > 1) problem[i]++;//重复过的 for(int i = 1; i <= n; i++) ok[i] = problem[i] + 1;//答案 for(int i = 1; i <= n; i++) ans = max(ans, ok[i]);//取最大值 cout << ans; return 0;}```
### 赛后题解
其实我的大体思路想的是完全正确的,就是 $ans = problem_i + 1$,这个是没问题的,然后我来分析一下我的错因:
:::error[错因]
- $problem_i$ 定义错误:应该是单头奶牛与其他奶牛的交集,但是我定义成了跨牛的多头奶牛的特征共点- 没有枚举交集 $∩$,直接用了 $map$ 统计了个数
:::
### 补题代码
```cpp line-numbers
#include <bits/stdc++.h>using namespace std;const int N = 1e2 + 5;int n, ans, k[N], problem[N], ok[N];string s[N][N];map <string, bool> mp[N];int main(){ cin >> n; for(int i = 1; i <= n; i++){ string name; cin >> name >> k[i]; for(int j = 1; j <= k[i]; j++){ cin >> s[i][j]; mp[i][s[i][j]] = true;//记录有s[i][j] } } for(int i = 1; i <= n; i++){ int maxx = 0; for(int j = 1; j <= n; j++){ if(i == j) continue;//同一头,记得跳过 int cnt = 0; for(int p = 1; p <= k[i]; p++)//枚举特征 if(mp[j][s[i][p]]) cnt++;//相等 maxx = max(maxx, cnt);//取最大交集数 } problem[i] = maxx; ok[i] = problem[i] + 1;//回答几个yes } for(int i = 1; i <= n; i++) ans = max(ans, ok[i]); cout << ans; return 0;}```
### 复杂度分析
**时间复杂度**:$O(N ^ 2KlogN)$ **空间复杂度**:$O(N$ $logN)$
### 本题收获
以后维护一个数组的时候,一定要在又一开始就想清楚这个数组的含义,不要想当然的就把他算进正确答案。
该枚举的时候还是得枚举,不能因为要省去时间复杂度就舍不得用去循环。
## [T4:T784195 [USACO13NOV] Pogo-Cow S 加强版](https://www.luogu.com.cn/problem/T784195)
### 本题基本情况4 行未改动- **总**用时:$30$ 分钟左右
### 初始思路
看到题目的第一眼,我觉得首先想到的是 $\texttt{BFS}$,我也觉得很奇怪,为什么会想到 $\texttt{BFS}$ 去……
然后第二眼才发现,**模拟**说不定能骗点分,然后就非常愉快的拿了 $5$ 分……
### 考场解题过程
枚举起点和方向,然后暴力求最大得分
### 考场代码
```cpp line-nubers
#include <bits/stdc++.h>using namespace std;const int N = 3e3 + 5;int n, ans;struct node{ int pos, score;}a[N];bool cmp(node x, node y){ return x.pos < y.pos;}int main(){ cin >> n; for(int i = 1; i <= n; i++) cin >> a[i].pos >> a[i].score; sort(a + 1, a + n + 1, cmp); for(int i = 1; i <= n; i++) for(int j = -1; j <= 1; j += 2){ int scr = a[i].score, lst = a[i].pos, dis = 0; for(int k = i + j; k >= 1 && k <= n; k += j) if(abs(a[k].pos - lst) >= dis){ dis = abs(a[k].pos - lst); scr += a[k].score; lst = a[k].pos; } ans = max(ans, scr); } cout << ans; return 0;}```
### 赛后题解
不难想到,从右往左跳实际上就是从左往右跳的“逆运算”,也就是直接把 $x$ 取相反数,考虑对坐标 $x$ 排序。
#### 测试点1~3(没骗到 15 分awa。。。)
##### 状态定义
`dp[i][j]` 表示在 $i$ 号点且前一次跳了 $j$ 能获得的最大分数
##### 确定答案
$$\max dp_{i, j}$$
##### 状态转移
考虑直接枚举下一次跳到 $k$,当 $x_k - x_i \ge j$ 时,$dp_{i, j}$ 可以转移到 $dp_{k, x_k - x_i}$
##### 初始状态
$$dp_{i, 0} = p_i$$
#### 测试点4~10(这 50 分就有一点点难想了)
##### 状态定义
`dp[i][j]` 表示在 $i$ 号点且上一次在 $j$ 号点能获得的最大分数
##### 确定答案
$$\max dp_{i, j}(j < i,i, j ∈ [0,\max(x_i)])$$
##### 状态转移
$$dp_{i, j} = p_i + \max(dp_{i, j} | x_i - x_j \le x_k - x_i)$$
##### 初始状态
$$dp_{i, i} = p_i$$
#### 所有测试点(有亿点点费脑筋,能想出来的直接膜拜👍)
如果 $i$ 不变,随着 $k$ 变大,$j$ 的最小值则会变小,$\max(dp_{i, j} | x_i - x_j \le x_k - x_i)$ 也会变大,考虑使用**双指针**和**后缀最大值**优化枚举 $j$ 的过程,就不会 $\color{black} \texttt{TLE}$ 了
### 补题代码
```cpp line-numbers
#include <bits/stdc++.h>#define int long longusing namespace std;const int N = 3e3 + 5;int n, ans, dp[N][N], maxx[N][N];struct node{ int loc, scr;}a[N];bool cmp(node a, node b){ return a.loc < b.loc;}void solve(){ for(int i = 1; i <= n; i++){ dp[i][i] = a[i].scr; ans = max(ans, dp[i][i]); } for(int i = 1; i <= n; i++){ for(int j = i - 1; j >= 1; j--){ int k = i - 1; while(k >= 1 && a[i].loc - a[j].loc >= a[j].loc - a[k].loc) k--; dp[i][j] = a[i].scr + maxx[j][k + 1]; ans = max(ans, dp[i][j]); } for(int j = i - 1; j >= 1; j--) maxx[i][j] = max(dp[i][j], maxx[i][j + 1]); maxx[i][i] = dp[i][i]; }}signed main(){ cin >> n; for(int i = 1; i <= n; i++) cin >> a[i].loc >> a[i].scr; sort(a + 1, a + n + 1, cmp); solve(); reverse(a + 1, a + n + 1); for(int i = 1; i <= n; i++) a[i].loc = -a[i].loc; solve(); cout << ans << '\n'; return 0;}```
### 复杂度分析
**时间复杂度**:$O(N ^ 2)$ **空间复杂度**:$O(N ^ 2)$
### 本题收获
这道题唯一需要注意的是:“其实我可以骗分骗的更多”,其实能拿 $\color{orange} \texttt{50}$ 分的,真的没想到会出 $dp$,下次需要注意。
# 整体反思
## 做得较好的地方
签到题还是 $\color{green} \texttt{AC}$ 了的,抓住了这个机会,而且 $\text{freopen}$ 也没有写错。其他的题能暴力骗点分的也都骗了分,最后该检查的也检查了
## 需要改进的地方
写完暴力之后,可以想着怎么去优化,在能拿到得分的情况下再**多拿一点分**,而且这次只是侥幸拿到了第 $6$ 名,并不代表真实实力,真正的大佬们都被 $\text{freopen}$ 等东西卡住了,下次加油,也*不要为打翻的牛奶哭泣*。
## 时间安排总结
自认为安排的挺好的,先总体看一遍题,然后再**先易后难**依次做每一道题,最后检查一下,时间安排得很妥当,下次延用。
##### 感谢观看QwQ~