7.28总结

· · 个人记录

7.28总结

得分情况与用时

题目名称 得分 用时(分钟) 备注
\text{number} \color{Green}100 \color{Green}10 /
\text{water} \color{Green}100 \color{Yellow}30 /
\text{seat} \color{Gray}0 \color{Yellow}30 题面看懂了,但不会写
\text{club} \color{Red}20 \color{Yellow}30 打了暴力
总计 220 100 /

题目总结

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

题目大意

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

解题思路

显然这是一个排序题。

我们维护一个桶t,存储每个数字出现的个数。存储完后从大到小输出即可。

正确代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int t[15];
string s;
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--){
        //输出t[i]个i(当前数)
        for(int j=t[i];j>=1;j--){
            cout<<i;
        }
    }
    return 0;
}

个人总结

肥肠简单的一道题,好题多出。

P1190 [NOIP 2010 普及组] 接水问题(\text{water})

题目大意

有n个人要接水,第i个人的接水时间为w_i。现有m个水龙头。这些人会按照顺序依次接水。当有人接完水后,下一个人将会立刻进入空位接水。求所有同学接完水的时间。

解题思路

显然这是一道模拟题。

我们维护一个变量t表示将要接替的人的位置(初始为m)并模拟接水的过程(每1时间)。当有人接完水后(!w[i]),下一个人立刻接替位置并更新t(t++; w[i]=w[t];)。

当后面的所有人接替完前面人的位置后,模拟的循环结束。最后答案就是之前循环的次数加剩下的人的接水时间(max({w[1],w[2],w[3]...w[m]}))。

正确代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int n,m,w[100005],ans;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    freopen("water.in","r",stdin);
    freopen("water.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>w[i];
    }
    int t=m;//t:将要接替的人的位置
    while(t<n){
        //模拟每1时间的接水过程
        for(int i=1;i<=m;i++){
            w[i]--;//接1点水
            if(!w[i]){//接完了
                t++;//下一个人来
                w[i]=w[t];//接替位置
            }
        }
        ans++;//更新答案
    }
    //求剩下的人的接水时间
    int maxw=0;
    for(int i=1;i<=m;i++){
        maxw=max(maxw,w[i]);
    }
    cout<<ans+maxw<<endl;
    return 0;
}

个人总结

其实之前写过(依旧忘了),想到了要全部模拟接水的过程,就是卡在计算答案的逻辑上。最后还是写出来了。

P1056 [NOIP 2008 普及组] 排座椅(\text{seat})

题目大意

有一个N \times M的矩阵,矩阵上有D对相邻的点。现有K条横切线和L条纵切线,它们可以任意放置,求让相邻的点分开最多的放置方案。

解题思路

每条线之间是不受影响的,贡献可以单独计算。所以我们维护两个数组row与col,row_i与col_i分别表示第i条横切线可以隔开的点数量与第i条纵切线可以隔开的点数量。输入坐标后判断它们的纵坐标是否相等,相等说明横向相邻,否则纵向相邻。判断后把当前分开线的贡献加1(因为后面要排序,所以还要记录编号)。最后先按贡献降序排序,再按选中的编号升序排序即可。

正确代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int n,m,k,l,d,x,y,p,q;
struct P{
    int id,tot;//id:编号 tot:分开总数
}row[10005],col[10005];
bool cmp(P x,P y){
    return x.tot>y.tot;
}
bool cmpid(P x,P y){
    return x.id<y.id;
}
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>>k>>l>>d;
    for(int i=1;i<=d;i++){
        cin>>x>>y>>p>>q;
        if(x!=p){//从行分开
            row[min(x,p)].id=min(x,p);//记录编号
            row[min(x,p)].tot++;//记录分开总数
        }
        else{//从行分开
            col[min(y,q)].id=min(y,q);//记录编号
            col[min(y,q)].tot++;//记录分开总数
        }
    }
    //按分开的数量降序排序
    sort(row+1,row+n+1,cmp);
    sort(col+1,col+m+1,cmp);
    //按编号升序排序
    sort(row+1,row+k+1,cmpid);
    sort(col+1,col+l+1,cmpid);
    //输出答案
    for(int i=1;i<=k;i++){
        cout<<row[i].id<<" ";
    }
    cout<<endl;
    for(int i=1;i<=l;i++){
        cout<<col[i].id<<" ";
    }
    return 0;
}

个人总结

花了30分钟,题目看懂了,却不太会写(这不是昨天的剧情吗。。。)。早知道尝试一下了,唉!

P14361 [CSP-S 2025] 社团招新(\text{club})

题目大意

### 解题思路 **40分思路**: 直接用$\text{DFS}$选和不选加剪枝,时间复杂度约$O(3^n \times t)$。 **100分思路**: 先把每个人分配到他最喜欢的社团,可以开$3$个`vector`,记录每个社团的人数。如果人数没有超过$\frac{n}{2}$,那么直接计算最大的满意度。否则留下$\frac{n}{2}$个人,剩下的人就调到他第二喜欢的社团。 那么需要解决以下问题: **1.输入时的分配方法**:在输入的时候把它们按照满意度升序排序,还要记录对应社团的编号,然后放到它们最喜欢的社团里去。 **2.超过人数限制时的调整方法**:我们要调走满意度损失最小的人,则排序规则按照最喜欢的社团满意度减第二喜欢的社团满意度进行降序排序,把后面的人调到第二喜欢的社团即可 ### 正确代码 ```cpp line-numbers #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int t,n,N; struct P{ int num,id; }a[100005][3]; vector<int>q[3]; bool cmp1(P x,P y){//按照满意度升序排序 return x.num<y.num; } bool cmp2(int x,int y){//按照最喜欢的社团满意度减第二喜欢的社团满意度降序排序 int xx=a[x][2].num-a[x][1].num; int yy=a[y][2].num-a[y][1].num; return xx>yy; } int f(int id1){//计算第id1个社团的贡献 int sum=0;//sum:第id1个社团的贡献值 if(q[id1].size()<=N){//没有超限 //直接计算最大满意度 for(auto i:q[id1])sum+=a[i][2].num; return sum; } sort(q[id1].begin(),q[id1].end(),cmp2);//先排序 //前n/2个取最大值 for(int i=0;i<N;i++){ int idx=q[id1][i]; sum+=a[idx][2].num; } //剩余的取次大值 for(int i=N;i<q[id1].size();i++){ int idx=q[id1][i]; sum+=a[idx][1].num; } return sum; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); freopen("club.in","r",stdin); freopen("club.out","w",stdout); cin>>t; while(t--){ q[0].clear(); q[1].clear(); q[2].clear(); cin>>n; N=(n>>1);//提前计算n/2 for(int i=1;i<=n;i++){ cin>>a[i][0].num>>a[i][1].num>>a[i][2].num; a[i][0].id=0;a[i][1].id=1;a[i][2].id=2;//记录编号 sort(a[i],a[i]+3,cmp1);//排序 q[a[i][2].id].push_back(i);//分配到最喜欢的社团 } int res=0; res+=f(0)+f(1)+f(2);//计算三个贡献和 cout<<res<<endl; } return 0; } ``` ### 个人总结 很快写出了$20$分代码,但不知道怎么剪枝,如果想一下剪枝就好了。 ~~™竟然是S组t1~~ ## 整体总结 在$\text{T2}$上卡了很久,卡在计算答案的逻辑上。其实后面还有很多时间,要是能多想一下$\text{T3}$的思路和$\text{T4}$暴搜的剪枝方法就好了。$ε=(´ο`*)))$ 唉! By [JWM918](https://www.luogu.com.cn/user/1268845)