POJ 2985 The k-th Largest Group

OI 生涯回忆录 《Pilgrimage》 前言 于 NOI2020 后写下本文。 本文简单提及了在写作时尚能回忆起来的,在我的 OI 生涯中对我的影响重大的人、事、比赛等。 写作本文,旨在在回忆允许的范围内,对 OI 生涯进行一定程度上的梳理,总结。 开端 入门 我最早接触计算机程序设计,是在小学。 那时候,我的数学成绩不错,就被老师推荐去学习编程。 就这样,我在局前街小学的何静老师的指导下,走上了编程的道路。 小学时的编程,与其说是 “竞赛” ,不如说是 “兴趣班” 。 当时,包括我在内,很多小孩子天性是浮躁的。支撑我对着无聊的代码学下去的,并不 阅读详情

题目链接:POJ 2985 The k-th Largest Group

并查集 + 树状数组。

主要考察点是用树状数组求第k小的数。其中用到了二分法。

#include <iostream>
#include <stdio.h>
#include <cstring>

using namespace std;

const int MAX_N = 200000 + 100;
int n,m;
int num[MAX_N],p[MAX_N],a[MAX_N];

int lowbit(int i)
{
    return i & (-i);
}
void add(int i,int val)
{
    while(i <= n)
    {
        num[i] += val;
        i += lowbit(i);
    }
}
int sum(int i)
{
    int ans = 0;
    while(i > 0)
    {
        ans += num[i];
        i -= lowbit(i);
    }
    return ans;
}
int find(int x)
{
    return p[x] == x ? x : p[x] = find(p[x]);
}
int main()
{
    while(scanf("%d %d",&n,&m) != EOF)
    {
        memset(num,0,sizeof(num));
        for(int i = 1;i <= n;i++)
            a[i] = 1;
        for(int i = 1;i <= n;i++)
            p[i] = i;
        int cnt = n;
        add(1,n);
        int temp;
        int _a,_b;
        for(int i = 0;i < m;i++)
        {
            scanf("%d",&temp);
            if(temp == 1)
            {
                scanf("%d",&_a);
                _a= cnt - _a + 1;//转换为找第k小的数
                int l = 1;
                int r = n;
                while(l <= r)
                {
                    int mid = (l + r) >> 1;
                    if(sum(mid) >= _a)
                        r = mid - 1;
                    else
                        l = mid + 1;
                }
                printf("%d\n",l);
            }
            else
            {
                scanf("%d %d",&_a,&_b);
                _a = find(_a);
                _b = find(_b);
                if(_a == _b)
                    continue;
                p[_b] = _a;
                add(a[_a],-1);
                add(a[_b],-1);
                add(a[_a] = a[_a] + a[_b],1);
                cnt--;
            }
        }
    }
    return 0;
}


基于单片机的智能温控风扇设计 该智能温控风扇系统的设计目标是根据环境温度自动控制风扇的转速或开关状态,确保用户始终处于舒适的温度范围内。实时温度监测:通过温度传感器(如DS18B20或DHT22)实时监控环境温度。风扇控制:根据温度变化自动调节风扇的转速,温度过高时风扇加速,温度适中时风扇转速降低,达到节能与舒适平衡。LCD显示:通过LCD屏幕实时显示温度信息。无线通信:通过Wi-Fi或蓝牙模块将温度数据发送至手机或云平台,支持远程控制风扇的开关或调节转速。报警功能:温度过高或过低时,蜂鸣器发出警报,提示用户注意。 阅读详情

相关推荐

Transformer时序预测避坑指南:TFT在风电场的5个实战经验

本文分享了Temporal Fusion Transformer在风电场功率预测中的五个实战经验,涵盖数据预处理、模型调优、显存优化、注意力机制解读及在线部署。针对风电数据噪声大、概念漂移等挑战,提供了从构建数据质量标签到高效超参调优的具体方案,并强调利用Transformer架构的可解释性进行业务洞察,助力实现稳健的工业级时序预测。

bert9linguist的博客 462

POJ--2985(树状数组 / Treap +并查集)

2015-03-1619:52:57 思路:一道比较裸的插入不断插入数值、删除数值 + 查询第k大的题目。   (1)对于合并操作,可以采用并查集来维护,并维护一个num值来表示group总数。   (2)对于查询第k大的操作     (i)可以用树状数组维护+二分查找来不断逼近第k大     (ii)可以利用树状数组本身c[]数组的特性来倍增逼近第k大     (iii)...

dingdi3021的博客 267

OCV/AOCV/SOCV的介绍和应用

