栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Acwing 第 41 场周赛

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Acwing 第 41 场周赛

Powered by:NEFU AB-IN

link

文章目录

第 41 场周赛

A AcWing 4308. 组合字符串

思路代码 B AcWing 4309. 消灭老鼠

思路代码 C AcWing 4310. 树的DFS

思路代码

第 41 场周赛

A AcWing 4308. 组合字符串

思路

尽可能延长s1, 判断是否取s2第一个即可
字典序:

a, b每一位比较,如果a[i] < b[i],那么a代码

'''
Author: NEFU AB-IN
Date: 2022-03-05 18:59:48
FilePath: ACMAcwing41a.py
LastEditTime: 2022-03-05 19:03:50
'''
s1, s2 = input().split()

res = s1[0]
for i in range(1, len(s1)):
    if s1[i] > s2[0]:
        res += s2[0]
        print(res)
        exit(0)
    res += s1[i]
if res == s1:
    res += s2[0]
print(res)
B AcWing 4309. 消灭老鼠

思路

枚举斜率即可
精度尽可能大!!

代码
'''
Author: NEFU AB-IN
Date: 2022-03-05 19:07:41
FilePath: ACMAcwing41周赛b.py
LastEditTime: 2022-03-05 19:25:24
'''

s = list()

n, x0, y0 = map(int, input().split())
for i in range(n):
    x, y = map(int, input().split())
    if x == x0:
        s.append(int(2e9))
    else:
        s.append((y - y0) / (x - x0))

res = 1
s.sort()
for i in range(1, len(s)):
    if s[i] - s[i - 1] > 1e-10: #1e-10以下才够
        res += 1

print(res)
C AcWing 4310. 树的DFS

思路

采用vector存树,dfs根节点得到res答案序列,cnt记录每个点的子节点数量,从res中找即可

递归问题别用python,太容易爆了

代码
#include 
using namespace std;
#define LL long long
#define MP make_pair
#define SZ(X) ((int)(X).size())
#define IOS                                                                                                            
    ios::sync_with_stdio(false);                                                                                       
    cin.tie(0);                                                                                                        
    cout.tie(0);
#define DEBUG(X) cout << #X << ": " << X << endl;
typedef pair PII;
const int INF = 0x3f3f3f3f;

const int N = 2e5 + 100;
vector g[N];
vector res(0);

int cnt[N], vis[N];

int dfs(int u)
{
    res.push_back(u);
    for (auto v : g[u])
    {
        int s = dfs(v);
        cnt[u] += s;
    }
    return cnt[u];
}

signed main()
{
    IOS;
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <= n - 1; ++i)
    {
        int x;
        cin >> x;
        g[x].push_back(i + 1);
    }
    for (int i = 1; i <= n; ++i)
    {
        cnt[i] = 1;
    }
    dfs(1);
    for (int i = 1; i <= n; ++i)
    {
        vis[res[i]] = i; //反向标记res
    }
    for (int i = 1; i <= q; ++i)
    {
        int u, k;
        cin >> u >> k;
        if (k > cnt[u])
        {
            cout << "-1n";
            continue;
        }
        cout << res[vis[u] + k - 1] << 'n';
    }
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/753788.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号