牛客小白月赛39 (题解报告)

本文介绍了ACM/NOI/CSP等算法编程比赛的若干题目,包括暴力枚举、字符串处理、模拟算法和贪心策略等。针对每个题目,提供了简洁的解题思路和代码实现,涉及数据结构与算法的应用。

题目地址:

牛客小白月赛39_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ

目录

题目地址:

A-憧憬

 B-欢欣

C-奋发

​ 

D-绝望

E-迷惘

​ 

F-孤独

 G-冷静

 H-终别


A-憧憬

 简单的暴力枚举O(n^2)就行

判断向量平行就行x1y2==x2y1

const int MAX=100010;
int yy[MAX];
int xx[MAX];

int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int x1,y1,x2,y2;
        cin>>x1>>y1>>x2>>y2;
        xx[i]=x2-x1;
        yy[i]=y2-y1;
    }
    int x1,y1,x2,y2;
    cin>>x1>>y1>>x2>>y2;
    int xb=x2-x1;
    int yb=y2-y1;
//    cout<<"xb="<<xb<<endl;
//    cout<<"yb="<<yb<<endl;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            int x=xx[i]+xx[j];
            int y=yy[i]+yy[j];
//            cout<<"x="<<x<<endl;
//            cout<<"y="<<y<<endl;
            if(x*yb==y*xb){
                cout<<"YES"<<endl;
                return 0;
            }
        }
    }
    cout<<"NO"<<endl;
}

 B-欢欣

简单的字符串处理可以直接模拟也可以借助STL的string方式进行处理 

const int MAX=10000010;
string s;
int main(){
    cin>>s;
    for(int i=0;i<s.size();i++){
        if(s[i]=='Q'&&s[i+1]=='A'&&s[i+2]=='Q')
        {
            cout<<i+1;
            break;
        }
    }
}

C-奋发

 

D-绝望

 

E-迷惘

 

 简单的模拟题:

1.先把数转化为二进制整数

2.然后去掉前导零

3.转化为十进制相加即可

int main(){
    int n;
    cin>>n;
    ll ans=0;
    for(int i=0;i<n;i++){
        ll a;
        cin>>a;
        string s;
        s=work(a,2);
        reverse(s.begin(),s.end());
        string ss;
        int id;
        for(int j=0;j<s.size();j++){
            if(s[j]!='0')
            {
                id=j;
                break;
            }
        }
        for(int j=id;j<s.size();j++){
            ss+=s[j];
        }
//        cout<<"this is string "<<ss<<endl;
        ll num=0;
        for(int j=0;j<ss.size();j++){
//            cout<<j<<" "<<ss[j]<<endl;
            num+=(ss[j]-'0')*pow(2,ss.size()-1-j);
        }
//        cout<<"this is num"<<num;
        ans+=num;
    }
    cout<<ans<<endl;
}

F-孤独

 

 G-冷静

 

其实题意就是找出1-n最小质因数大于等于k的数的个数

我们可以开一个树状数组 从小到大 加入每个数的最小质因数

然后询问即可

我们先预处理每个数的最小质因数

然后

const int MAX=3e6+5;


// bitset<N> v;
ll v[MAX];
ll p[MAX];
ll cnt = 0;
void init(int n)
{
    for (int i = 2; i <= n; i++){
        if (v[i]==0) {
            p[++cnt] = i;
            v[i] = i;
        }
        for (int j = 1; j <= cnt; j++){
            if(p[j] > v[i] || p[j] > n / i)break;
            v[i * p[j]] = p[j];
        }
    }
}