library中的cell delay是在某个固定的PVT(operating condition)下仿真得出的。,cell的delay并不是一个固定值,而是一个随机值,遵循高斯分布或门特卡洛分布,在STA中用OCV来模拟这一『特征』。OCV全称on chip variation,用于描述不同管子间由于工艺偏差、电压降、温度变化引起的delay变化。,也用于描述工艺偏差引起的net厚度宽度的变化从而导致net的电容电阻变化。优点:设置起来比较简单粗暴,对于不那么先进的工艺可行!的影响,更贴合实际。

m0_61003348的博客 8980

poj 2985 树状数组+并查集(第k大组数)

题意:一开始有n个数,有两种操作,0 a b表示将a所在组和b所在组合并,1 a表示查询当前的第k大组数有多少个。 思路:合并集合很简单用并查集即可。查询的方法是:用树状数组记录数集中某个数出现次数,即tree[i]表示含有i个数的组有多少个。查找的时候二分即可。 #include #include #include #include #include #include using

dearmango 534

WPF实战:利用Live Charts实现高性能动态曲线可视化

本文详细介绍了如何在WPF中使用Live Charts实现高性能动态曲线可视化,特别适用于工业监控和金融分析等实时数据场景。通过增量渲染、内存复用和硬件加速等核心技术,Live Charts能够高效处理高频数据更新,显著提升图表流畅度。文章提供了从基础实现到工业级优化的完整解决方案,帮助开发者应对各种性能挑战。

weixin_29189373的博客 407

数字电路实验避坑指南:如何正确搭建主从JK触发器测试电路(附Proteus仿真)

本文详细解析了主从JK触发器的工作原理及实验搭建指南,重点解决了常见的Catching Problem现象,并提供了Proteus仿真优化方案。通过元器件选型、电路连接步骤和常见错误排查,帮助读者正确搭建测试电路,确保数字电路实验的稳定性和可靠性。

weixin_30790841的博客 469

POJ 2985 The k-th LargestGroup(Treap树)

Description Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants to group some of the cats. To do that, he first offers a nu...

邵光亮的博客 807

高效利用ChEMBL数据库:从靶点查询到活性数据处理的完整指南

本文提供了高效利用ChEMBL数据库的完整指南,从靶点查询到活性数据处理。详细介绍了靶点精准定位、高级过滤技巧、数据导出优化以及活性数据清洗、特征工程和可视化等实战技巧,帮助药物研发人员快速获取和处理生物活性数据,提升工作效率。

weixin_29278805的博客 792

树状数组求第k大值

以POJ 2985为例,具体的写在程序里。思路都是基于二分的思想。 下面是(LogN)^2的方法 /* 题意:某人养了很多猫,他会把一些猫合并成一组,并且会询问第k大组有几只猫 算法:处理集合用并查集,动态更新第K值用树状数组,具体的看注释 2011-07-21 19:59 */ #include #define MAXN 300000 int a[MAXN

z309241990的专栏 4762

SAP GUI 字段遮罩功能详解及实现

默认情况下,系统使用作为掩码模式,遮罩整个字段内容长度;对于货币金额类型,掩码后会保留小数位并在整数位部分全部显示为,如****.**若需自定义掩码模式,可利用以下方式:维护“Maintain Masking Pattern”节点,在其中定义新的掩码方式(如:只露出后四位,其他位以X替代等);实现 BAdI(或类似命名,具体以系统版本为准)的方法,通过 ABAP 代码读取原始值,并依据业务逻辑生成掩码字符串返回,动态替换界面输出值;

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 709

Visual Studio调试技巧与实用方法总结(实战经验分享)

本文根据多年的项目实战经验,详细介绍一些Visual Studio常用的高效调试手段与技巧。

dvlinker的技术专栏 3万+

POJ-2985(树状数组 + 并查集 + 二分)

题目:http://poj.org/problem?id=2985 这题考察了两个点呢,组合并用到了并查集,求第K大的数用到了树状数组,刚开始WA了几次,发现是在组合并之后仅仅update(*, -1)了一次,因为是删除了两个组增加了一个组,应该是update(*, -1)两次,不细心啊。。。 #include #include #include using namespace

还有多远 604

华为OD新系统机试真题 2026.5.10 - 美观的灯笼

华为OD新系统机试真题【美观的灯笼】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【美观的灯笼】

qq_45776114的博客 640

POJ2985 The k-th Largest Group

题目链接:http://poj.org/problem?id=2985 并查集合并小组,这个自然是不用说的,主要的问题是怎么求第K大值。 首先,线段树每一个结点存储的是组内元素的个数区间内有多少个组,初始化的时候所有的组中都是一个元素,所以当区间左端点为1的时候,结点的值为n,然后每次合并,原有小组的空间内结点值-1,合并后的新组空间内结点值+1,线段树这么维护完了以后,询问过程中,如果右子树

