8.6总结

· · 个人记录

8.6总结

得分情况与用时

题目名称 得分 用时(分钟) 备注
\text{number} \color{Green}100 \color{Cyan}5 /
\text{seat} \color{Green}100 \color{Yellow}30 /
\text{xor} \color{Yellow}70 \color{Yellow}30 数组开小了
\text{polygon} \color{YellowGreen}80 \color{Orange}40 打了暴力(不太暴力)
总计 350 105 /

题目总结

P14357 [CSP-J 2025] 拼数(\text{number})

题目大意

给定一个字符串s,请提取里面的所有数字并组成一个最大数字。

解题思路

维护一个桶t,将s中的数字加入桶并从大到小输出即可。

正确代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
string s;
int t[135];
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    freopen("number.in","r",stdin);
    freopen("number.out","w",stdout);
    cin>>s;
    for(int i=0;i<s.length();i++){
        if(s[i]>='0'&&s[i]<='9')t[s[i]-'0']++;//加入桶
    }
    for(int i=9;i>=0;i--){
        while(t[i]--)cout<<i;//输出t[i]个i
    }
    return 0;
}

个人总结

【防爆零这一块/.】

P14358 [CSP-J 2025] 座位(\text{seat})

题目大意

考场有n行m列的座位,给定n \times m位考生的成绩a,所有考生将按照成绩由高到低蛇形分配座位(方法如下图),求第1位考生(成绩为a_1)的座位为第几列第几行。

解题思路

观察到奇数列的移动方式为从上(1)到下(n),偶数列的移动方式为从下(n)到上(1)。所以我们每列每列地模拟。

我们维护三个变量:

  • x:当前纵坐标
  • y:当前横坐标
  • cnt:当前排名

奇数列的模拟方法如下:

while(x<=n){
    ma[x][y]=a[cnt];//记录该位置考生的成绩
    cnt++;//计数加1
    x++;//移动
}
x=n;//因为x此时会越界(n+1),所以将x设为n

偶数列的模拟方法同理。

最后枚举每个位置上的考生的成绩,找到第一位考生的位置即可。

正确代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int n,m,a[105],sc,ma[15][15];
bool cmp(int x,int y){
    return x>y;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    freopen("seat.in","r",stdin);
    freopen("seat.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n*m;i++){
        cin>>a[i];
    }
    sc=a[1];//记录分数
    sort(a+1,a+n*m+1,cmp);//降序排序
    int cnt=1,x=1,y=1;//维护三个变量
    while(y<=m){
        //偶数列
        if(y%2==0){
            while(x){
                ma[x][y]=a[cnt];
                cnt++;
                x--;
            }
            x=1;
        }
        //奇数列
        else{
            while(x<=n){
                ma[x][y]=a[cnt];
                cnt++;
                x++;
            }
            x=n;
        }
        y++;
    }
    //输出
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(ma[i][j]==sc){
                cout<<j<<" "<<i<<endl;
                return 0;
            }
        }
    }
    return 0;
}

个人总结

【依旧防爆零/.】,但是在代码逻辑上卡了很久,最后重新写出来了。

P14359 [CSP-J 2025] 异或和(\text{xor})

题目大意

一个长度位n的序列a_1,a_2,...,a_n,区间[l,r]的异或和为a_l \oplus a_{l+1} \oplus ... \oplus a_r。请选择择序列中尽可能多的不相交的区间,使每个区间的异或和均为k。

解题思路

这道题仔细看一下,就是最大不相交区间问题。

前言:贪心的“最大不相交区间问题”:

贪心策略:按右端点进行升序排序,每次选择满足条件且结束时间最少的区间,这样能给后面留出更多空间。

题目要求让我们在异或和为k的所有区间内,求最大不相交区间数量。

bool cmp(P x,P y){
    return x.r<y.r;
}

int rt=0;
for(auto i:p){
    if(i.l>rt){
        rt=i.r;
        ans++;
    }
}

40分思路:

用双重循环枚举所有区间,并用循环暴力计算异或和。

for(int i=1;i<=n;i++){
    for(int j=i;j<=n;j++){
        int tmp=0;
        for(int l=i;l<=j;l++)tmp^=a[l];//计算异或和
        if(tmp==k)p.push_back({i,j});//记录本次区间
    }
}

60分思路:

优化成双重循环,用异或前缀和数组计算异或和。

for(int i=1;i<=n;i++){
    for(int j=i;j<=n;j++){
        int tmp=s[j]^s[i-1];//计算异或和
        if(tmp==k)p.push_back({i,j});//记录本次区间
    }
}

100分思路:

我们要想办法优化成单层循环。

