版本比较
错题本
正文
增加 1161 行删除 0 行
767 行未改动全部展开768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932767 行未改动全部展开 return 0;}```::::
## [ABC252F] Bread
### 题目信息
|题目编号|[AT_abc252_f](https://www.luogu.com.cn/problem/AT_abc252_f)||:--:|:--------:||时间 |2026.03.21 ||难度 |$\textcolor{yellow}{普及/提高-}$||算法 |贪心、优先队列 ||易错点 |剩余面包也可能成为贪心要点 ||是否二刷|是|
### 思路
这一道题其实有点像合并果子这道题,只不过是最后合并完还可能剩下一段面包,但是这一段面包什么时候出现也是贪心要点,所以也要把剩下的面包加入优先队列中。
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>#define int unsigned long longusing namespace std;int a[200005];priority_queue<int,vector<int>,greater<int> > q;signed main(){ int n,m; cin >> n >> m; int sum = 0; for(int i = 1;i <= n;i++){ int x; cin >> x; sum += x; q.push(x); } if(sum != m) q.push(m - sum); int ans = 0; while(q.size() >= 2){ int x = q.top(); q.pop(); int y = q.top(); q.pop(); ans = ans + x + y; q.push(x+y); } cout << ans;}```::::
## Silver Fox vs Monster
### 题目信息
|题目编号|[AT_abc153_f](https://www.luogu.com.cn/problem/AT_abc153_f)||:--:|:--------:||时间 |2026.03.21 ||难度 |$\textcolor{green}{普及+/提高}$||算法 |贪心||易错点 |思路走向错误、upper/lower选用错误 ||是否二刷|否|
### 思路
这道题中,如果炸弹在一个区间的中间不好思考,不妨将炸弹平移到区间的最左侧,不改变爆炸区间,这样便于思考,对于一个位置受到的伤害应当是之前引爆的炸弹对于它的伤害+剩余血量受到的最小伤害,然后我们可以发现,炸弹的影响是连续性的,所以说可以使用二分的方式找到这个炸弹之后的第一个不受影响的位置,利用差分标记,以计算每一个位置受到伤害。
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>#define int long longusing namespace std;struct point{ int x,h;}a[200005];int pos[200005];int res[200005];bool cmp(point a,point b){ return a.x < b.x;}signed main(){// freopen("B.in","r",stdin);// freopen("B.out","w",stdout); int n,m,k; cin >> n >> m >> k; for(int i = 1;i <= n;i++) cin >> a[i].x >> a[i].h; sort(a+1,a+1+n,cmp); for(int i = 1;i <= n;i++){ pos[i] = a[i].x; } m *= 2; int ans = 0; for(int i = 1;i <= n;i++){ res[i] = res[i] + res[i-1]; if(a[i].h - res[i] * k <= 0) continue; int lst = pos[i] + m; int pos1 = upper_bound(pos+1,pos+1+n,lst) - pos; int cnt = (a[i].h - res[i] * k) / k + ((a[i].h - res[i] * k) % k ? 1 : 0); ans += cnt; res[i] += cnt; res[pos1] -= cnt; } cout << ans;}```::::
## Strange Function B
### 题目信息
|题目编号|[P15571](https://www.luogu.com.cn/problem/P15571)||:--:|:--------:||时间 |2026.03.21 ||难度 |$\textcolor{yellow}{普及/提高-}$||算法 |思维、递推||易错点 |不开unsigned见祖宗 ||是否二刷|否|
### 思路
处理一个很长的数字串较有难度,所以我们考虑拆分一下,每一位分析对于答案的贡献
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>#define int long longusing namespace std;struct point{ int x,h;}a[200005];int pos[200005];int res[200005];bool cmp(point a,point b){ return a.x < b.x;}signed main(){// freopen("B.in","r",stdin);// freopen("B.out","w",stdout); int n,m,k; cin >> n >> m >> k; for(int i = 1;i <= n;i++) cin >> a[i].x >> a[i].h; sort(a+1,a+1+n,cmp); for(int i = 1;i <= n;i++){ pos[i] = a[i].x; } m *= 2; int ans = 0; for(int i = 1;i <= n;i++){ res[i] = res[i] + res[i-1]; if(a[i].h - res[i] * k <= 0) continue; int lst = pos[i] + m; int pos1 = upper_bound(pos+1,pos+1+n,lst) - pos; int cnt = (a[i].h - res[i] * k) / k + ((a[i].h - res[i] * k) % k ? 1 : 0); ans += cnt; res[i] += cnt; res[pos1] -= cnt; } cout << ans;}```::::
## CSP-J 2023 一元二次方程
### 题目信息
|题目编号|[P9750](https://www.luogu.com.cn/problem/P9750)||:--:|:--------:||时间 |2026.10.17 ||难度 |$\textcolor{yellow}{普及/提高-}$||算法 |大模拟、数学||易错点 |注意细节 ||是否二刷|否|
### 思路
题面已经提供了足够详细的信息,按照题面思路完成模拟即可
注意几个细节:
1. 系数 `x` 如果满足 $|x|=1$,则不需用输出系数
2. 判断到底什么时候取最大值
3. 记得约分
4. 注意正负号
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>using namespace std;void work(){ int a,b,c; cin >> a >> b >> c; int x = b * b - 4 * a * c; if(x < 0){ cout << "NO"; return; } else if(x == 0){ int p = -b,q = 2 * a; int y = __gcd(p,q); p /= y,q /= y; if(q == 1) cout << p; else{ if(p * q < 0) cout << '-'; cout << abs(p) << "/" << abs(q); } return; } else{ //可合并 if((int)sqrt(x)*(int)sqrt(x) == x){ int p = -b + sqrt(x),q = 2 * a; int p2 = -b - sqrt(x); double ans1 = p * 1.0 / q * 1.0; double ans2 = p2 * 1.0 / q * 1.0; if(ans1 > ans2){ if(p == 0) cout << 0; else{ int y = __gcd(p,q); p /= y,q /= y; if(q == 1 || q == -1) cout << q * p; else{ if(p * q < 0) cout << '-'; cout << abs(p) << "/" << abs(q); } } } else{ if(p2 == 0) cout << 0; else{ int y = __gcd(p2,q); p2 /= y,q /= y; if(q == 1 || q == -1) cout << q * p2; else{ if(p2 * q < 0) cout << '-'; cout << abs(p2) << "/" << abs(q); } } } } //不可合并 else{ //非根号 int p = -b,q = 2 * a; int y = __gcd(p,q); p /= y,q /= y; bool hv = true; if(p == 0){ hv = false; } else if(q == 1) cout << p; else{ if(p * q < 0) cout << '-'; if(abs(q) == 1) cout << abs(p); cout << abs(p) << "/" << abs(q); } if(hv) cout << '+'; //根号 q *= y; int rt = 1; int nx = x; for(int i = 2;i <= nx;i++){ int cnt = 0; while(nx % i == 0){ nx /= i; cnt++; } rt *= pow(i,floor(cnt/2)); if(cnt % 2 == 1) nx *= i; } int y2 = __gcd(q,rt); q /= y2; rt /= y2; if(rt != 1){ if(q == -1 || q == 1) cout << abs(rt) << "*sqrt(" << nx << ")"; else{ cout << abs(rt) << "*sqrt(" << nx << ")/" << abs(q); } } else{ if(q == -1 || q == 1) cout << "sqrt(" << nx << ")"; else{ cout << "sqrt(" << nx << ")/" << abs(q); } } }
}}signed main(){ int t,m; cin >> t >> m; while(t--){ work(); cout << endl; }}```::::
## [CSP-S 2021] 廊桥分配
### 题目信息
|题目编号|[P7913](https://www.luogu.com.cn/problem/P7913)||:--:|:--------:||时间 |2026.10.17 ||难度 |$\textcolor{green}{普及+/提高-}$||算法 |贪心||易错点 |每一次分配的廊桥一定是编号最小的空闲廊桥而不是随机分配 ||是否二刷|否|
### 思路
考虑每一班飞机,每次选择编号最小的空闲飞机进行接机操作
不难发现,对于一组数据,如果增加了一个廊桥 $k$,并不影响前 $1$ ~ $k-1$ 的接机顺序。
根据这一点可以进行贪心。维护两个小根堆堆 $q1,q2$ ,$q1$ 维护每一趟廊桥空闲的最小时间,$q2$ 维护在某一时间点时所有空闲廊桥的编号。
对于一趟飞机 $i$,现将所有在此时已经空闲的廊桥从 $q1$ 中退出,装入 $q2$。如果此时 $q2$ 非空,即有廊桥可以进行接机操作,则选择编号最小的廊桥 $x$,用$sum_x$ 记录 $x$ 的接机数量;否则就选择一个新廊桥进行接机。
分别对国内、国际进行此操作,然后前缀和计算并求出最大值即可。
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>using namespace std;struct point{ int l,r;}a[100005],b[100005];priority_queue<pair<int,int>> q,q1;priority_queue<int,vector<int>,greater<int> > q2,q3;bool cmp(point a,point b){ return a.l < b.l;}int cnt1[100005],cnt2[100005];signed main(){ int x,n,m; cin >> x >> n >> m; for(int i = 1;i <= n;i++){ cin >> a[i].l >> a[i].r; } sort(a+1,a+1+n,cmp); int t1 = 0; for(int i = 1;i <= n;i++){ while(!q.empty() && -q.top().first < a[i].l){ q2.push(q.top().second); q.pop(); } if(!q2.empty()){ q.push({-a[i].r,q2.top()}); cnt1[q2.top()]++; q2.pop(); } else{ cnt1[++t1]++; q.push({-a[i].r,t1}); } } for(int i = 1;i <= m;i++){ cin >> b[i].l >> b[i].r; } sort(b+1,b+1+m,cmp); int t2 = 0; for(int i = 1;i <= m;i++){ while(!q1.empty() && -q1.top().first < b[i].l){ q3.push(q1.top().second); q1.pop(); } if(!q3.empty()){ q1.push({-b[i].r,q3.top()}); cnt2[q3.top()]++; q3.pop(); } else{ cnt2[++t2]++; q1.push({-b[i].r,t2}); } } for(int i = 1;i <= x;i++){ cnt1[i] += cnt1[i-1]; cnt2[i] += cnt2[i-1]; } int ans = 0; for(int i = 0;i <= x;i++){ ans = max(ans,cnt1[i] + cnt2[x-i]); } cout << ans;}```::::
## [JSOI2007] 建筑抢修
### 题目信息
|题目编号|[P4053](https://www.luogu.com.cn/problem/P4053)||:--:|:--------:||时间 |2026.10.17 ||难度 |$提高(青)$||算法 |反悔贪心||易错点 | 不开long long见祖宗 ||是否二刷|否|
### 思路
对于一个任务 $i$,考虑先选择完成这项任务,如果此时超出原有限制,则考虑每一次删掉最耗费时间的任务,因为这些任务对后续任务的影响是最大的,最后留在堆中的任务就是互不影响的任务数目最大的方案,即为答案
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>#define int long longusing namespace std;struct point{ int x,y;}a[150005];bool cmp(point a,point b){ return a.y < b.y;}priority_queue<int> q;signed main(){ int n; cin >> n; for(int i = 1;i <= n;i++){ cin >> a[i].x >> a[i].y; } sort(a+1,a+1+n,cmp); int ans = 0; for(int i = 1;i <= n;i++){ ans = ans + a[i].x; q.push(a[i].x); while(!q.empty() && ans > a[i].y){ ans = ans - q.top(); q.pop(); } } cout << q.size();}```::::
## [POI 2012] HUR-Warehouse Store
### 题目信息
|题目编号|[P3545](https://www.luogu.com.cn/problem/P3545)||:--:|:--------:||时间 |2026.10.17 ||难度 |$提高(青)$||算法 |反悔贪心||易错点 | 不开long long见祖宗x2 ||是否二刷|否|
### 思路
每一次进货后先考虑满足顾客,如果产生矛盾,则撤回对购买数量最多的顾客,这些顾客是对整体计算最大的部分,然后在维护堆权值的同时维护对应位置,最后输出即可。
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>#define int long longusing namespace std;int a[250005];int b[250005];priority_queue<pair<int,int> > q;int ans[250005];signed main(){ int n; cin >> n; for(int i = 1;i <= n;i++){ cin >> a[i]; } for(int i = 1;i <= n;i++){ cin >> b[i]; } int rst = 0; for(int i = 1;i <= n;i++){ rst += a[i]; rst -= b[i]; q.push({b[i],i}); while(!q.empty() && rst < 0){ rst = rst + q.top().first; q.pop(); } } cout << q.size() << endl; int m = 0; while(!q.empty()){ auto p = q.top(); q.pop(); ans[++m] = p.second; } sort(ans+1,ans+1+m); for(int i = 1;i <= m;i++) cout << ans[i] << " ";}```::::
## 售货员的难题
### 题目信息
|题目编号|[P1171](https://www.luogu.com.cn/problem/P1171)||:--:|:--------:||时间 |2026.10.11 ||难度 |$\textcolor{green}{普及+/提高-}$||算法 |状压DP||易错点 | 选择正确的转移方式 ||是否二刷|否|
### 思路
设 $f_{x,i}$ 表示目前在 $x$,此时状态为 $i$ 的最小花费。
考虑转移,$i$ 中如果有某一位 $j$ 为 $1$,则表示位置 $j+1$ 已经被访问(因为 $j$ 从 $0$ 开始标号)。将所有已经被访问的点提取进入 $s$ 数组,则前一个点一定在 $s$ 中产生。枚举 $s_{1…r}$ 。
转移方程:$$f_{x,i}=\min_{k=1}^r(f_{x,i-2^{k-1}}+a_{x,k})$$
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>#define int long longusing namespace std;int a[25][25];int f[25][(1<<20)+5];int s[25];signed main(){ int n; cin >> n; for(int i = 1;i <= n;i++){ for(int j = 1;j <= n;j++) cin >> a[i][j]; } memset(f,0x3f,sizeof(f)); f[1][1] = 0; for(int i = 1;i < (1 << n);i++){ int m = i,t = 0; while(m){ s[++t] = m & 1; m >>= 1; } for(int k = 1;k <= n;k++){ if(!s[k]) continue; for(int l = 1;l <= n;l++){ if(k == l) continue; if(s[l]){ f[k][i] = min(f[k][i],f[l][i-(1<<(k-1))] + a[l][k]); } } } } int ans = f[1][(1<<n)-1]; for(int i = 2;i <= n;i++) ans = min(ans,f[i][(1<<n)-1]+a[i][1]); cout << ans; return 0;}```::::
## [POI 2008] PLA-Postering
### 题目信息
|题目编号|[P3467](https://www.luogu.com.cn/problem/P3467)||:--:|:--------:||时间 |2026.09.29 ||难度 |$\textcolor{yellow}{普及}$||算法 |单调栈||易错点 | 理清思路再写题 ||是否二刷|否|
### 思路
对于一个位置 $i$,如果之前有某一个位置 $j$ 使得 $a_j < a_i$,则说明对于 $i$ 位置需要多增加一张海报,但是如果 $a_i \le a_j$,则 $i$ 位置可以合并在 $j$ 位置所使用的海报中。
考虑维护一个单调递增的栈,对于每一位 $i$,删去所有大于 $a_i$ 的数,如果此时栈顶的元素 $x < a_i$,则说明需要多一张海报,$ans+1$。
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>using namespace std;int h[250005];signed main(){ int n; cin >> n; for(int i = 1;i <= n;i++){ int x; cin >> x; cin >> h[i]; } stack<int> stk; int ans = 0; for(int i = 1;i <= n;i++){ while(!stk.empty() && stk.top() > h[i]){ stk.pop(); } if(stk.empty() || stk.top() != h[i]) ans = ans + 1; stk.push(h[i]); } cout << ans;} ```::::
## [NOIP 2017 普及组] 棋盘
### 题目信息
|题目编号|[P3956](https://www.luogu.com.cn/problem/P3956)||:--:|:--------:||时间 |2026.09.16 ||难度 |$\textcolor{green}{普及+/提高-}$||算法 |广搜,最短路||易错点 | 不要尝试使用神秘算法(堆优化) ||是否二刷|否|
### 思路
这道题数据范围较小,可以考虑使用搜索。
我们设定搜索状态为 $\{x,y,c,cnt,b\}$,分别表示坐标,颜色,所画金币数量,是否使用魔法。
使用类似最短路的思想,分类讨论是否可以使用魔法,如果可以更新下一位状态,则继续向其拓展,最后求出最小的花费即可
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>using namespace std;int a[105][105];struct point{ int x,y,c,cnt; bool b;};queue<point> q;int vis[105][105][2];int ax[] = {-1,0,1,0};int ay[] = {0,-1,0,1};signed main(){ int n,m; cin >> n >> m; for(int i = 1;i <= n;i++){ for(int j = 1;j <= n;j++){ a[i][j] = -1; } } for(int i = 1;i <= m;i++){ int x,y,c; cin >> x >> y >> c; a[x][y] = c; } q.push((point){1,1,a[1][1],0,0}); memset(vis,0x3f,sizeof(vis)); vis[1][1][a[1][1]] = 0; while(!q.empty()){ point p = q.front(); q.pop(); int x = p.x,y = p.y,c = p.c,cnt = p.cnt; bool f = p.b;// cout << x << " " << y << " " << c << " " << cnt << " " << f << endl; for(int i = 0;i <= 3;i++){ int nx = x + ax[i]; int ny = y + ay[i]; if(nx < 1 || ny < 1 || nx > n || ny > n) continue; if(a[nx][ny] == -1){ if(f) continue; if(vis[nx][ny][0] > cnt + 2 + (c != 0)){ q.push((point){nx,ny,0,cnt+2+(c!=0),1}); vis[nx][ny][0] = cnt + 2 + (c != 0); } if(vis[nx][ny][1] > cnt + 2 + (c != 1)){ q.push((point){nx,ny,1,cnt+2+(c!=1),1}); vis[nx][ny][1] = cnt + 2 + (c != 1); } } else{ if(a[nx][ny] == c){ if(vis[nx][ny][c] <= cnt) continue; q.push((point){nx,ny,a[nx][ny],cnt,0}); vis[nx][ny][c] = cnt; } else{ if(vis[nx][ny][a[nx][ny]] <= cnt + 1) continue; q.push((point){nx,ny,a[nx][ny],cnt+1,0}); vis[nx][ny][a[nx][ny]] = cnt + 1; } } } } cout << (min(vis[n][n][0],vis[n][n][1]) == vis[n+1][n+1][0] ? -1 : min(vis[n][n][0],vis[n][n][1]));}```::::
## 跳石头
### 题目信息
|题目编号|[P10914](https://www.luogu.com.cn/problem/P10914)||:--:|:--------:||时间 |2026.09.11 ||难度 |$\textcolor{yellow}{普及}$||算法 |bitset||易错点 | 不要`+1` ||是否二刷|否|
### 思路
反向处理跳石头的方案。从第 $n$ 位向第 $1$ 位遍历,对于每一位 $i$,先将 $c_i$ 添加进 $bitset_i$ 中,然后如果 $i + c_i \le n$,则将 $bitset_{i+c_i}$ 中能到达的所有权值添加进 $bitset_i$,$i * 2$ 情况同理。最后记录最大的长度。
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>using namespace std;int c[40005];bitset<40005> f[40005];signed main(){ int n; cin >> n; for(int i = 1;i <= n;i++) cin >> c[i]; for(int i = n;i >= 1;i--){ f[i].set(c[i]); if(i + c[i] <= n) f[i] |= f[i+c[i]]; if(i * 2 <= n) f[i] |= f[i*2]; } int ans = 0; for(int i = 1;i <= n;i++) ans = max(ans,(int)f[i].count()); cout << ans;}```::::
## [USACO19JAN] Icy Perimeter S
### 题目信息
|题目编号|[P5198](https://www.luogu.com.cn/problem/P5198)||:--:|:--------:||时间 |2026.09.15 ||难度 |$\textcolor{yellow}{普及}$||算法 |搜索||易错点 | 思路不要太复杂 ||是否二刷|否|
### 思路
对于每一个 $#$ 连通块,考虑进行 $dfs$,然后,正常计算面积和周长即可,最后按照比值完成比大小。
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>using namespace std;int a[1005][1005];bool vis[1005][1005];int ax[] = {-1,0,1,0};int ay[] = {0,-1,0,1};int n,sum,cnt;void dfs(int x,int y){ vis[x][y] = 1; sum++; for(int i = 0;i <= 3;i++){ int nx = x + ax[i]; int ny = y + ay[i]; if(a[nx][ny] == 0) cnt++; if(a[nx][ny] == 0 || vis[nx][ny]) continue; dfs(nx,ny); }}signed main(){ cin >> n; for(int i = 1;i <= n;i++){ for(int j = 1;j <= n;j++){ char ch; cin >> ch; if(ch == '#') a[i][j] = 1; } } int anss = 0,ansl = INT_MAX; for(int i = 1;i <= n;i++){ for(int j = 1;j <= n;j++){ if(vis[i][j] || !a[i][j]) continue; dfs(i,j); if(anss < sum) anss = sum,ansl = cnt; else if(anss == sum) ansl = min(ansl,cnt); sum = 0,cnt = 0; } } cout << anss << " " << ansl; } ```::::
## Cow Travelling S
### 题目信息
|题目编号|[P1535](https://www.luogu.com.cn/problem/P1535)||:--:|:--------:||时间 |2026.09.15 ||难度 |$\textcolor{yellow}{普及}$||算法 |搜索||易错点 | 选择正确的优化方式 ||是否二刷|否|
### 思路
这道题,考虑使用 $bfs + DP$ 的方式,在 $bfs$ 的过程中,如果遇到已经访问过的位置,或者是到达时间上限的时候,就选择用 $DP$ 转移而不是用 $bfs$。
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>using namespace std;char ch[105][105];struct point{ int x,y,t;};queue<point> q;int ax[] = {-1,0,1,0};int ay[] = {0,1,0,-1};int f[105][105][20];bool vis[105][105][20];signed main(){ int n,m,t; cin >> n >> m >> t; for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ cin >> ch[i][j]; } } int sx,sy,ex,ey; cin >> sx >> sy >> ex >> ey; q.push({sx,sy,0}); f[sx][sy][0] = 1; vis[sx][sy][0] = true; int cnt = 0; while(!q.empty()){ point p = q.front(); q.pop(); for(int i = 0;i <= 3;i++){ int nx = p.x + ax[i]; int ny = p.y + ay[i]; if(nx < 1 || ny < 1 || nx > n || ny > m || ch[nx][ny] == '*') continue; if(p.t == t - 1){ vis[nx][ny][p.t] = true; f[nx][ny][t] += f[p.x][p.y][t-1]; continue; } if(vis[nx][ny][p.t+1]){ f[nx][ny][p.t+1] += f[p.x][p.y][p.t]; continue; } vis[nx][ny][p.t+1] = true; f[nx][ny][p.t+1] += f[p.x][p.y][p.t]; q.push({nx,ny,p.t+1}); } } cout << f[ex][ey][t];} ```::::
## [USACO15DEC] Switching on the Lights S
### 题目信息
|题目编号|[P2845](https://www.luogu.com.cn/problem/P2845)||:--:|:--------:||时间 |2026.09.23 ||难度 |$\textcolor{green}{普及+/提高-}$||算法 |搜索||易错点 | 注意相邻位置之间可以互相到达 ||是否二刷|否|
### 思路
考虑两种情况。
##### 情况1
直接向周围可以到达且没有走过的位置移动
##### 情况2
拉下电闸,此时对应位置电灯被点亮,此时如果这个位置周围是有被经过的位置,则说明该位置可达,跳过中间所有过程,直接移动到该位置
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>using namespace std;int n,m;vector<pair<int,int> > v[105][105];queue<pair<int,int> > q;int l[105][105];int vis[105][105];int ax[] = {1,0,-1,0};int ay[] = {0,1,0,-1};signed main(){ cin >> n >> m; for(int i = 1;i <= m;i++){ int x,y,ax,ay; cin >> x >> y >> ax >> ay; v[x][y].push_back({ax,ay}); } l[1][1] = 1; vis[1][1] = true; q.push({1,1}); int ans = 1; while(!q.empty()){ auto p = q.front(); q.pop(); int x = p.first,y = p.second; for(int i = 0;i <= 3;i++){ int nx= x + ax[i],ny = y + ay[i]; if(!vis[nx][ny] && l[nx][ny]){ q.push({nx,ny}); vis[nx][ny] = true; } } for(auto k : v[x][y]){ int nx = k.first,ny = k.second; if(l[nx][ny]) continue; bool f = false; for(int i = 0;i <= 3;i++){ int nnx = nx + ax[i],nny = ny + ay[i]; if(l[nnx][nny] && vis[nnx][nny]){ f = true; break; } } l[nx][ny] = true; ans++; if(f){ vis[nx][ny] = true; q.push({nx,ny}); } } } cout << ans;} ```::::
## [CEOI 2015] 世界冰球锦标赛 (Day2)
### 题目信息
|题目编号|[P4799](https://www.luogu.com.cn/problem/P4799)||:--:|:--------:||时间 |2025.10.04 ||难度 |$\textcolor{green}{普及+/提高-}$||算法 |meet in the middle||易错点 | 不会如何meet in the middle ||是否二刷|否|
### 思路
先对 $a$ 数组排序,删去所有超出预算范围的价格,然后折半搜索,先处理前半部分,计算出所有可能的权值,记录到数组 $rst$ 中。然后再查找后半部分,对于计算出的权值 $q$ 用二分搜索的方式计算出方案数。
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>#define int long longusing namespace std;int n,m;int a[45];int r[4000005],tot = 0,mid = 0; void dfs(int cur,int sum){ if(cur == mid + 1){ r[++tot] = sum; return; } if(sum + a[cur] <= m) dfs(cur + 1,sum + a[cur]); dfs(cur + 1,sum);}int ans = 0;void dfs2(int cur,int sum){ if(cur == mid){ ans = ans + upper_bound(r+1,r+1+tot,m - sum) - r - 1; return; } if(sum + a[cur] <= m) dfs2(cur - 1,sum + a[cur]); dfs2(cur - 1,sum);}signed main(){ cin >> n >> m; for(int i = 1;i <= n;i++){ cin >> a[i]; } sort(a+1,a+1+n); while(a[n] > m) n--; mid = (n + 1) >> 1;// for(int i = 1;i <= mid;i++) cout << a[i] << " ";// cout << endl; dfs(1,0); sort(r+1,r+1+tot);// for(int i = 1;i <= tot;i++) cout << r[i] << " ";// cout << endl; dfs2(n,0); cout << ans;}```::::
## 过河问题
### 题目信息
|题目编号|[P1809](https://www.luogu.com.cn/problem/P1809)||:--:|:--------:||时间 |2025.10.09 ||难度 |$\textcolor{yellow}{普及}$||算法 |贪心||易错点 | 讨论数学式应当列对 ||是否二刷|否|
### 思路
先排序。从大到小每次考虑两个人(不考虑最快的和第二快的人),有两种情况。
##### 情况1
让最快的人和将两个人依次送到对岸,然后再将船开回对岸。
耗时:$2a_1+a_{lst}+a_{lst-1}$
##### 情况2
让第一快的人和第二快的人先共同前往对岸,第二快的人将船开回,最慢的和第二慢的人共同乘船前往对岸,最快的人再将船开回
耗时:$a_1+2a_2+a_{lst}$
每一次去两种情况的最小值。最后有可能剩下 $2/3$ 人,此时直接处理这种情况即可。
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>#define int long longusing namespace std;int a[100005];signed main(){ int n; cin >> n; int sum = 0; for(int i = 1;i <= n;i++){ cin >> a[i]; sum += a[i]; } sort(a+1,a+1+n); int i,ans = 0; for(i = n;i > 3;i -= 2){ ans = ans + min(a[1] + a[2] + a[i] + a[2],a[i-1] + a[1] + a[i] + a[1]); } if(i == 2) ans += a[2]; else if(i == 3) ans += a[1] + a[2] + a[3]; cout << ans;}```::::
## 过河问题
### 题目信息
|题目编号|[P1809](https://www.luogu.com.cn/problem/P1809)||:--:|:--------:||时间 |2025.10.09 ||难度 |$\textcolor{yellow}{普及}$||算法 |贪心||易错点 | 讨论数学式应当列对 ||是否二刷|否|
### 思路
先排序。从大到小每次考虑两个人(不考虑最快的和第二快的人),有两种情况。
##### 情况1
让最快的人和将两个人依次送到对岸,然后再将船开回对岸。
耗时:$2a_1+a_{lst}+a_{lst-1}$
##### 情况2
让第一快的人和第二快的人先共同前往对岸,第二快的人将船开回,最慢的和第二慢的人共同乘船前往对岸,最快的人再将船开回
耗时:$a_1+2a_2+a_{lst}$
每一次去两种情况的最小值。最后有可能剩下 $2/3$ 人,此时直接处理这种情况即可。
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>#define int long longusing namespace std;int a[100005];signed main(){ int n; cin >> n; int sum = 0; for(int i = 1;i <= n;i++){ cin >> a[i]; sum += a[i]; } sort(a+1,a+1+n); int i,ans = 0; for(i = n;i > 3;i -= 2){ ans = ans + min(a[1] + a[2] + a[i] + a[2],a[i-1] + a[1] + a[i] + a[1]); } if(i == 2) ans += a[2]; else if(i == 3) ans += a[1] + a[2] + a[3]; cout << ans;}```::::
## [USACO06NOV] Bad Hair Day S
### 题目信息
|题目编号|[P2866](https://www.luogu.com.cn/problem/P2866)||:--:|:--------:||时间 |2025.09.28 ||难度 |$\textcolor{yellow}{普及}$||算法 |单调栈||易错点 | 不开long long见祖宗 ||是否二刷|否|
### 思路
与其计算一头牛会看到几头牛,不如反向计算一头牛会被多少头牛看到。
维护一个单调递减的栈。对于每一个 $i$,考虑删去所有看不到它的 $x$,即 $x \le a_i$ 的所有奶牛,此时剩余栈中的所有牛都能看到它,统计栈的大小和。
::::success[AC code]```cpp line-numbers#include<bits/stdc++.h>#define int long longusing namespace std;int a[80005];stack<int> stk;int c[80005];signed main(){ int n; cin >> n; for(int i = 1;i <= n;i++){ cin >> a[i]; } int ans = 0; for(int i = 1;i <= n;i++){ while(!stk.empty() && a[stk.top()] <= a[i]){ stk.pop(); } stk.push(i); ans = ans + stk.size() - 1; } cout << ans;}```::::