Shuffle related problem.

视觉特征-自监督-对比学习:SimCLR(在潜空间用对比损失最大化同一样本经不同数据增强所得视图间的一致性来学习表示)【Loss:对比损失函数】【同批次非同源样本作为负样本】【2020-02】 Abstract摘要本文提出了SimCLR:一个用于视觉表征对比学习的简单框架。我们简化了近期提出的对比式自监督学习算法,无需专门的架构或记忆库。为了理解是什么使对比预测任务能够学习到有用的表征,我们系统地研究了该框架的主要组成部分。我们表明:(1)数据增强的组合在定义有效预测任务中起着关键作用;(2)在表征与对比损失之间引入可学习的非线性变换,可显著提升所学习表征的质量;(3)与监督学习相比,对比学习受益于更大的批量大小和更多的训练步骤。 阅读详情

Shuffle is quite a frequently asked question in interviews. The most famous one is shuffle a deck of cards.

Peiyush Jain, 2004 published a paper "A simple in-place Algorithm for in-Shuffle". However this paper is a bit hard to understand without some math background.

Blog  gives a good way to understand this paper. Here, I just tweak it in my own way of understanding.

To better understand the paper, we need to first think about this problem.

Suppose we have an array, 1 2 3 4 5 6 7 8. We want to shuffle it as 5 1 6 2 7 3 8 4. It is like we have two hand of cards. One hand with 1 2 3 4, the other with 5 6 7 8

We want to perfectly shuffle it.

If we are allowed to use extra space, we can place them easily. 1--> pos: 2, 2 --> pos: 4, 3--> pos: 6, 4 --> pos: 8; 5--> pos: 1, 6--> pos: 3, 7 --> pos: 5, 8 -->pos: 7

thus, the i number goes to 2*i % (n+1)

#include "header.h"
using namespace std;
// Time complexity O(N), Space complexity O(N)
void shuffle(vector<int>& nums) {
  int n = nums.size();
  vector<int> target(n, 0);
  for(int i = 1; i <= nums.size(); ++i) {
    int pos = i;
    int targetPos = (2 * i) % (n + 1);
    target[targetPos - 1] = nums[pos - 1];
  }
  for(int i = 0; i < n; ++i) nums[i] = target[i];
}

int main(void) {
  vector<int> nums{1, 2, 3, 4, 5, 6, 7, 8};
  shuffle(nums);
  for(int i = 0; i < nums.size(); ++i) {
    cout << nums[i] << " ";
  }

}

To review the above process, we can write it in another way. 

1--> pos: 2,  [2, 1, 3, 4, 5, 6, 7, 8] mark pos: 2 as negative

2 --> pos: 4, [4, 1, 3, 2, 5, 6, 7, 8] mark pos: 4 as negative

3--> pos: 6, [5, 1, 6, 2, 7, 3, 8, 4] mark pos:  6 as negative

4 --> pos: 8; [8, 1, 3, 2, 5, 6, 7, 4] mark pos: 8 as negative 

5--> pos: 1, [5, 1, 3, 2, 7, 6, 8, 4] mark pos: 1 as negative 

6--> pos: 3, [5, 1, 6, 2, 7, 3, 8, 4] mark pos: 3 as negtaive

7 --> pos: 5, [5, 1, 3, 2, 7, 6, 8, 4] mark pos: 5 as negative

8 -->pos: 7; [7, 1, 3, 2, 5, 6, 8, 4] mark pos: 7 as negative

It thus forms two cycles: 1--> 2-->4-->8-->7-->5-->1; 3-->6-->3;

To perform this rule of shuffle, we need to remember the head, as long as we dont reach the head again, we can just keep on shuffle!

void shuffle(vector<int>& nums, int from, int mod) { // from is the head of cycle, mod equals to n + 1
  for(int i = from * 2 % mid; i != from; i = i * 2 % mod) {
    swap(nums[from], nums[i]);
  }
}

Great! Right now, seems like we can shuffle the array in O(1) space, however, how to find the cycle head might be a problem. Here comes the paper!

"A Simple In-Place Algorithm for In-Shuffle" paper summarize a conclusion that: 

to every 2 * n = (3 ^ k -1), this array will have k cycles, and each cycle starts with 1, 3, 9 ... 3 ^ k - 1. This conclusion makes life easier!

The algorithm is stated as follows:

Input: An array A[1 ..., 2n]

Step 1: Find a 2m = 3 ^ k - 1 such that 3^k <= 2n <= 3 ^ (k+1)

Step 2: Do a right cycle shift of A[m + 1, ...., n + m] by a distance m

Step 3: For each i (0, 1, ..., k - 1), starting at 3 ^ i, do the cycle leader. Algorithm for the in-shuffle permutation of order 2m.

Step 4: Recursively do the in-shuffle algorithm on A[2m + 1, ...., 2n].


The above statement however only solves the array whose length meets: 2 * n = 3 ^ k - 1. (8 = 9 - 1) In the above given example, n = 4, 8 = 3 ^ k - 1; K = 2. Thus, the cycle starting point would be 3^ 0, 3 ^ 1 ... 3 ^ (k-1) which is (1, 3....)

