错题本

· · 个人记录

总结

正文

最大乘积

题目信息

题目编号 P1249
难度 \textcolor{yellow}{普及/提高-}
时间 2025.9.6
算法 贪心、高精度
易错点 没有写高精度
是否二刷 是

思路

不难发现,和一定,差小积大,所以我们要尽量减少每两个相邻数字的差值,所以我们可以想到若干连续的除 1 以外的自然数,即 2,3,4,5…

但是又不是每一个数都可以恰好分解成若干连续自然数相加,但是为了保证差值最小化且不出现重复数字,我们可以从后往前一轮又一轮的为每一个数匀上 1,直到与原数没有差值

最后还要记得用高精度算法求出所有数的积

::::success[AC code]

#include<bits/stdc++.h>
//#pragma GCC opminize(2,3,"Ofast","inline")
#define endl '\n'
//#define int register int
//#define int long long
//#define double long double
const int N = 1e5,M = 1e5 + 10;
const double pi = 3.1415927;
using namespace std;
int a[10005];
int p[100005];
void multiplus(int x){
    int t = 0;
    for(int i = N;i >= 0;i--){
        p[i] = p[i] * x + t;
        t = p[i] / 10;
        p[i] %= 10;
    }
}
void print(){
    int l = 0;
    while(l <= N && !p[l]) l++;
    for(int i = l;i <= N;i++){
        cout << p[i]; 
    }
}
signed main(){
    ios::sync_with_stdio(false);
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    //<----初始化---->
    int n;
    cin >> n;
    int sum = 0,t = 0;
    for(int i = 2;sum <= n;i++){
        sum = sum + i;
        a[++t] = i;
        if(sum + i + 1 > n) break;
    }
    int pos = t,c = n - sum;
    while(c){
        a[pos]++;
        pos--;
        if(pos < 1) pos = t;
        c--; 
    }
    p[N] = 1;
    for(int i = 1;i <= t;i++){
        cout << a[i] << " ";
        multiplus(a[i]);
    }
    cout << endl;
    print();
    return 0;
}

::::

三连击(升级版)

题目信息

题目编号 P1618
难度 \textcolor{orange}{普及-}
时间 2025.10.2
算法 枚举
易错点 A、B、C可以为0
是否二刷 是

思路

不难发现,这道题我们可以先特判含有0的情况

再尝试枚举其中的一个数,然后将按照比例对剩余的两个数进行求解,筛去含有重复数字或者0,又或是不为整数的组合

最后输出所有组合或者判断是否无解

:::success[AC code]

#include<bits/stdc++.h>
using namespace std;
bool check(int x,int y,int z){
    int t[15];
    memset(t,0,sizeof(t));
    for(int i = 1;i <= 3;i++){
        t[x % 10]++;
        t[y % 10]++;
        t[z % 10]++;
        x /= 10,y /= 10,z /= 10;
    }
    for(int i = 1;i <= 9;i++){
        if(t[i] != 1) return false;
    }
    return !t[0];
}
signed main(){
    int a,b,c;
    int cnt = 0;
    cin >> a >> b >> c;
    if(!a||!b||!c){
        cout << "No!!!";
        return 0;
    }
    int gcd = __gcd(a,__gcd(b,c));
    a /= gcd,b /= gcd,c /= gcd;
    for(int x = 123;x <= 987;x++){
        double y = (double)x * 1.0 / (double)a * 1.0 * b;
        int ny = x / a * b;
        double z = (double)x * 1.0 / (double)a * 1.0 * c;
        int nz = x / a * c;
        if((int)y != ny || (int)z != nz || ny > 999 || nz > 999) continue;
        if(check(x,ny,nz)){
            cout << x << " " << ny << " " << nz << endl;
            cnt++;
        }
    }
    if(cnt == 0) cout << "No!!!";
}

::::

地毯填补问题

题目信息

题目编号 P1228
时间 2025.10.2
难度 \textcolor{yellow}{普及/提高-}
算法 分治
易错点 细节错误(如公主位置和左上角坐标位置弄反)、没看出是分治
是否二刷 是