根据异或的性质,如果s_{i-1} \oplus s_j=k,那么s_{i-1}=s_j \oplus k。对于所有异或和为k的且以j结尾的区间,我们肯定要选长度最短的区间。

因此,维护一个t数组,t_k表示异或和为k的区间的最大左端点下标(最后一次出现的位置)。对于本次的s_j,我们要找到前面的s_j \oplus k(即s_{i-1})的最后一次出现的位置t_{s_j \oplus k}。所以本次的区间就是[t_{s_j \oplus k}+1,j]。最后还要更新t_{s_j}为j。

memset(t,-1,sizeof t);//记为-1以方便记录区间
t[0]=0;//初始化
for(int j=1;j<=n;j++){
    int i=t[(s[j]^k)];//s[j]^k最后一次出现的位置
    if(i!=-1)p.push_back({i+1,j});//记录本次区间
    t[s[j]]=j;//更新桶
}

这样记录所有区间的右端点是直接有序的,可以不用排序。

正确代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int n,k,a[500005],s[500005],ans,t[50000005];
struct P{
    int l,r;
};
vector<P>p;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    freopen("xor.in","r",stdin);
    freopen("xor.out","w",stdout);
    memset(t,-1,sizeof t);
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        s[i]=s[i-1]^a[i];//计算异或前缀和
    }
    //记录区间
    t[0]=0;
    for(int j=1;j<=n;j++){
        int i=t[(s[j]^k)];
        if(i!=-1)p.push_back({i+1,j});
        t[s[j]]=j;
    }
    //求出答案
    int rt=0;
    for(auto i:p){
        if(rt<i.l){
            rt=i.r;
            ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}

个人总结

一直在想优化,结果数组开小了,要不然就AC了。

P14360 [CSP-J 2025] 多边形(\text{polygon})

题目大意

有n根小木棍,第i根小木棍长为a_i,从这些小木棍中选出3 \sim n根,使所有小木棍的长度之和大于所有小木棍的长度最大值的两倍。求这样选择的方案数。

解题思路

40分思路:

直接用\text{DFS}选和不选即可。

void dfs(int x,int cnt,int sum,int maxa){//cnt:选的个数 maxa:选中的最大值 sum:选中的数的和
    if(x>n){//选完后
        if(cnt>=3&&maxa*2<sum)ans++;//满足条件:答案加1
        return;
    }
    dfs(x+1,cnt+1,sum+a[x],max(maxa,a[x]));//选
    dfs(x+1,cnt,sum,maxa);//不选
}

暴搜挂着机,打表出省一

80分思路:

注:设s为所有棍木长度之和。

【\text{DFS}依旧太慢了/.】,可以用01背包。

100分思路:

这样dp数组要开到一亿(2 \times 5 \times 10^7),显然不彳亍。

注意到a_i \le 5000,也就是说木棍长度不大于5000。所以当选中的木棍总长度>5000,就一定可以构成合法多边形。

所以,我们维护一个变量super,表示让数字总和>5000的满足条件的方案数。如果之前有方案选中的木棍总长度>5000,就将此方案加入super中,因为每根木棍可选可不选,所以处理完后要将super翻倍。

(没有部分\texttt{Code},想看的请看下面)

正确代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int Mod=998244353;
int n,a[5005],dp[2][5005],ans,super;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    freopen("polygon.in","r",stdin);
    freopen("polygon.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    //初始化
    sort(a+1,a+n+1);
    dp[0][0]=1;
    for(int i=1;i<=n;i++){
        //更新答案
        for(int j=a[i]+1;j<=5000;j++){
            ans=(ans+dp[0][j])%Mod;
        }
        ans=(ans+super)%Mod;
        super=(super*2)%Mod;//将super翻倍
        //将木棍总长度>5000的方案加入super中
        for(int j=5001-a[i];j<=5000;j++){
            super=(super+dp[0][j])%Mod;
        }
        //状态转移
        for(int j=0;j<=5000;j++){
            if(j>=a[i])dp[1][j]=(dp[0][j]+dp[0][j-a[i]])%Mod;
            else dp[1][j]=dp[0][j];
        }
        for(int j=0;j<=5000;j++){
            dp[0][j]=dp[1][j];
        }
    }
    cout<<ans<<endl;
    return 0;
}

个人总结

之前依旧讲过,但没写出来。

666还有入AC了

整体总结

$\text{T3}$数组开小了,要不然就AC了。 虽然$350$分足够一等奖了(分数线$245$分),但之前讲过(~~我不满意~~)。 $\textsf{\textbf{By}}$ [$\textsf{\textbf{JWM918}}$](https://www.luogu.com.cn/user/1268845)