浅雨歌 570

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

poj2985The k-th Largest Group

链接:http://poj.org/problem?id=2985 题意:初始给定n个元素个数为1的集合,然后给定m个操作,有两种操作:C=0时输入a,b表示将a,b所在的集合合并成一个集合,C=1时输入k表示询问当前所有集合中集合大小排序中的第k大的集合的size。 分析:集合合并直接用并查集维护就可以了,找第k大的话用树状数组找第k大就好了。 代码: #include #include

Fsss 457

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

POJ2985 The k-th Largest Group(treap+并查集)

题目链接 Input 1st line: Two numbers N and M (1 ≤ N, M ≤ 200,000), namely the number of cats and the number of operations. 2nd to (m + 1)-th line: In each line, there is number C specifying t

HOrchard 589

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Poj 2985 The k-th Largest Group

题目:http://poj.org/problem?id=2985 题目:合并集合,求元素第k多的集合有多少个元素。 合并集合很简单用并查集即可。 这里我主要想说一下如何用树状数组并在线查询数集内第k小(大)的数。 方法是:用树状数组记录数集中某个数出现次数,每输入一个数,相当于将该数出现次数加1,对应到树状数组中就相当于insert(t, 1),统计的时候,可以利用树状数组的求和,这样我

1648

[POJ2985]The k-th Largest Group

Problem 刚开始,每个数一个块。 有两个操作:0 x y 合并x,y所在的块 1 x 查询第x大的块 Solution 用并查集合并时,把原来的大小删去,加上两个块的大小和。 Notice 非旋转Treap一直错。。。 Code 旋转Treap(非旋转Treap总是TLE...) #include<cmath> #include<cstdio> #inclu...

Q882199的博客 223

poj2985 The k-th Largest Group

并查集+treap维护的第k大数 #include <iostream> #include <cstdlib> #include <cstdio> using namespace std; struct Node{ int val, l, r, rnd, sze, hav; }nd[200005]; int n, m, fa[200005], ...

dianjiaxian1205的博客 126

poj 2985 The k-th Largest Group(线段树+并查集)

题目链接http://poj.org/problem?id=2985 题意: 有N只猫,刚开始每只猫各自在不同的集合,现在给出M个操作,有两种操作:   1.把第i只猫和第j只猫所在集合合并。   2.查找第i大的集合内有多少只猫。 思路: 操作一,集合合并很容易想到并查集。 操作二,如果每次合并之后进行查找直接遍历一遍,效率O(n^2),很容易超时。所以可以考虑

To be excellent! 337

POJ2985_The k-th Largest Group(树状数组)

转自:https://www.cnblogs.com/wuyiqi/archive/2011/12/25/2301071.html 题意: 首先给你N只老鼠,M个操作;最开始一只老鼠一个组; 输入0,输入两个数i,j;使得i老鼠与j老鼠的组合并; 输入1,输入一个数K,求第K大的组有多少只老鼠。 思路: 第K大就是第n-k+1小,用树状数组求第K小。 觉得:https://www.cnblogs....

CY05627的博客 393

poj 2985 The k-th Largest Group 并查集+树状数组求第k大

题意大概是:两种操作,合并集合,求元素第k多的集合有多少个元素 集合的合并就用并查集,同时维护一个数组tol表示该集合中的元素个数 树状数组,存值为i的个数,sum(i)表示值小于等于i的个数 树状数组求第k大,用二分法即可 #include #include const int MAX = 200010; int b[MAX]; int a[MAX],tol[MAX]; int n,

renew 1462

POJ 2985 The k-th Largest Group [并差集+treap]

给出N个数,每个数各自成一个集合,然后M个操作,有两种,0,会将两个数所属的集合并起,1,问当前所有集合中第K大的size是多少,需要对集合大小为1的特别处理。

Asuna_acmer 581

[poj-2985]The k-th Largest Group_Treap+并查集

The k-th Largest Group poj-2985     题目大意:给你n只猫,有两种操作:1.将两只猫所在的小组合并。2.查询小组数第k大的小组的猫数。     注释:1<=n,m<=200,000.       想法:开始的想法就是用Treap合并,用Treap删除。然后发现Treap合并实在是...太tm gay了。没办法,用并查集吧。想法就出现了,...

dianan0938的博客 155

poj_2985 The k-th Largest Group 树状数组求第K大

前面说了树状数组的三个用途, 在

e的专栏 748
上一篇: UVa 348 - Optimal Array Multiplication Sequence
下一篇: SG函数模板
小爷永远不死
博客等级 码龄15年 58粉丝 363原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值