思路

如果直接填充 8*8 的棋盘好像有点难度,但是我们可以将 8*8 的棋盘拆解成 4 个 4*4 的棋盘,4*4 的棋盘又可以拆解成 4 个 2*2 的棋盘……

以此类推,可以想到用分治算法解决此题。

观察这个棋盘,以棋盘中心做分界线,我们假设围绕中心的 4 个点可以放置假公主,但是每一个区域内只能有一个公主(不论真假)

那么我们如果每次解决的是大小为 l*l 的棋盘,且有一个公主位于左上角,那么左上角区域的假公主就不能放置,其他情况同理。

这样我们就可以一步步逼近正确答案了。

::::success[AC code]

#include<bits/stdc++.h>
using namespace std;
void dfs(int x,int y,int a,int b,int r){
    if(r == 1) return;
    r /= 2;
    int mx = a + r - 1,my = b + r - 1;
    if(x <= mx && y <= my){
        cout << mx + 1 << " " << my + 1 << " " << 1 << endl;
        dfs(x,y,a,b,r);
        dfs(mx,my+1,a,b+r,r);
        dfs(mx+1,my,a+r,b,r);
        dfs(mx+1,my+1,a+r,b+r,r);
    }
    if(x <= mx && y > my){
        cout << mx + 1 << " " << my << " " << 2 << endl;
        dfs(mx,my,a,b,r);
        dfs(x,y,a,b+r,r);
        dfs(mx+1,my,a+r,b,r);
        dfs(mx+1,my+1,a+r,b+r,r);
    }
    if(x > mx && y <= my){
        cout << mx << " " << my + 1 << " " << 3 << endl;
        dfs(mx,my,a,b,r);
        dfs(mx,my+1,a,b+r,r);
        dfs(x,y,a+r,b,r);
        dfs(mx+1,my+1,a+r,b+r,r);
    }
    if(x > mx && y > my){
        cout << mx << " " << my << " " << 4 << endl;
        dfs(mx,my,a,b,r);
        dfs(mx,my+1,a,b+r,r);
        dfs(mx+1,my,a+r,b,r);
        dfs(x,y,a+r,b+r,r);
    }
}
signed main(){
    int k,x,y;
    cin >> k >> x >> y;
    dfs(x,y,1,1,1<<k);
}

::::

拼数

题目信息

题目编号 P1012
时间 2025.10.3
难度 \textcolor{yellow}{普及/提高-}
算法 排序
易错点 排序方式没有get清楚(误当成字典序排序)
是否二刷 是

思路

这道题不难发现我们要让最后拼成的数字更大,就应当基于这些数字拍一个特定的数据,所以我们要使用排序算法。

排序的依据应当是对于两个不同的数 X 和 Y,如果 X+Y > Y+X,就应当让 X 排在 Y 之前。

::::success[AC code]

#include<bits/stdc++.h>
using namespace std;
string s[25];
bool cmp(string s,string t){
    return (s + t) < (t + s);
}
signed main(){
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++){
        cin >> s[i];
    }
    sort(s+1,s+1+n,cmp);
    for(int i = n;i >= 1;i--) cout << s[i];
}

::::

[Poetize6]IncDec Sequence

题目信息

题目编号 P4552
时间 2025.10.3
难度 \textcolor{green}{普及+/提高}
算法 差分、结论
易错点 没有开long long
是否二刷 是

思路

如果是区间加减的话,我们不难想到这题可以用差分解决。

我们先根据已有的数组求出差分数组 s,考虑贪心策略,不难发现差分数组有正有负,那么有以下几种情况:

  1. 如果同时选择一个 X(X < 0) 和一个 Y(Y > 0),那么此时我们要将他们清空,则需要的最小代价为 |\min(X,Y)|
  2. 如果我们选择一个 X(X>0),要将其与 s_1 清空,则需要 |X| 的代价。
  3. 如果我们选择一个 X(X<0),要将其

