当前位置: 首页 > news >正文

天梯赛训练 补题

L3-029 还原文件

暴力dfs ,赛时没敲出来

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll __int128
#define PII pair<int,int>
#define PSI pair<string,int>
#define PVI pair<vector<int>,int>
#define endl '\n'
#define mk make_pair
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
#define Pi acos(-1.0)
#define ull unsigned long long
int ls(int p){ return (p << 1) ;}
int rs(int p){ return (p << 1 | 1) ;}
inline int lowbit(int x) { return x & (-x) ;}
//(A - B) % mod =((A % mod) - (B % mod) +mod) % mod
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
const double eps = 1e-8;

int n ,m;
int a[N];
vector<int> g[N] ,ans;
bool vis[N] ,ok;
void dfs(int x) {
    if (ok) return;
    if (x >= n) {
        for (int i = 0 ; i < ans.size() ; i++) {
            if (!i) cout << ans[i];
            else cout << " " << ans[i];
        }
        return;
    }
    for (int i = 1 ; i <= m ; i++) {
        if (vis[i]) continue;
        bool f = 0;
        for (int j = 0 ; j < g[i].size() ; j++) {
            if (a[x + j] != g[i][j]) {
                f = 1;
                break;
            }
        }
        if (f) continue;
        vis[i] = 1;
        ans.push_back(i);
        dfs(x + g[i].size() - 1);
        vis[i] = 0;
        ans.pop_back();
    }
}
void solve() {
    cin >> n;
    for (int i = 1 ; i <= n ; i++) cin >> a[i];
    cin >> m;
    for (int i = 1 ; i <= m ; i++) {
        int k;
        cin >> k;
        while (k--) {
            int x;
            cin >> x;
            g[i].push_back(x);
        }
    }
    dfs(1);
}
signed main(){
    IOS
    int O_O = 1;
    // cin >> O_O;
    while(O_O--) solve();
}

 L1-064 估值一亿的AI核心代码

大模拟,字符串的,之前写过,但还是有点复杂,赛时只拿了18分

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll __int128
#define PII pair<int,int>
#define PSI pair<string,int>
#define PVI pair<vector<int>,int>
#define endl '\n'
#define mk make_pair
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
#define Pi acos(-1.0)
#define ull unsigned long long
int ls(int p){ return (p << 1) ;}
int rs(int p){ return (p << 1 | 1) ;}
inline int lowbit(int x) { return x & (-x) ;}
//(A - B) % mod =((A % mod) - (B % mod) +mod) % mod
const int mod = 1e9 + 7;
const int N = 1e5 + 6;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};



void solve(){
    string s ,c;
    vector<string> ans;
    getline(cin,s);
    int n = s.length();
    cout << s << endl;
    for(int i = 0 ; i < n ; i++){
        if(!i and s[i] == ' '){
            while(i < n and s[i] == ' ') i++;
        }
        if(s[i] == ' ' and i + 1 < s.size() and isalnum(s[i + 1])) c += s[i];
        if(s[i] != ' ') {
            if(!isalnum(s[i])) c += " ";
            c += s[i];
        }
    }
    n = c.size();
    for(int i = 0 ; i < n ; i++) {
        if(c[i] == '?') c[i] = '!';
        if(c[i] >= 'A' and c[i] <= 'Z' and c[i] != 'I') c[i] += ('a' - 'A');
    }
    // cout << c << endl;
    string x;
    for(int i = 0 ; i < n ; i++) {
        if(c[i] != ' ') x += c[i];
        else ans.push_back(x) ,x.clear();
    }
    if(x.size()) ans.push_back(x);
    cout << "AI:";
    if(ans.empty() or !isalnum(ans[0][0])) cout << " ";
    for(int i = 0 ; i < ans.size() ; i++) {
        if(!isalnum(ans[i][0])) cout << ans[i];
        else if(ans[i] == "can" and i + 1 < ans.size() and ans[i + 1] == "you") {
            cout << " I can";
            i++;
        }else if(ans[i] == "could" and i + 1 < ans.size() and ans[i + 1] == "you") {
            cout << " I could";
            i++;
        }else if(ans[i] == "me" or ans[i] == "I") {
            cout << " you";
        }else cout << " " << ans[i];
    }
    cout << endl;
}
signed main(){
    // IOS
    int O_O = 1;
    cin >> O_O;
    getchar();
    while(O_O--) solve();
}

