2025年11月20日 廖老师第九次课

· · 题解

题目信息

题目 难度 一开始的分数 结束时的分数
T696513 [ABC432B] Permute to Minimize 普及− 100 100
T696508 [CF2166A] Same Difference 普及− 0 100
T696683 粉兔 普及− 0 100
T684839 植物大战僵尸 普及− 0 100

T1 T696513 [ABC432B] Permute to Minimize

题意分析

给你一个n,要求将n重新排列并且使其没有前导零。

思路

  1. 输入n。
  2. 将n分解为一个个数(x)。
  3. 将x按升序排序。
  4. 重新排列。
  5. 输出结果。

    代码

    #include<bits/stdc++.h>
    #define int long long
    #define endl '\n'
    using namespace std;
    int x[10];
    signed main()
    {
     ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
     int n;
     //1. 输入n。
     cin>>n;
     //2. 将n分解为一个个数(x)。
     string a=to_string(n);
     for(int i=0;i<a.size();i++)x[i+1]=(a[i]-'0');
     //3. 将x按升序排序。
     sort(x+1,x+a.size()+1);
     //4. 重新排列。
     for(int i=a.size();i>=1;i--)
     {
         if(x[i]!=0&&x[i-1]==0&&i-1!=0)swap(x[i],x[i-1]);
     }
     //5. 输出结果。
     for(int i=1;i<=a.size();i++)cout<<x[i];
     return 0;
    }

    T2 T696508 [CF2166A] Same Difference

    题意分析

    有t组测试样例,给你一个长度为n的字符串s,要求求出使每个字符相同所需的最少操作次数。

    思路

  6. 输入t。
  7. 输入n和s。
  8. 进行计数。
  9. 输出结果。

    代码

    #include<bits/stdc++.h>
    #define int long long
    #define endl '\n'
    using namespace std;
    signed main()
    {
     ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
     int t;
     //1. 输入t。
     cin>>t;
     while(t--)
     {
         int n,sum=0;
         string s;
         //2. 输入n和s。
         cin>>n>>s;
         //3. 进行计数。
         for(int i=0;i<n-1;i++)
         {
             if(s[i]!=s[n-1])sum++;
         }
         //4. 输出结果。
         cout<<sum<<endl;
     }
     return 0;
    }

    T3 T696683 粉兔

    题意分析

    给你一个n\times m的矩形,矩形中有N只雄粉兔和M只雌粉兔,求有多少只粉兔至少有三条逃跑通道。

    思路

  10. 输入n,m,N和M。
  11. 用循环看粉兔的周围有没有异性粉兔。
  12. 进行计数。
  13. 输出合格的粉兔合格的粉兔的数量。

    代码

    #include<bits/stdc++.h>
    #define int long long
    #define endl '\n'
    using namespace std;
    char a[505][505];
    signed main()
    {
     //1. 输入n,m,N和M。
     ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
     int n,m,N,M,cnt=0;
     cin>>n>>m>>N>>M;
     for(int i=1;i<=n;i++)
     {
         for(int j=1;j<=m;j++)
         {
             cin>>a[i][j];
         }
     }
     for(int i=1;i<=n;i++)
     {
         for(int j=1;j<=m;j++)
         {
           //2. 用循环看粉兔的周围有没有异性粉兔。
             if(a[i][j]=='F')
             {
                 int f1=1,f2=1,f3=1,f4=1;
                 for(int l=1;l<=j;l++)
                 {
                     if(a[i][l]=='M')f1=0;
                 }
                 for(int l=j;l<=m;l++)
                 {
                     if(a[i][l]=='M')f2=0;
                 }
                 for(int h=1;h<=i;h++)
                 {
                     if(a[h][j]=='M')f3=0;
                 }
                 for(int h=i;h<=n;h++)
                 {
                     if(a[h][j]=='M')f4=0;
                 }
           //3. 进行计数。
                 if(f1+f2+f3+f4>=3)cnt++;
             }
             if(a[i][j]=='M')
             {
                 int f1=1,f2=1,f3=1,f4=1;
           //2. 用循环看粉兔的周围有没有异性粉兔。
                 for(int l=1;l<=j;l++)
                 {
                     if(a[i][l]=='F')f1=0;
                 }
                 for(int l=j;l<=m;l++)
                 {
                     if(a[i][l]=='F')f2=0;
                 }
                 for(int h=1;h<=i;h++)
                 {
                     if(a[h][j]=='F')f3=0;
                 }
                 for(int h=i;h<=n;h++)
                 {
                     if(a[h][j]=='F')f4=0; 
                 }
           //3. 进行计数。
                 if(f1+f2+f3+f4>=3)cnt++;
             }
         }
     }
     //4. 输出合格的粉兔合格的粉兔的数量。
     cout<<cnt;
     return 0;
    }

    T4 T684839 植物大战僵尸Permute to Minimize

    题意分析

    给你n只zombie,告诉你每只zombie出现的时间以及在出现的行数(可能会同时出现同一位置的僵尸),问至少需要多少坚果才能消灭所有的zombie。

    思路

  14. 设st结构体。
  15. 设cmp结构体排序函数。
  16. 输入n和每只zombie出现的时间以及在出现的行数。
  17. 这一行没扔过坚果保龄球。
  18. 这一行扔过坚果保龄球。
  19. 输出结果。

    代码

    #include<bits/stdc++.h>
    #define int long long
    #define endl '\n'
    using amespace std;
    //1. 设st结构体。
    struct st
    {
     int hs,sj;
    }s[2005];
    //2. 设cmp结构体排序函数。
    int cmp(st s1,st s2)
    {
     if(s1.hs==s2.hs)return s1.sj<s2.sj;
     return s1.hs<s2.hs;
    }
    int t[10]; 
    signed main()
    {
     ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
     //3. 输入n和每只zombie出现的时间以及在出现的行数。
     int n;
     cin>>n;
     for(int i=1;i<=n;i++)cin>>s[i].hs>>s[i].sj;
     sort(s+1,s+n+1,cmp);
     int cnt=0;
     for(int i=1;i<=n;i++)
     {
         //4. 这一行没扔过坚果保龄球。
         if(t[s[i].hs]==0)
         {
             t[s[i].hs]=s[i].sj;
             cnt++;
         }
         //5. 这一行扔过坚果保龄球。
         else
         {
             if(s[i].sj-t[s[i].hs]>=60)
             {
                 t[s[i].hs]=s[i].sj;
                 cnt++;
             }
         }
     }
     //6. 输出结果。
     cout<<cnt;
     return 0;
    }