综上所述,我们便可以推出:

如果计 s 中所有正数的和为 X,所有负数的绝对值的和为 Y,则有

ans_1 = |X-Y| + \min(X,Y) \\ ans_2 = |X-Y| + 1

::::success[AC code]

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[100005];
int c[100005];
signed main(){
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++){
        cin >> a[i];
        c[i] = a[i] - a[i-1];
    }
    int cnt0 = 0,cnt1 = 0;
    for(int i = 2;i <= n;i++){
        (c[i] < 0 ? cnt0 += abs(c[i]) : cnt1 += c[i]);
    }
    cout << abs(cnt0 - cnt1) + min(cnt0,cnt1) << endl;
    cout << abs(cnt0 - cnt1) + 1;
}

::::

【模板】扫描线&矩阵面积并

题目信息

题目编号 P5490
时间 2025.10.3&2025.10.4
难度 \textcolor{blue}{提高+/省选-}
算法 扫描线、离散化、线段树
易错点 细节问题(+= 写成 =)
是否二刷 是

思路

对于一个矩阵面积并,我们不妨可以尝试将其分解成多个不相交的小矩阵的面积和,如下图所示

不难发现,当我们沿着 y 轴自下而上的扫过整个坐标系时,这一整个不规则图形的面积就被分成了若干个小矩阵的面积

我们可以发现这些小矩阵的底边的宽与这些长方形的宽有一些关系,我们以长方形的长向 x 轴做垂线,此时每一个长方形的宽就被分成如上图所示的那样。

我们把长方形的下底边看成是这个这个长方形的入边,上底边看做是长方形的出边,这是我们就会得到:

当扫描线经过入边时,该长方形就开始对整幅图形的面积产生贡献;当扫描线经过出边时,该长方形的贡献停止

所以我们不难 get 到,当我们可以将每个长方形做出的贡献进行累加,每次经过入边,贡献 +1,经过出边,贡献 -1。

又因为我们之前对每一条底边进行过分解,于是我们就可以将一条完整的长转化成一个个具有公共性的线段,这是我们就可以用线段树来优化这个思路了,即

我们每次遇到入边就将该入边包含的线段编号对应的出现次数都 +1,遇到出边就 -1,最终每一个小矩阵答案就是所有 入边出现的次数 × 矩阵上下y坐标的差

当然,由于 x 坐标有一个特点:范围大,数量小,所以我们还需要离散化来辅助我们完成这道题。

::::success[AC code]

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int nx[200005],ny[200005];
struct point{
    int ans,cnt,sum;
}a[100005<<3];
struct node{
    int x,l,r,p;
}t[200005];
bool cmp(node a,node b){
    return a.x < b.x;
}
map<int,int> mp;
int L[200005];
void push_up(int pos){
    if(!a[pos].cnt){
        a[pos].ans = a[pos*2].ans + a[pos*2+1].ans;
    }
    else a[pos].ans = a[pos].sum;
    return;
}
void build(int pos,int l,int r){
    if(l == r){
        a[pos].sum = ny[l];
        a[pos].ans = a[pos].cnt = 0;
        return;
    }
    int mid = ((l + r) >> 1);
    build(pos * 2,l,mid);
    build(pos * 2 + 1,mid + 1,r);
    push_up(pos);
    a[pos].sum = a[pos * 2].sum + a[pos * 2 + 1].sum;
}
void add(int L,int R,int pos,int l,int r,int p){
    if(L <= l && R >= r){
        a[pos].cnt = a[pos].cnt + p;
        if(l == r) a[pos].ans = (a[pos].cnt > 0 ? 1 : 0) * a[pos].sum;
        else push_up(pos);
        return;
    }
    int mid = (l + r) >> 1;
    if(mid >= L) add(L,R,pos * 2,l,mid,p);
    if(mid < R) add(L,R,pos * 2 + 1,mid + 1,r,p); 
    push_up(pos);
}
signed main(){
    cin >> n;
    int cnt = 0,tot = 0;
    for(int i = 1;i <= n;i++){
        int ax,ay,ex,ey;
        cin >> ax >> ay >> ex >> ey;
        t[++tot] = node{ax,ay,ey,1};
        ny[++cnt] = ay;
        t[++tot] = node{ex,ay,ey,-1};
        ny[++cnt] = ey;
    }
    sort(ny+1,ny+1+cnt);
    cnt = unique(ny+1,ny+1+cnt) - ny - 1;
    for(int i = 1;i <= tot;i++){
        t[i].l = lower_bound(ny + 1,ny + 1 + cnt,t[i].l) - ny;
        t[i].r = lower_bound(ny + 1,ny + 1 + cnt,t[i].r) - ny - 1;
    }
    for(int i = 1;i < cnt;i++){
        ny[i] = ny[i+1] - ny[i];
    }
    cnt--;
    sort(t+1,t+1+tot,cmp);
    build(1,1,cnt);
    int ans = 0;
    for(int i = 1;i <= tot;i++){
        ans += (t[i].x - t[i-1].x) * a[1].ans;
        add(t[i].l,t[i].r,1,1,cnt,t[i].p);
    }
    cout << ans;
}

