2025寒假天梯赛训练2

这是寒假第二场天梯赛训练

出了大问题,有个大模拟没写出来,样例通过测试点全WA。目前仍未解决。大模拟是一个大坎。单纯的看代码能力。其余的有并查集和dp,这两个解决了。还有一个图论题可以用并查集骗分没有骗到。一直很担心图论和dp, 牛客有题单给我写。我还是要继续补题。

L1-1 谢谢卡尔!

复习一下字符输出。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int, int> PII;
const int N = 2e5 + 7;
const int inf = LLONG_MAX / 10;
const int mod = 998244353;
// const int mod = 1e9 + 7;
#define x first
#define y second
//二分暴龍龍

void miaojiachun()
{
    cout<<"谢谢卡尔!\\(>_<)/"<<endl;
}
signed main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin >> ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

L1-2 现在是,幻想时间!

这个人和我一样嘿,不嘻嘻。
就是浮点数相除就好了。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int, int> PII;
const int N = 2e5 + 7;
const int inf = LLONG_MAX / 10;
const int mod = 998244353;
// const int mod = 1e9 + 7;
#define x first
#define y second
//二分暴龍龍

void miaojiachun()
{
    double n,t;
    cin>>n>>t;
    printf("%.3lf",n/t);
}
signed main()
{
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin >> ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

L1-3 你是来陪可莉炸鱼的吗?

简单模拟

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int, int> PII;
const int N = 2e5 + 7;
const int inf = LLONG_MAX / 10;
const int mod = 998244353;
// const int mod = 1e9 + 7;
#define x first
#define y second
//二分暴龍龍

void miaojiachun()
{
    int n;
    cin>>n;
    vector<int>a(n+1);
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    int sum=0;
    for(int i=1;i<=n;i++){
        if(a[i]<100){
            sum++;
        }if(100<=a[i] and a[i]<200){
            sum+=2;
        }if(200<=a[i] and a[i]<300){
            sum+=5;
        }if(300<=a[i] and a[i]<400){
            sum+=10;
        }if(400<=a[i]){
            sum+=15;
        }
    }
    cout<<sum<<endl;
}
signed main()
{
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin >> ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

L1-4 扫雷游戏

按照题意二层循环加查找求值就好了。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int, int> PII;
const int N = 2e5 + 7;
const int inf = LLONG_MAX / 10;
const int mod = 998244353;
// const int mod = 1e9 + 7;
#define x first
#define y second
//二分暴龍龍
char c[15][15];
int dx[8]={0,0,-1,1,-1,-1,1,1};
int dy[8]={1,-1,0,0,-1,1,-1,1};
int a[15][15];
void miaojiachun()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>c[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(c[i][j]=='.'){
                for(int z=0;z<8;z++){
                    int x=i+dx[z];
                    int y=j+dy[z];
                    if(x>=1 and x<=n and j>=1 and j<=m and c[x][y]=='*'){
                        a[i][j]++;
                    }
                }
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(a[i][j]==0){
                cout<<c[i][j];
            }else{
                cout<<a[i][j];
            }
        }
        cout<<endl;
    }
}
signed main()
{
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin >> ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

L1-5 史莱姆

遇到不一样的字符再储存,否则就跳过。输出大小和字符串。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int, int> PII;
const int N = 2e5 + 7;
const int inf = LLONG_MAX / 10;
const int mod = 998244353;
// const int mod = 1e9 + 7;
#define x first
#define y second
//二分暴龍龍
void miaojiachun()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    s=" "+s;
    string ans="";
    for(int i=1;i<=n;i++){
        if(s[i]==s[i-1]){
            continue;
        }else{
            ans+=s[i];
        }
    }
    cout<<ans.size()<<endl;
    cout<<ans<<endl;
}
signed main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin >> ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

L1-6 加密通信

由于模数错了一发,这个要按长度取模。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int, int> PII;
const int N = 2e5 + 7;
const int inf = LLONG_MAX / 10;
const int mod = 998244353;
// const int mod = 1e9 + 7;
#define x first
#define y second
//二分暴龍龍
int a[27];
void miaojiachun()
{
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    s+=s;
    s=" "+s;
    for(int i=0;i<26;i++){
        cin>>a[i];
    }
    string ans="";
    for(int i=n-(k%n)+1;i<=n-(k%n)+n;i++){
        cout<<a[s[i]-'a'];
    }
    // cout<<ans<<endl;
}
signed main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin >> ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

L1-7 字符操作

记录当前的反转次数,如果是偶数次反转相当于没反转,如果是奇数次反转。那么就更改所给 a, b。小于 n 的让他加 n。大于 n 的让他减 n。就相当于反转后按原来给定 a, b 修改。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int, int> PII;
const int N = 2e5 + 7;
const int inf = LLONG_MAX / 10;
const int mod = 998244353;
// const int mod = 1e9 + 7;
#define x first
#define y second
//二分暴龍龍
void miaojiachun()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    s=" "+s;
    int k;
    cin>>k;
    int sum=0;
    while(k--){
        int t,a,b;
        cin>>t>>a>>b;
        if(t==1){
            if(sum%2==0){
                swap(s[a],s[b]);
            }else{
                if(a>n){
                    a-=n;
                }else{
                    a+=n;
                }if(b>n){
                    b-=n;
                }else{
                    b+=n;
                }
                swap(s[a],s[b]);
                // cout<<a<<" "<<b<<endl;
            }
        }else{
            sum++;
        }   
    }
    if(sum%2==0){
        for(int i=1;i<=2*n;i++){
            cout<<s[i];
        }
    }else{
        for(int i=n+1;i<=2*n;i++){
            cout<<s[i];
        }
        for(int i=1;i<=n;i++){
            cout<<s[i];
        }
    }
}
signed main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin >> ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