However, if the length != 3 ^ k - 1, we can use divide and conquer! 

if we have an array: a1, a2, a3, a4, a5, a6, a7, b1, b2, b3, b4, b5, b6, b7: array.size() == 14.

We can first partition the array into  two parts: a1, a2, a3, a4,    a5, a6, a7,     b1, b2, b3, b4,    b5, b6, b7

We cab shuffle the first part: a1, a2, a3, a4, b1, b2, b3, b4     a5, a6, a7, b5, b6, b7(this is the part partition left over)

//copyright@caopengcs 8/24/2013
//时间O(n),空间O(1)
void PerfectShuffle2(int *a, int n)
{
    int n2, m, i, k, t;
    for (; n > 1;)
    {
        // step 1
        n2 = n * 2;
        for (k = 0, m = 1; n2 / m >= 3; ++k, m *= 3)
          ;
        m /= 2;
        // 2m = 3^k - 1 , 3^k <= 2n < 3^(k + 1)

        // step 2
        right_rotate(a + m, m, n);

        // step 3
        for (i = 0, t = 1; i < k; ++i, t *= 3)
        {
          cycle_leader(a , t, m * 2 + 1);
        }

        //step 4
        a += m * 2;
        n -= m;

    }
    // n = 1
    t = a[1];
    a[1] = a[2];
    a[2] = t;
}



VLA-2025-03:GR00T N1【骨架:Eagle-2;端到端双系统VLA:S2 VLM+S1 DiT+FM;数据:单/双臂+人形,机器人+人类+仿真+神经共592.9M帧/8375.7小时】 通用机器人需要多功能的躯体和智能的头脑。近年来,人形机器人的发展在构建人类世界中通用自主性的硬件平台方面展现出巨大潜力。一个基于海量多样化数据源训练的机器人基础模型,对于使机器人能够推理新情况、稳健应对现实世界的多变性以及快速学习新任务至关重要。为此,我们推出了GR00T N1,一个面向人形机器人的开放基础模型。GR00T N1是一个具有双系统架构的视觉‑语言‑动作(VLA)模型。视觉语言模块(系统2)通过视觉和语言指令来解读环境。随后的扩散Transformer模块(系统1)实时生成流畅的电机动作。 阅读详情

相关推荐

【信息科学与工程学】计算机科学与自动化——第三百零五篇 数据中心 Scale-Up、Scale-Out、Scale-Across 18

普通CDN在遭遇50Gbps攻击时,节点瘫痪率高达70%。云化安全CDN的零信任模型通过将动态身份验证、最小权限访问与CDN的分布式加速能力深度集成,实现安全与性能的协同优化。云化的安全CDN(如腾讯云EdgeOne)与普通的安全CDN在架构、功能、性能和适用场景上存在显著差异。某视频平台接入腾讯云EdgeOne+零信任方案后,全球平均延迟降至46ms,攻击拦截率99.97%,验证了协同模型在高并发场景的普适性。​:随着边缘计算普及,云化安全CDN将逐步取代传统方案,成为高交互、高安全场景的标配基础设施。

weixin_49199313的博客 1696

idea中如何取消 2relateds problems 的错误提示

随时记录

weixin_43771904的博客 4143

CV-骨架02:Swin Transformer V2: Scaling Up Capacity and Resolution【2021-11】

