【牛客小白月赛117】E题——种类数小结

1 初步想法

1.1 前置知识:vector数组的去重操作

unique()将不重复的元素放在数组前面,重复元素移到后面,qs获取不重复元素的后一个位置,之后用erase()函数去除重复元素。

qs=unique(a.begin()+1,a.begin()+k+1);
a.erase(qs,a.end());

1.2 模拟的解法

根据题目意思,统计数组中不同数的种类数,然后遍历数组,每次减去种类数,直到数组中的元素只有一种为止。在实现上,我的输入的数是从数组a下标1开始,所以我设置的循环条件是a.size()>2,因为a[0]=0,如果碰到输入n个相同的非零整数,此时去重后元素的个数是2。

实现代码(运行超时,只通过30%左右的数据)

#include<bits/stdc++.h>
using namespace std;
int n;
vector<long long> a(100005);
void process(){
	int k=a.size()-1;
	for(int i=1;i<=a.size();i++){
		if(a[i]-k>0) a[i]=a[i]-k;
		else a[i]=0;
	}
	vector<long long>::iterator qs;
	sort(a.begin()+1,a.begin()+k+1);
	qs=unique(a.begin()+1,a.begin()+k+1);
	a.erase(qs,a.end());
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	vector<long long>::iterator qs;
	sort(a.begin()+1,a.begin()+n+1);
	qs=unique(a.begin()+1,a.begin()+n+1);
	a.erase(qs,a.end());
	long long cnt=0;
	while(a.size()>2){
		process();
		cnt++;
	}
	cout<<cnt;
    return 0;
}

不过这种解法的问题是会超时,数据规模是10的5次方,main函数的while循环加上函数process()里面也有一重循环,效率不高,要想一种新的解法。

2 新方法

2.1 新方法的思想

我设计以下测试样例:

测试样例1
4
100 200 300 400

对于测试样例1,排序后,我们知道100是最先变化为0的,100在减小为0的过程中,种类数已知是4个,所以,100减为0需要进行100/4=25次操作。之后在剩下的数中,100减为0后依次变为100,200,300(是所有数同时减100),对于这里的100要变成0,因为目前数组有4,100,200,300这4种数,需要经过100/4=25次操作。之后,剩下的数以此为100,200,因为目前数组有4,100,200这3种数,100变成0需要经过100/3+1=34次操作(目前数组有),然后剩下一个数98,需要经过98/2=49次操作。一共是25+25+34+49=133次操作。通过这样的做法我们可以降低时间复杂度。

2.2 新方法的实现

为了实现新方法,在数组各元素减为0的过程中,我们要设置几个变量:
ans:最终结果
sum:累计减少量
cnt:当前的种类数,对于非0元素,如果不是第一个,因为前面的数已经变成0,所以非第一个数需要+1,把0算进去。cnt=a.size()-i+(i!=0);
x:表示当前的数变为0需要操作的次数,我们需要向上取整:x=(a[i]-sum+cnt-1)/cnt。

实现代码

#include<bits/stdc++.h>  // 包含所有标准库头文件
#define LL long long    // 定义长整型别名
int n;
using namespace std;

int main(){
    cin>>n;  // 输入数组长度
    vector<LL> a(n);  // 创建长度为n的长整型向量
    for(int i=0;i<n;i++) cin>>a[i];  // 输入数组元素
    
    // 排序并去重,确保数组是升序且无重复元素
    sort(a.begin(),a.end());
    a.erase(unique(a.begin(),a.end()),a.end());
    
    // 如果数组只有一个元素,直接输出0
    if(a.size()==1)
        cout<<0<<endl;
    else{
        LL ans=0,sum=0,cnt=0;  // ans:结果,sum:累计减少量,cnt:当前影响的元素数量
        
        // 遍历去重后的数组
        for(int i=0;i<n;i++){
            // 如果当前元素减去累计减少量已经小于0,跳过
            if(a[i]-sum<0) continue;
            
            // 计算当前影响的元素数量:剩余元素数 + (如果不是第一个元素,还包括之前的元素)
            cnt=a.size()-i+(i!=0);
            
            // 计算需要多少次操作才能将当前元素减为0
            // (a[i]-sum+cnt-1)/cnt 是向上取整的技巧
            LL x=(a[i]-sum+cnt-1)/cnt;
            
            ans+=x;  // 累计操作次数
            sum+=x*cnt;  // 更新累计减少量
        }
        cout<<ans<<endl;
    }
    return 0;
}

参考

b站讲解

### 关于牛客小白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(' ') x = int(x) target_node = nodes.get(x, None) if not target_node: continue if op_type == '1': update_subtree(target_node, 1) elif op_type == '2' and target_node is not None: exclude_subtree(target_node, n, 1) elif op_type == '3': 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 == '4': 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值