::::

过河卒

题目信息

题目编号 P1002
时间 2025.10.4
难度 \textcolor{orange}{普及-}
算法 递推
易错点 没有开 long long
是否二刷 是

思路

这道题不难发现是个递推,我们假设过河卒走到格子 (i,j) 是的方案数为 a_{i,j},则有 a_{i,j} = a_{i-1,j} + a_{i,j-1},然而有些格子是不能走的,比如马所在的格子和马能走到的格子,我们将这些格子标记为 -1,如果一个格子的左边或者上面有各自是不能走的或是越界的,我们的计算公式就会变为 a_{i,j} = a_{i-1,j} 或 a_{i,j} = a_{i,j-1} 或 a_{i,j} = 0

于是我们将第 0 行和第 0 列的能从原点走到的格子都设为 1 初始化就完成了。

::::success[AC code]

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[25][25];
int ax[] = {-2,-2,-1,-1,1,1,2,2};
int ay[] = {-1,1,-2,2,2,-2,-1,1};
signed main(){
    int n,m,x,y;
    cin >> n >> m >> x >> y;
    n++,m++,x++,y++;
    a[x][y] = -1;
    for(int i = 0;i <= 7;i++){
        int nx = x + ax[i],ny = y + ay[i];
        if(nx > 0 && ny > 0) a[nx][ny] = -1;
    }
    for(int i = 1;i <= n;i++){
        if(a[i][1] == 0) a[i][1] = 1;
        else break;
    }
    for(int i = 2;i <= m;i++){
        if(a[1][i] == 0) a[1][i] = 1;
        else break;
    }
    for(int i = 2;i <= n;i++){
        for(int j = 2;j <= m;j++){
            if(a[i][j]) continue;
            a[i][j] = (a[i-1][j] == -1 ? 0 : a[i-1][j]) + (a[i][j-1] == -1 ? 0 : a[i][j-1]);
        }
    }
    cout << a[n][m];
}

::::

外星密码

题目信息

题目编号 P1928
时间 2025.10.4
难度 \textcolor{yellow}{普及/提高-}
算法 模拟/递归
易错点 细节问题(多次复制写成了翻倍复制)
是否二刷 是

思路

不难理解,这些括号就像一个卷心菜,一层包一层,所以如果我们想要处理第 1 层的括号,就要先处理第 2 层的括号;要处理第 2 层的括号,就要先处理第 3 层的括号……

于是我们为这些括号标个序号,越靠外,层数越小。然后我们从左往右来像这个很复杂的字符串拆开,得到了像下图一样的这串东西。

这时候,我们发现这个字符串好像更直观了一些,我们就把它当成是把这个卷心菜一层一层掰开,在同一个括号内的元素序号相同,比如 3 和 C 都是第 2 层的…