L2-047 锦标赛

树,赛时没有想到这种做法,其实就是左右子树互相判断建树就能得到答案了

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll __int128
#define PII pair<int,int>
#define PSI pair<string,int>
#define PVI pair<vector<int>,int>
#define endl '\n'
#define mk make_pair
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
#define Pi acos(-1.0)
#define ull unsigned long long
int ls(int p){ return (p << 1) ;}
int rs(int p){ return (p << 1 | 1) ;}
inline int lowbit(int x) { return x & (-x) ;}
//(A - B) % mod =((A % mod) - (B % mod) +mod) % mod
const int mod = 1e9 + 7;
const int N = 1e3 + 5;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

int ans[1 << 19];

int Find(int x ,int y) {
    if (x >= y) return 0;
    int z = max(Find(ls(x) ,y) ,Find(rs(x) ,y));
    if (z > ans[x]) return z;
    return ans[x];
}

void solve() {
    int k;
    cin >> k;
    int m = (1 << (k + 1));
    for (int i = k ; i > 0 ; i--) {
        for (int j = 1 << i , l = 1 << (i - 1) ; l > 0 ; l-- ,j += 2) {
            cin >> ans[j];
        }
    }
    cin >> ans[1];
    for (int i = 0 ; i < k ; i++) {
        for (int j = 0 ; j < (1 << i) ; j++) {
            int res = (1 << i) + j;
            if (ans[res] < ans[ls(res)]) {
                cout << "No Solution" << endl;
                return;
            }
            ans[rs(res)] = ans[res];
            int l = Find(ls(res) ,m) ,r = Find(rs(res) ,m);
            if (ans[ls(res)] != l or ans[res] != r) {
                swap(ans[ls(res)] ,ans[rs(res)]);
                l = Find(ls(res) ,m) ,r = Find(rs(res) ,m);
                if (l != ans[ls(res)] or r != ans[rs(res)]) {
                    cout << "No Solution" << endl;
                    return;
                }
            }
        }
    }
    for (int i = 1 << k ; i < m ; i++) cout << ans[i] << " \n"[i == m - 1];

}
signed main(){
    IOS
    int O_O = 1;
    // cin >> O_O;
    while(O_O--) solve();
}

 L2-048 寻宝图

bfs,就是判断条件要注意,遍历过的可以直接赋值为‘0’

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll __int128
#define PII pair<int,int>
#define PSI pair<string,int>
#define PVI pair<vector<int>,int>
#define endl '\n'
#define mk make_pair
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
#define Pi acos(-1.0)
#define ull unsigned long long
int ls(int p){ return (p << 1) ;}
int rs(int p){ return (p << 1 | 1) ;}
inline int lowbit(int x) { return x & (-x) ;}
//(A - B) % mod =((A % mod) - (B % mod) +mod) % mod
const int mod = 1e9 + 7;
const int N = 1e3 + 5;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

int ans1 ,ans2 ,n ,m;
string mp[100005];
bool ok ,ok1;
void bfs(int x ,int y){
    if (x < 0 or y < 0 or x >= n or y >= m or mp[x][y] == '0') return;
    if (mp[x][y] > '0') ok = 1;
    if (mp[x][y] > '1') ok1 = 1;
    mp[x][y] = '0';
    for (int i = 0 ; i < 4 ; i++) {
        int rx = x + dx[i] ,ry = y + dy[i];
        bfs(rx ,ry);
    }
}

void solve() {
    cin >> n >> m;
    for (int i = 0 ; i < n ; i++) cin >> mp[i];

    for (int i = 0 ; i < n ; i++) {
        for (int j = 0 ; j < m ; j++) {
            if (mp[i][j] > '0') {
                ok1 = 0 ,ok = 0;
                bfs(i,j);
                if(ok) ans1++;
                if(ok1) ans2++;
            }
        }
    }
    cout << ans1 << " " << ans2 << endl;
}
signed main(){
    IOS
    int O_O = 1;
    // cin >> O_O;
    while(O_O--) solve();
}

 L3-015 球队“食物链”

