8.6总结
8.6总结
得分情况与用时
| 题目名称 | 得分 | 用时(分钟) | 备注 |
|---|---|---|---|
| 数组开小了 | |||
| 打了暴力(不太暴力) | |||
| 总计 |
题目总结
P14357 [CSP-J 2025] 拼数(\text{number} )
题目大意
给定一个字符串
解题思路
维护一个桶
正确代码
#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} )
题目大意
考场有
解题思路
观察到奇数列的移动方式为从上(
我们维护三个变量:
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} )
题目大意
一个长度位
解题思路
这道题仔细看一下,就是最大不相交区间问题。
前言:贪心的“最大不相交区间问题”:
贪心策略:按右端点进行升序排序,每次选择满足条件且结束时间最少的区间,这样能给后面留出更多空间。
题目要求让我们在异或和为
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分思路:
我们要想办法优化成单层循环。
根据异或的性质,如果
因此,维护一个
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} )
题目大意
有
解题思路
40分思路:
直接用
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分思路:
注:设
【
- 预处理:对
a 数组排序,确保a_i 为最大值时前面的木棍长度都不超过a_i ; - 状态设计:
dp[0][j]表示前i 根木棍中选出若干根长度为j 的方案数; - 状态转移:
01 背包求方案数模版:if(j>=a[i])dp[1][j]=(dp[0][j]+dp[0][j-a[i]])%Mod; else dp[1][j]=dp[0][j];; - 初始状态:
dp[0][0]=1; - 答案:每次
dp 中j>a_i 的状态累加到答案中(可以与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<=s;j++){ ans=(ans+dp[0][j])%Mod; } //状态转移 for(int j=0;j<=s;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<=s;j++){ dp[0][j]=dp[1][j]; } }
100分思路:
这样
注意到
所以,我们维护一个变量super,表示让数字总和super中,因为每根木棍可选可不选,所以处理完后要将super翻倍。
(没有部分
正确代码
#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了