对于同一层的元素,我们把字母和字母拼在一起,数字和数字拼在一起,最终可可以得到这一层展开之后的最终形式,如 [3C] 变成 CCC,但是由于括号没了,所以我们将这个新的字符串降一级,与下一级的 B、D 一起被操作

不难发现,由内之外的去操作,所以我们需要用到栈来辅助我们完成这个操作的过程

::::success[AC code]

#include<bits/stdc++.h>
#define int long long
using namespace std;
string s;
stack<pair<string,int>> stk;
stack<pair<int,int>> stk1;
int cnt = 0;
string p[10005];
int check(string tar){ //1->alpha 2->number 3->[ 4->]
    if(tar == "[") return 3;
    if(tar == "]") return 4;
    if(tar[0] >= 'A' && tar[0] <= 'Z') return 1;
    return 2;
}
signed main(){
    cin >> s;
    int len = s.size();
    int t = 0;
    string s1 = "";
    string num1 = "";
    for(int i = 0;i < len;i++){
        if(s[i] >= 'A' && s[i] <= 'Z') s1 += s[i];
        else if(s[i] == '['){
            if(num1.size() > 0) p[++t] = num1,num1 = "";
            if(s1.size() > 0) p[++t] = s1,s1 = "";
            p[++t] = '[';
        }
        else if(s[i] >= '0' && s[i] <= '9') num1 = num1 + s[i];
        else{
            if(num1.size() > 0) p[++t] = num1,num1 = "";
            if(s1.size() > 0) p[++t] = s1,s1 = "";
            p[++t] = ']';
        }
    }
    if(s1.size() > 0) p[++t] = s1;
    for(int i = 1;i <= t;i++){
        int cd = check(p[i]);
        if(cd == 1){
            stk.push({p[i],cnt});
        }
        else if(cd == 2){
            int x = 0;
            for(auto k : p[i]) x = x * 10 + k - '0';
            stk1.push({x,cnt});
        }
        else if(cd == 3){
            cnt++;
        }
        else{
            pair<int,int> num = stk1.top();
            stk1.pop();
            pair<string,int> str = stk.top();
            stk.pop();
            string k = str.first;
            cnt--;
            while(!stk.empty() && stk.top().second == str.second){
                k = stk.top().first + k;
                stk.pop(); 
            }
            string nk = "";
            for(int i = 1;i <= num.first;i++){
                nk = nk + k;
            }
            str = {nk,cnt};
            stk.push(str);
        }
    }
    string ans = "";
    while(!stk.empty()){
        ans = stk.top().first + ans;
        stk.pop();
    }
    cout << ans;
}

::::

逆序对

题目信息

题目编号 P1908
时间 2025.10.5
难度 \textcolor{yellow}{普及/提高-}
算法 树状数组
易错点 不开long long
是否二刷 是

思路

我们来想一想,如果两个数互为逆序对,那么它们之间的规律是什么呢?

如果一个数 a_i > a_j 且 i < j,则我们称 (a_i,a_j) 是一组逆序对,这时如果我们对这个数组从小到大排序,那么 a_i 一定排在 a_j 之后,所以如果我们存储一下这些数在原数组中的位置,则 pos_{a_i} 一定排在 pos_{a_j} 之后,所以我们用树状数组来维护每一个原始位置出现的次数,我们会发现当我们找到一个数 a_i 时,与其形成逆序对的数的个数就是排完序后在它前面,但那时原始位置在他后面的数,即

我们可以通过查找树状数组中pos_{a_i} - 1 这些数出现的次数,就可得到对于这个数的逆序对个数。

::::success[AC code]