L1-8 vivo50!

简单排序模拟,不足为惧。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int, int> PII;
const int N = 2e5 + 7;
const int inf = LLONG_MAX / 10;
const int mod = 998244353;
// const int mod = 1e9 + 7;
#define x first
#define y second
//二分暴龍龍
struct stu{
    string name;
    double jd;
    int dy;
    int zy;
    int g;
    int k;
    double zong;
};
bool cmp(stu a,stu b){
    if(a.zong==b.zong){
        return a.name<b.name;
    }
    return a.zong>b.zong;
}
void miaojiachun()
{
    int n;
    cin>>n;
    int k;
    cin>>k;
    vector<stu>a(n+1);
    vector<stu>b;
    for(int i=1;i<=n;i++){
        cin>>a[i].name>>a[i].jd>>a[i].dy>>a[i].zy>>a[i].g;
        // a[i].k=i;
        a[i].zong=((a[i].jd*10+50)+a[i].zy)*0.7+0.3*min(100ll,a[i].dy+70ll);
        // cout<<a[i].zong<<endl;
        if(a[i].g==1){
            b.push_back(a[i]);
        }
    }

    sort(b.begin(),b.end(),cmp);
    // cout<<1<<endl;
    int num=1;
    b[0].k=1;
    // cout<<b[0].name<<endl;
    for(int i=1;i<b.size();i++){
        if(b[i].zong!=b[i-1].zong){
            num=i+1;
            b[i].k=num;
        }else{
            b[i].k=b[i-1].k;
        }
    }
    for(int i=0;i<b.size();i++){
        if(b[i].k<=k){
            cout<<b[i].k<<" "<<b[i].name<<" ";
            printf("%.1lf\n",b[i].zong);
        }
    }
}
signed main()
{
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin >> ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

L2-1 游戏圈

并查集的板子。来查找两个人是不是属于同一关系网。在输出关系网的个数。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int, int> PII;
const int N = 2e5 + 7;
const int inf = LLONG_MAX / 10;
const int mod = 998244353;
// const int mod = 1e9 + 7;
#define x first
#define y second
//二分暴龍龍
int fa[N*5];
int find(int i)
{
	if(fa[i]==i)
	{
		return i;
	}
	else
	{
		fa[i]=find(fa[i]);
		return fa[i];
	}
}
void unionn(int i,int j)
{
	int i_=find(i);
	int j_=find(j);
	fa[i_]=j_;
}
void miaojiachun()
{
    int n,m,q;
    cin>>n>>m>>q;
	for(int i=1;i<=n;i++){
		fa[i]=i;
	}
	while(m--){
		int x,y;
		cin>>x>>y;
		unionn(x,y);
	}
    while(q--){
        int x,y;
        cin>>x>>y;
        if(find(x)==find(y))
		{
			cout<<"yes"<<endl;
		}else
		{
			cout<<"no"<<endl;
		}
    }
    int sum=0;
    for(int i=1;i<=n;i++){
        if(fa[i]==i){
            sum++;
        }
    }
    cout<<sum<<endl;
}
signed main()
{
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin >> ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

L2-2 组套题

难死我了。题目就很难读,我搞出样例之后一看测试点全WA,想死了。用了一个半小时来搞大模拟。代码实现能力的问题。而且我也想逃避之后出现的搜索和图论题,导致在这里卡死了。

点击查看代码
#include<bits/stdc++.h>
#define endl '\n'

using namespace std;

const int N = 1e3 + 10;

typedef long long ll;
int n,m,a[N];
struct Juan{
    int tao, tinum;
};
bool cmp(Juan a, Juan b)
{
        if(b.tao == a.tao) return b.tinum > a.tinum;
        return b.tao > a.tao;    
}
vector<Juan> ans[N], cnt;
queue<Juan> q[N];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n;
    for(int i = 1; i <= 10; i++)
      cin >> a[i];     
    for(int i = 1; i <= n ; i ++) 
    {
        cin >> m;
        for(int j = 0; j < m ;j++)
        {
            string s;
            cin >> s;
            int x = 1;
            int a1,b1;
            a1 = b1 = 0;
            while(s[x] >= '0' && s[x] <= '9')
            {
            a1 = a1 * 10 + (s[x] - '0');
            x ++ ;    
            }
            x++;
            while(s[x] >= '0' && s[x] <= '9')
            {
            b1 = b1 * 10 + (s[x] - '0');
            x ++ ;    
            }
            if(a[a1]) 
            {
                a[a1] -- ;
                ans[a1].push_back({i, b1});
            }            
            else
               q[a1].push({i, b1});
        }
    }
    for(int i = 1 ;i <= 10; i++)
    {
        while(a[i])
        {
           bool f = 0;
           for(int j = i + 1; j <= 10; j++)
           {
           if(q[j].size())
             {
                 ans[j].push_back(q[j].front());
                 q[j].pop();
                 a[i] --;
                 f = 1;
                 break;
             }
           }
         if(f) continue;
         for(int j = i - 1; j >= 1; j--)
         {
             if(q[j].size())
             {
                 ans[j].push_back(q[j].front());
                 q[j].pop();
                 a[i] --;
                 f = 1;
                 break;
             }
         }
        }
     } 
     for(int i = 1; i <= 10; i++)
      if(ans[i].size())
      {
          sort(ans[i].begin(), ans[i].end(), cmp);
          for(Juan j : ans[i])
           cnt.push_back(j);
      }
    for(int i = 0 ;i < cnt.size() ; i++)
    {
        if(i) printf(" ");
        printf("%d-%d",cnt[i].tao, cnt[i].tinum);        
    }
    return 0;
}

L2-3 简单的数数

使用 dp[i][j] 表示前 i 个元素可以通过若干操作最终合并为 j 的方案数。

只有第一个元素能直接合并为自己,因此 dp[0][A[0]] = 1

状态转移:

  • 枚举第 i−1 步的所有可能结果 prev。

  • 对于当前的元素 A[i],计算两种操作的结果:

    • 操作1:(prev+A[i])%10
    • 操作2:(prev×A[i])%10
  • 将前一步的方案数累加到当前状态中。

点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int, int> PII;
const int N = 2e5 + 7;
const int inf = LLONG_MAX / 10;
const int mod = 998244353;
// const int mod = 1e9 + 7;
#define x first
#define y second
//二分暴龍龍

void miaojiachun()
{
    int n;
    cin >> n;

    vector<int> A(n);
    for (int i = 0; i < n; ++i) {
        cin >> A[i];
    }
    vector<vector<int>> dp(n, vector<int>(10, 0));

    dp[0][A[0]] = 1;

    for (int i = 1; i < n; ++i) {
        for (int prev = 0; prev < 10; ++prev) {
            if (dp[i - 1][prev] > 0) {
                int sum = (prev + A[i]) % 10; 
                int pr = (prev * A[i]) % 10;
                dp[i][sum] = (dp[i][sum] + dp[i - 1][prev]) % mod;
                dp[i][pr] = (dp[i][pr] + dp[i - 1][prev]) % mod;
            }
        }
    }
    for (int k = 0; k < 10; ++k) {
        cout << dp[n - 1][k] << endl;
    }

}
signed main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int ING = 1;
    // cin >> ING;
    while (ING--)
    {
        miaojiachun();
    }
    return 0;
}

that's not all.

posted @ 2025-01-26 12:42  miao-jc  阅读(18)  评论(0)    收藏  举报