Ze Liu* Han Hu*† Yutong Lin Zhuliang Yao Zhenda Xie Yixuan Wei Jia Ning Yue Cao Zheng Zhang Li Dong Furu Wei Baining Guo Microsoft Research Asia{v-zeliu1,hanhu,t-yutonglin,t-zhuyao,t-zhxie,t-yixuanwei,v-jianing}@microsoft.com {yuecao,zhez,lidong1,fuwei,bai

u013250861的博客 80

idea中报错 1 related problem,关于对象,变量

1 related problem ”这个是idea提醒你,引用这个类或者方法的地方可能与语法错误。第一次用idea,关于它的报错有一些不懂,还有参数传递,对象这些,希望万能的网友帮帮我。你的倒数第二个图,你new 这个对象时里面放到这写变量都没有声明,他自然会报错了。按住ctrl 鼠标点击一下这个方法,就能看到有哪些地方引用。

男神的专栏 2565

On a search problem related to branch-and-bound procedures.pdf

On a search problem related to branch-and-bound procedures.pdfOn a search problem related to branch-and-bound procedures.pdfOn a search problem related to branch-and-bound procedures.pdfOn a search problem related to branch-and-bound procedures.pdfOn a search problem related to branch-and-bound procedures.pdf

ABB AC500 系列 PLC 与上位机iFix 的通讯配置

在 软件中,有 3 种方式可以进入 IP config tool 的配置界面 双击 IP_settings,点击 IP config tool,即可进入 IP config tool 界面点击菜单栏的 Tool→IP config如果单独安装了 ABB IP Configration,则可以通过开始→程序→ABB→ABB IPConfigration 启动 IP 配置工具启动 IP config tool 后,即可进入 IP 配置界面。

xdpcxq的专栏 4028

Java语言基础知识(三)

Java语言基础知识

qq526017350的博客 2301

FreeCAD源码分析:Sketcher模块

Sketcher模块基于Part模块功能实现了特征编辑(Feature Editing)建模的功能。这类参数化建模系统主要包括几何表示、约束求解器等两大组成部分。

国产CAx(CAD/CAE/CAM)软件研发 6523

A Gene Shuffle Problem

A Gene Shuffle Problem The genomes of two related species, like that of cabbage and turnip, may contain similar genes. Their order in the genome may be different due to genome transpositions during e...

CHEN_ZAN_YU的博客 247

Oracle19c时区升级实战:从32到42版本的数据泵TSTZ报错解决方案

本文详细介绍了Oracle19c时区版本从32升级到42的实战步骤,重点解决数据泵TSTZ报错问题。通过检查时区版本、下载补丁包、应用补丁及执行升级脚本等关键操作,确保数据库时区数据更新,避免夏令时转换错误和历史时区规则不匹配等问题。文章还提供了常见问题排查和升级后的注意事项,帮助DBA高效完成时区升级任务。

n7o8p的博客 282

HALCON基于描述符的模板匹配(未标定)

HALCON基于不标定描述符的模板匹配

king_linj的博客 890

【vLLM-Ascend+MindSpeed】昇腾后训练强化学习最佳实践

本文基于 Atlas 800T A3 完整跑通 verl + DAPO 框架下的 Qwen3-32B 强化学习训练,提供开箱镜像、全链路脚本、参数解读与性能调优指南,帮助开发者少踩坑、可复现地开展昇腾 RL 后训练。

zzzliupi的博客 456

3DGS-to-PC实战指南:零代码实现3D高斯场景到点云/Mesh的高效转换

本文详细介绍了3DGS-to-PC工具如何实现3D高斯场景到点云/Mesh的高效转换,无需代码即可完成。通过实战案例和参数调优技巧,帮助用户快速掌握这一开源插件的使用方法,提升3D数据处理效率。特别适合建筑扫描、影视特效等领域的专业人士。

q1w3e5r7t9y的博客 273

AndroidStudio关闭Related problems提示

转自:【IDE】AndroidStudio关闭Related problems提示_命运之手的博客-优快云博客_1related problem

chuyouyinghe的专栏 2078

Qt入门教程【环境搭建篇】Qt6+Mingw+QMake+QtCreator

Qt Windows平台安装

小鱼酱的博客 2万+

PCIE2.0/PCIE3.0/PCIE4.0/PCIE5.0接口的带宽、速率计算

一、PCIE接口速率: 二、PCIE相关概念: 传输速率为每秒传输量GT/s,而不是每秒位数Gbps,因为传输量包括不提供额外吞吐量的开销位; 比如 PCIe 1.x和PCIe 2.x使用8b / 10b编码方案,导致占用了20% (= 2/10)的原始信道带宽。 GT/s —— Giga transation per second (千兆传输/秒),即每一秒内传输的次数。重点在于描述物理层通信协议的速率属性,可以不和链路宽度等关联。 Gbps —— Giga Bits Per Second

技术联盟 1万+

普及JAVA类与对象的基本信息

给成员变量赋初始值的代码写在构造方法中,其它业务代码还是去做普通方法基本类型变量(变量)中装的是具体的数,引用类型变量(引用)中装的是对象成员变量写在类中方法外,作用范围为整个类局部变量写在方法中(包括方法的参数),作用范围为当前方法java规定成员变量与局部变量是可以同名的,使用的时候默认采取的是就近原则注意当成员变量和局部变量同名时,若想访问成员变量,则this不能省略。...

m0_66501633的博客 346

基于Python的车牌识别系统实现

本文将以基于Python的车牌识别系统实现为方向,介绍车牌识别技术的基本原理、常用算法和方法,并详细讲解如何利用Python语言实现一个完整的车牌识别系统。

商务合作 / 项目定制 / 学习交流。个人vx:lovely_wml 5万+

1.SpringBoot常见错误

===========================================================错误:spring boot 报错:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default p答案:因为application.java 文件不能直接放在main/jav...

路漫漫的博客 968

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Related Problems

1. “高效地安排面试” 扩展问题一:某一天,在微软亚洲研究院有N个面试要进行,它们的时间分别为(B[i], E[i])(B[i]为面试开始时间,E[i]为面试结束时间)。假设一个面试者一天只参加一个面试。为了给面试者提供一个安静便于发挥的环境,我们希望将这N个面试安排在若干个面试点。不同的面试在同一个时间不能被安排在同一个面试点。如果你是微软亚洲研究院的HR,现在给定这N个面试的时间之后,你

说自己行就行 8732

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

【IDE】AndroidStudio关闭Related problems提示

天天向上 Up Up Up (*^▽^*) 3816

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Intellij IDEA 开启自动构建项目并显示 Problems 窗口的方法

具体方法见下图:

HeatDeath的博客 1万+
上一篇: Haffman Encoding and Decoding
下一篇: TODO: System design a recommendation system
ActiveCoder
博客等级 码龄11年 4粉丝 345原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值