#include<bits/stdc++.h>
#define int long long
using namespace std;
struct point{
    int x,pos;
}a[500005];
int n;
int c[500005];
bool cmp(point a,point b){
    return (a.x == b.x ? a.pos > b.pos : a.x > b.x);
}
int lowbit(int x){
    return x & (-x);
}
void add(int x,int p){
    for(int i = x;i <= n;i = i + lowbit(i)){
        c[i] += p;
    }
}
int query(int x){
    int ans = 0;
    for(int i = x;i > 0;i = i - lowbit(i)){
        ans = ans + c[i];
    }
    return ans;
}
signed main(){
    cin >> n;
    for(int i = 1;i <= n;i++){
        cin >> a[i].x;
        a[i].pos = i;
    }
    sort(a+1,a+1+n,cmp);
    int ans = 0;
    for(int i = 1;i <= n;i++){
        add(a[i].pos,1);
        ans = ans + query(a[i].pos - 1);
    }
    cout << ans;
}

::::

【MX-S4-T1】「yyOI R2」youyou 的垃圾桶

题目信息

题目编号 P11217
时间 2025.10.25
难度 \textcolor{green}{普及+/提高}
算法 线段树、树上二分
易错点 到底是先二分再求值还是直接二分寻找位置
是否二刷 否

思路

我们发现,每一次的更改都是区间性的,同时也是持续性的,但是 n、m 都非常大,那么我们可以考虑用线段树来维护区间和,以方便计算每一次的攻击力总和

其中线段树要记得加上懒标记,否则会 TLE,其余线段树的区间加、区间查询便不再赘余

那么当我们要判断进行几轮之后会结束这一场,就可以先计算能进行完整的几趟,我们记录每一次总和的系数 k,每次将其翻倍,进行求解。

那剩余的这一部分呢?我们可以考虑二分,但是如果使用的是朴素二分 + 树上查询,时间复杂度是 O(\log^2 n) 的,这样就会超时,那么我们可以换一个思路,进行树上二分,即

对于每一次查询,如果左半边的含系数总和比剩余生命值要大,则向左半部分查找,反之向右半部分查找

这样有一个好处,就是可以快速、准确地查找到最后一次的所在位置,并且将 O(\log^2 n) 降为了 O(\log n) 的时间复杂度,大大提高了插叙效率。

::::success[AC code]

#include<bits/stdc++.h>
#define int long long
using namespace std;
struct point{
    int sum,laz,l,r;
}a[800005];
int b[400005]; 
int tot = 0;
int n,q,d;
inline void build(int pos,int l,int r){
    tot = max(tot,pos);
    a[pos].l = l,a[pos].r = r;
    if(l == r){
        a[pos].sum = b[l];
        return;
    }
    build(pos << 1,l,(l + r) >> 1);
    build((pos << 1) + 1,((l + r) >> 1) + 1,r);
    a[pos].sum = a[pos << 1].sum + a[(pos << 1) + 1].sum;
}
inline void push_down(int pos){
    a[pos << 1].laz += a[pos].laz;
    a[(pos << 1) + 1].laz += a[pos].laz;
    a[pos << 1].sum = a[pos << 1].sum + a[pos].laz * (a[pos << 1].r - a[pos << 1].l + 1);
    a[(pos << 1) + 1].sum = a[(pos << 1) + 1].sum + a[pos].laz * (a[(pos << 1) + 1].r - a[(pos << 1) + 1].l + 1);
    a[pos].laz = 0;
}
inline void add(int tl,int tr,int c,int pos,int l,int r){
    if(l >= tl && r <= tr){
        a[pos].sum += (a[pos].r - a[pos].l + 1) * c;
        a[pos].laz += c;
        return;
    }
    if(a[pos].laz) push_down(pos);
    int mid = (l + r) >> 1;
    if(mid >= tl) add(tl,tr,c,pos << 1,l,mid);
    if(mid < tr) add(tl,tr,c,(pos << 1) + 1,mid + 1,r);
    a[pos].sum = a[pos << 1].sum + a[(pos << 1) + 1].sum;
}
inline int run(int pos,int res,int f){
    if(a[pos].l == a[pos].r){
        return a[pos].l;
    }
    if(a[pos].laz) push_down(pos);
    if(a[pos << 1].sum * f >= res) return run(pos << 1,res,f);
    else return run((pos << 1) + 1,res - a[pos << 1].sum * f,f);
}
void work(){
    scanf("%lld %lld %lld",&n,&q,&d);
    for(int i = 1;i <= n;i++){
        int x;
        cin >> b[i];
    }
    build(1,1,n);
    while(q--){
        int l,r,x;
        scanf("%lld %lld %lld",&l,&r,&x);
        add(l,r,x,1,1,n);
        int sum = a[1].sum;
        int ans = 0,f = 1;
        int nd = d;
        while(nd > 0){
            if(nd - sum <= 0) break;
            ans = ans + n;
            nd = nd - sum;
            sum <<= 1,f <<= 1;
        }
        int rans = run(1,nd,f) - 1;
        printf("%lld\n",ans+rans);
    }
    return;
}
signed main(){
//  freopen("wxyt2.in","r",stdin);
//  freopen("wxyt.out","w",stdout);
    int t = 1;
//  cin >> t;
    while(t--){
        work();
        if(t) cout << endl;
    }
    return 0;
}