struct node {
    ll r,k;
    ll pos;
    bool operator <(const node &p)const{
        return r<p.r;
    }
}a[MAX];
ll c[MAX];
ll lowbit(int x){
    return x&(-x);
}
void add(int x,int val){
    while (x<MAX) {
        c[x]+=val;
        x+=lowbit(x);
    }
}
ll query(int x){
    ll ans=0;
    while (x) {
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}
ll ans[MAX];
int q;
signed main(){
    cin>>q;
    for(int i=1;i<=q;i++){
        a[i].r=read();
        a[i].k=read();
        a[i].pos=i;
    }
    sort(a+1,a+1+q);
    init(MAX);
    int now=2;
    for(int i=1;i<=q;i++){
        while (now<=a[i].r) {
            add(v[now],1);
            now++;
        }
        ans[a[i].pos]=query(a[i].r)-query(a[i].k-1);
    }
    for(int i=1;i<=q;i++)
        printf("%lld\n",ans[i]);
}

 H-终别

贪心:

如果不使用魔法的话直接从左到右依次处理即可

显然同时处理三个要比一个一个处理要好

如果第i个之前的都处理完了

那么我们就要同时处理第i,i+1,i+2个直到第i个被处理完

若是使用魔法的话 我们可以枚举使用魔法的位置

我们可以预处理l r数组 

l r数组是前l后r所有被消灭完所需要的处理的次数

使用最后的时间复杂度是O(n)

const int MAX=1e6+10;
ll l[MAX],r[MAX];
ll a[MAX];
ll b[MAX];
int n;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    for(int i=2;i<=n+1;i++){
        l[i]=l[i-1]+a[i-1];
        a[i]=max(0,a[i]-a[i-1]);
        a[i+1]=max(0,a[i+1]-a[i-1]);
    }
    for(int i=n-1;i>=0;i--){
        r[i]=r[i+1]+b[i+1];
        b[i]=max(0,b[i]-b[i+1]);
        b[max(i-1,0)]=max(0,b[max(0,i-1)]-b[i+1]);
    }
    ll ans=1e15;
    for(int i=1;i<n;i++){
        ans=min(ans,l[i]+r[i+1]);
    }
    cout<<ans;
}

 

### 关于牛客小白109的信息 目前并未找到关于牛客小白109的具体比信息或题解内容[^5]。然而,可以推测该事可能属于牛客网举办的系列算法之一,通常这类比会涉及数据结构、动态规划、图论等经典算法问题。 如果要准备类似的事,可以通过分析其他场次的比题目来提升自己的能力。例如,在牛客小白13中,有一道与二叉树相关的题目,其核心在于处理树的操作以及统计最的结果[^3]。通过研究此类问题的解决方法,能够帮助理解如何高效地设计算法并优化时间复杂度。 以下是基于已有经验的一个通用解决方案框架用于应对类似场景下的批量更新操作: ```python class TreeNode: def __init__(self, id): self.id = id self.weight = 0 self.children = [] def build_tree(n): nodes = [TreeNode(i) for i in range(1, n + 1)] for node in nodes: if 2 * node.id <= n: node.children.append(nodes[2 * node.id - 1]) if 2 * node.id + 1 <= n: node.children.append(nodes[2 * node.id]) return nodes[0] def apply_operations(root, operations, m): from collections import defaultdict counts = defaultdict(int) def update_subtree(node, delta): stack = [node] while stack: current = stack.pop() current.weight += delta counts[current.weight] += 1 for child in current.children: stack.append(child) def exclude_subtree(node, total_nodes, delta): nonlocal root stack = [(root, False)] # (current_node, visited) subtree_size = set() while stack: current, visited = stack.pop() if not visited and current != node: stack.append((current, True)) for child in current.children: stack.append((child, False)) elif visited or current == node: if current != node: subtree_size.add(current.id) all_ids = {i for i in range(1, total_nodes + 1)} outside_ids = all_ids.difference(subtree_size.union({node.id})) for idx in outside_ids: nodes[idx].weight += delta counts[nodes[idx].weight] += 1 global nodes nodes = {} queue = [root] while queue: curr = queue.pop(0) nodes[curr.id] = curr for c in curr.children: queue.append(c) for operation in operations: op_type, x = operation.split(&#39; &#39;) x = int(x) target_node = nodes.get(x, None) if not target_node: continue if op_type == &#39;1&#39;: update_subtree(target_node, 1) elif op_type == &#39;2&#39; and target_node is not None: exclude_subtree(target_node, n, 1) elif op_type == &#39;3&#39;: path_to_root = [] temp = target_node while temp: path_to_root.append(temp) if temp.id % 2 == 0: parent_id = temp.id // 2 else: parent_id = (temp.id - 1) // 2 if parent_id >= 1: temp = nodes[parent_id] else: break for p in path_to_root: p.weight += 1 counts[p.weight] += 1 elif op_type == &#39;4&#39;: pass # Implement similarly to other cases. result = [counts[i] for i in range(m + 1)] return result ``` 上述代码片段展示了针对特定类型的树形结构及其操作的一种实现方式。尽管它并非直接对应小白109中的具体题目,但它提供了一个可借鉴的设计思路。 ####
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值