dfs ,赛时没有想法,时间也不太够了,赛后重新写了一下,发现并不是很难,就判断是否存在一个环,遍历的时候记录答案,最后如果存在就输出

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll __int128
#define PII pair<int,int>
#define PSI pair<string,int>
#define PVI pair<vector<int>,int>
#define endl '\n'
#define mk make_pair
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
#define Pi acos(-1.0)
#define ull unsigned long long
int ls(int p){ return (p << 1) ;}
int rs(int p){ return (p << 1 | 1) ;}
inline int lowbit(int x) { return x & (-x) ;}
//(A - B) % mod =((A % mod) - (B % mod) +mod) % mod
const int mod = 1e9 + 7;
const int N = 50;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
const double eps = 1e-8;

int mp[N][N];
bool vis[N] ,ok;
int n ,ans[N];

void dfs(int u ,int num) {
    if (ok) return;
    ans[num] = u;
    if(num == n) {
        if (mp[u][1] == 1) ok = 1;
        return;
    }
    bool f = 1;
    for (int i = 1 ; i <= n ; i++) if(!vis[i] and mp[i][1]) f = 0;
    if (f) return;
    for (int i = 1 ; i <= n ; i++) {
        if (!vis[i] and mp[u][i]) {
            vis[i] = 1;
            dfs(i,num + 1);
            vis[i] = 0;
        }
    }
}

void solve() {
    cin >> n;
    vector<string> s(n + 1);
    for (int i = 1 ; i <= n ; i++) cin >> s[i];
    for (int i = 1 ; i <= n ; i++) {
        for (int j = 0 ; j < n ; j++) {
            if (s[i][j] == 'W') mp[i][j + 1] = 1;
            else if (s[i][j] == 'L') mp[j + 1][i] = 1;
        }
    }
    ok = 0;
    vis[1] = 1;
    dfs(1 ,1);
    if (ok) {
        for (int i = 1 ; i <= n ; i++) {
            if (i == 1) cout << ans[i];
            else cout << " " << ans[i];
        }
        cout << endl;
    }else cout << "No Solution" << endl;
}
signed main(){
    // IOS
    int O_O = 1;
    // cin >> O_O;
    while(O_O--) solve();
}

相关文章:

  • JavaScript系列(83)--正则表达式高级详解
  • RabbitMQ学习—day6—springboot整合
  • 数组分块问题 【刷题反思】
  • doris:使用 Hint 调整 Join Shuffle 方式
  • Java 阻塞队列:让并发更“懂事”
  • Matplotlib,Streamlit,Django大致介绍
  • Day26 第七章 回溯算法part05
  • 基于PSO粒子群优化的BiLSTM双向长短期记忆网络序列预测算法matlab仿真,对比BiLSTM和LSTM
  • mysql大数量表添加索引方案
  • Linux提权之环境劫持提权(九)
  • 大语言模型中的 Token如何理解?
  • Linux 命令大全完整版(03)
  • 【嵌入式Linux应用开发基础】多线程编程
  • 基于AIGC的图表自动化生成工具「图表狐」深度评测:如何用自然语言30秒搞定专业级数据可视化?
  • ABC381E题解
  • 数据结构之二叉树的定义及实现
  • Unity使用IL2CPP打包时,我们应该注意什么?如何避免(可以举例说明)
  • 创建虚拟环境以及配置对应的项目依赖
  • DeepSeek技术全景解析:架构创新与行业差异化竞争力
  • Spring Boot数据访问(JDBC)全解析:从基础配置到高级调优
  • 门户网站域名是什么意思/安卓优化大师官方版
  • 校园网站建设需要什么/百度贴吧官网入口
  • 合肥网站制作公司电话/网络推广和seo
  • 郑州美容网站建设/优化推广服务
  • 潍坊制作网站的公司/北京最新消息今天
  • 嘉兴企业做网站/优化方案怎么写