::::

[ABC252F] Bread

题目信息

题目编号 AT_abc252_f
时间 2026.03.21
难度 \textcolor{yellow}{普及/提高-}
算法 贪心、优先队列
易错点 剩余面包也可能成为贪心要点
是否二刷 是

思路

这一道题其实有点像合并果子这道题,只不过是最后合并完还可能剩下一段面包,但是这一段面包什么时候出现也是贪心要点,所以也要把剩下的面包加入优先队列中。

::::success[AC code]

#include<bits/stdc++.h>
#define int unsigned long long
using 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
时间 2026.03.21
难度 \textcolor{green}{普及+/提高}
算法 贪心
易错点 思路走向错误、upper/lower选用错误
是否二刷 否

思路

这道题中,如果炸弹在一个区间的中间不好思考,不妨将炸弹平移到区间的最左侧,不改变爆炸区间,这样便于思考,对于一个位置受到的伤害应当是之前引爆的炸弹对于它的伤害+剩余血量受到的最小伤害,然后我们可以发现,炸弹的影响是连续性的,所以说可以使用二分的方式找到这个炸弹之后的第一个不受影响的位置,利用差分标记,以计算每一个位置受到伤害。

::::success[AC code]

#include<bits/stdc++.h>
#define int long long
using 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
时间 2026.03.21
难度 \textcolor{yellow}{普及/提高-}
算法 思维、递推
易错点 不开unsigned见祖宗
是否二刷 否

思路

处理一个很长的数字串较有难度,所以我们考虑拆分一下,每一位分析对于答案的贡献

::::success[AC code]

#include<bits/stdc++.h>
#define int long long
using 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
时间 2026.10.17
难度 \textcolor{yellow}{普及/提高-}
算法 大模拟、数学
易错点 注意细节
是否二刷 否

思路

题面已经提供了足够详细的信息,按照题面思路完成模拟即可

注意几个细节:

  1. 系数 x 如果满足 |x|=1,则不需用输出系数

  2. 判断到底什么时候取最大值

  3. 记得约分

  4. 注意正负号

::::success[AC code]

#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
时间 2026.10.17
难度 \textcolor{green}{普及+/提高-}
算法 贪心
易错点 每一次分配的廊桥一定是编号最小的空闲廊桥而不是随机分配
是否二刷 否

思路

考虑每一班飞机,每次选择编号最小的空闲飞机进行接机操作

不难发现,对于一组数据,如果增加了一个廊桥 k,并不影响前 1 ~ k-1 的接机顺序。

根据这一点可以进行贪心。维护两个小根堆堆 q1,q2 ,q1 维护每一趟廊桥空闲的最小时间,q2 维护在某一时间点时所有空闲廊桥的编号。

对于一趟飞机 i,现将所有在此时已经空闲的廊桥从 q1 中退出,装入 q2。如果此时 q2 非空,即有廊桥可以进行接机操作,则选择编号最小的廊桥 x,用sum_x 记录 x 的接机数量;否则就选择一个新廊桥进行接机。

分别对国内、国际进行此操作,然后前缀和计算并求出最大值即可。

::::success[AC code]

#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
时间 2026.10.17
难度 提高(青)
算法 反悔贪心
易错点 不开long long见祖宗
是否二刷 否

思路

对于一个任务 i,考虑先选择完成这项任务,如果此时超出原有限制,则考虑每一次删掉最耗费时间的任务,因为这些任务对后续任务的影响是最大的,最后留在堆中的任务就是互不影响的任务数目最大的方案,即为答案

::::success[AC code]

#include<bits/stdc++.h>
#define int long long
using 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
时间 2026.10.17
难度 提高(青)
算法 反悔贪心
易错点 不开long long见祖宗x2
是否二刷 否

思路

每一次进货后先考虑满足顾客,如果产生矛盾,则撤回对购买数量最多的顾客,这些顾客是对整体计算最大的部分,然后在维护堆权值的同时维护对应位置,最后输出即可。

::::success[AC code]

#include<bits/stdc++.h>
#define int long long
using 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
时间 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]

#include<bits/stdc++.h>
#define int long long
using 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
时间 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]

#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
时间 2026.09.16
难度 \textcolor{green}{普及+/提高-}
算法 广搜,最短路
易错点 不要尝试使用神秘算法(堆优化)
是否二刷 否

思路

这道题数据范围较小,可以考虑使用搜索。

我们设定搜索状态为 \{x,y,c,cnt,b\},分别表示坐标,颜色,所画金币数量,是否使用魔法。

使用类似最短路的思想,分类讨论是否可以使用魔法,如果可以更新下一位状态,则继续向其拓展,最后求出最小的花费即可

::::success[AC code]

#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
时间 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]

#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
时间 2026.09.15
难度 \textcolor{yellow}{普及}
算法 搜索
易错点 思路不要太复杂
是否二刷 否

思路

对于每一个 # 连通块,考虑进行 dfs,然后,正常计算面积和周长即可,最后按照比值完成比大小。

::::success[AC code]

#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
时间 2026.09.15
难度 \textcolor{yellow}{普及}
算法 搜索
易错点 选择正确的优化方式
是否二刷 否

思路

这道题,考虑使用 bfs + DP 的方式,在 bfs 的过程中,如果遇到已经访问过的位置,或者是到达时间上限的时候,就选择用 DP 转移而不是用 bfs。

::::success[AC code]

#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
时间 2026.09.23
难度 \textcolor{green}{普及+/提高-}
算法 搜索
易错点 注意相邻位置之间可以互相到达
是否二刷 否

思路

考虑两种情况。

情况1

直接向周围可以到达且没有走过的位置移动

情况2

拉下电闸,此时对应位置电灯被点亮,此时如果这个位置周围是有被经过的位置,则说明该位置可达,跳过中间所有过程,直接移动到该位置

::::success[AC code]

#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
时间 2025.10.04
难度 \textcolor{green}{普及+/提高-}
算法 meet in the middle
易错点 不会如何meet in the middle
是否二刷 否

思路

先对 a 数组排序,删去所有超出预算范围的价格,然后折半搜索,先处理前半部分,计算出所有可能的权值,记录到数组 rst 中。然后再查找后半部分,对于计算出的权值 q 用二分搜索的方式计算出方案数。

::::success[AC code]

#include<bits/stdc++.h>
#define int long long
using 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
时间 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]

#include<bits/stdc++.h>
#define int long long
using 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
时间 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]

#include<bits/stdc++.h>
#define int long long
using 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
时间 2025.09.28
难度 \textcolor{yellow}{普及}
算法 单调栈
易错点 不开long long见祖宗
是否二刷 否

思路

与其计算一头牛会看到几头牛,不如反向计算一头牛会被多少头牛看到。

维护一个单调递减的栈。对于每一个 i,考虑删去所有看不到它的 x,即 x \le a_i 的所有奶牛,此时剩余栈中的所有牛都能看到它,统计栈的大小和。

::::success[AC code]

#include<bits/stdc++.h>
#define int long long
using 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;
}

::::