[Code Jam] Crazy Rows

科技中介服务机构想要提升服务效率,需要哪些数智工具支持?.docx 科技中介服务机构想要提升服务效率,需要哪些数智工具支持? 立即下载

Problem

You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the matrix.

Your goal is to have all the 1 values in the matrix below or on the main diagonal. That is, for each X where 1 ≤ X ≤ N, there must be no 1 values in row X that are to the right of column X.

Return the minimum number of row swaps you need to achieve the goal.

Input

The first line of input gives the number of cases, T. T test cases follow.
The first line of each test case has one integer, N. Each of the next N lines contains Ncharacters. Each character is either 0 or 1.

Output

For each test case, output

Case #X: K
where X is the test case number, starting from 1, and K is the minimum number of row swaps needed to have all the 1 values in the matrix below or on the main diagonal.

You are guaranteed that there is a solution for each test case.

Limits

1 ≤ T ≤ 60

Small dataset

1 ≤ N ≤ 8

Large dataset

1 ≤ N ≤ 40

Sample


Input 
 

Output 
 
3
2
10
11
3
001
100
010
4
1110
1100
1100
1000
Case #1: 0
Case #2: 2
Case #3: 4

这个题是贪心法吧,重点是对题目条件的挖掘:

题目并没有要求行与行之间有一个顺序关系,只要是最后一位合规则就行了。因此题目所给数据可以直接变成以每行最后有一位所在位置为内容的一维数组,这样不论是判断还是交换都变简单了。而且题目的贪心精神也更显而易见了。

#include<stdio.h>

#define N 50

char mat[N][N];
int fin[N];

int main(void){
	int t, tc;
	scanf("%d", &t);
	for(tc = 1; tc <= t; tc++){
		int n, i, j;
		scanf("%d", &n);
		for(i = 0; i < n; i++){
			char l[N];
			scanf("%s", l);
			for(j = n - 1; j >= 0; j--){
				if(l[j] == '1'){
					break;
				}
			}
			fin[i] = j;
		}
		
		int ans = 0;
		for(i = 0; i < n; i++){
			for(j = i; j < n; j++){
				if(fin[j] <= i){
					break;
				}
			}
			ans += j - i;
			int t = fin[j];
			int k = j;
			for(k = j; k > i; k--){
				fin[k] = fin[k - 1];
			}
			fin[i] = t;
		}
		printf("Case #%d: %d\n", tc, ans);
	}
	return 0;
}

usb 扩容盘检测修复工具源码,编译产物 立即下载

相关推荐

Doris + Iceberg 实现高效数据湖分层:架构解析与最佳实践

本文深入解析了如何利用Doris与Iceberg构建高效的数据湖冷热分层架构。通过将高频访问的热数据存储在Doris中以实现毫秒级查询,同时将历史冷数据归档至低成本的Iceberg存储,该方案在保障查询性能的同时显著降低了总体拥有成本。文章详细阐述了从架构设计、自动化迁移到统一查询的最佳实践,为应对海量数据下的成本与性能平衡提供了实战指南。

weixin_29207533的博客 135

疯狂矩阵 Crazy Rows(挑战程序设计竞赛)

给定一个由0和1组成的矩阵(方阵)。 只允许交换相邻的两行, 要把矩阵转化为下三角矩阵(主对角线上方的元素全是0), 最少需要交换几次? 输入的矩阵保证总能转化成下三角矩阵。 输入 第一行为整数 T(1≤T≤60),表示有多少组测试数据 每组测试数据: 第一行为一个整数n,接下来的n行,每行有n个整数表示这个方阵 1≤N≤40 输出 输出T行,每行格式:Case #{第几组测试数据}: {答案},表示第几组测试数据,答案是多少 样例1 输入 1 4 1...

Recursions的博客 335

深入浅出横向联邦学习、纵向联邦学习、联邦迁移学习

深入浅出解析横向联邦学习(Horizontal Federated Learning)、纵向联邦学习(Vertical Federated Learning)和联邦迁移学习(Federated Transfer Learning)有多个机构(比如几家不同的银行,或者几家医院)都拥有一些数据,他们希望联合起来训练一个更强大的机器学习模型,但出于隐私保护或法规要求,他们不能直接把数据共享出来。联邦学习就是为了解决这个问题而提出的。它的核心思想是“数据不动模型动”,即数据保留在本地,参与方共同训练模型。

frostmelody 全网同名,大家多多关注呀~ 持续分享优质内容! 2466

Crazy Rows

Problem You are given anNxNmatrix with 0 and 1 values. You can swap any twoadjacentrows of the matrix. Your goal is to have all the 1 values in the matrix below or on the main diagonal. That i...

weixin_34138377的博客 165

卷积神经网络中的边缘检测:从Canny算法到深度学习(PyTorch实现)

本文系统梳理了边缘检测从经典Canny算法到深度学习方法的演进,重点解析了Canny算法的数学原理及其对卷积神经网络设计的深远影响。通过PyTorch实战,详细演示了如何构建并训练一个基于HED架构的深度学习边缘检测模型,并探讨了模型优化、评估及与传统方法融合的高级技巧。

jenkins8butler的博客 152

STM32F103C8T6+HX711实战:从零打造智能体重秤(附OLED显示与语音播报)

本文详细介绍了基于STM32F103C8T6微控制器和HX711称重模块,从零构建智能体重秤的完整实战教程。内容涵盖硬件选型、系统架构、驱动编写、数据滤波校准算法,并实现了OLED显示与语音播报功能。该项目是嵌入式学习的综合性实践,尤其适合作为毕业设计课题,帮助开发者掌握传感器应用、数据处理及人机交互等核心技能。

weixin_28285943的博客 518

Crazy Rows (GoogleCode Jam 2009 Round2 A)

来自《挑战程序设计竞赛》 1.题目原文 Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the matrix. Your goal is to have all the 1 values in the matrix below or on t

相思作坊半世离殇的博客 529

【大模型】DeepSeek + Coze 打造个人专属AI智能体使用详解

DeepSeek + Coze 打造个人专属AI智能体使用详解

congge 1万+

crazy Rows 2009 Round2 A

/*crazy Rows 2009 Round2 A 给定一个有0 1 组成的矩阵,只允许交换相邻的两行 要把矩阵化为下三角矩阵(主对角线上方的元素都是0) 最少需要交换几次? 输入的矩阵保证总能化成下三角矩阵 限制:N∈[1,100] time <=1S 内存 <=65535KB EG: 1 1 1 0 1 1 1 0 1 0 0 0 1 1 0

zhagoodwell查的博客 1011

Python爬虫实战:58同城租房信息分析与可视化,寻找高性价比房源

随着城市化进程加快,租房需求日益增长。58同城作为国内领先的本地生活服务平台,汇聚了大量房源信息。本教程将使用最新的Python爬虫技术获取58同城租房数据,通过数据分析与可视化揭示各区域租金水平,并运用机器学习算法找出性价比高的房源。本项目将使用异步爬虫、反爬对抗技术、数据清洗、可视化分析和机器学习建模等技术栈。

2201_76125261的博客 1281

YOLO11-Seg优化:卷积变体系列篇 | 动态蛇形卷积(Dynamic Snake Convolution) | ICCV2023

动态蛇形卷积(Dynamic Snake Convolution),增强微小特征提取能力,引入到YOLO11-Seg,与C3k2结合实现二次创新

会AI的学姐 371

Crazy Rows(2009 Round2 A)

Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the matrix. Your goal is to have all the 1 values in the matrix below or on the main diagonal. That is,...

厄尔尼诺的博客 297

深度学习工具包 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")

Code Jam 2009 Round2 Problem A. Crazy Rows —— 贪心

Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the matrix. Your goal is to have all the 1 values in the matrix below or on the main diagonal. That i

XMzhou的专栏 1093

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

假人有限元模型

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

[C++]Crazy Rows

《挑战程序设计竞赛第二版》

小菜鸟博客 663

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Crazy Rows(字符串二维数组的输入,移动交换)

最先想到的是尝试所有N!种交换方式,但是时间复杂度不行 package 数字技巧实战; import java.util.Scanner; public class CrazyRows { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new ...

Claire的博客 372

GCJ Crazy Rows

题目大意: 给定一个you

M阳光的专栏 686

GCJ Crazy Rows 水题-贪心

#include #include #include #include #include #include #include #include #include #include using namespace std; int main(){ int n; char a[45][45]; while(scanf("%d",&n)!=EOF){ ge

点羽染墨 589

GCJ--Crazy Rows (2009 Round2 A)

Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the matrix. Your goal is to have all the 1 values in the matrix below or on the main diagonal. That

社畜のブログ 717

挑战程序设计竞赛: Crazy Rows

文章目录题目大意解题思路代码如下 题目大意 解题思路 贪心策略: (1) 先确定应该把那一行移动到第一行,选择的策略是将能够移动到第一行中最近的那一行移动到第一行。 (2) 然后按相同策略确定第二行、第三行… 正确性证明: 使用贪心交换准则,将最近的那一行移动到当前确定的行,至少不会比将其他行移动到当前确定的步数多。 技巧: 每行只用一个数记录当前最后一个1的位置。比如矩阵 ...

Wangpeiyi9979的博客 363

每天水一水 Crazy Rows (2009 Round2 A) GCJ

#include using namespace std; #define maxn 10000 + 10 int M[maxn][maxn]; int mark[maxn]; int n, m; int solve() { int ans = 0; for(int i=1; i<=n; i++) { int pos = -1;

Dikea's Blog 1022

GCJ 2009 Round 2 Problem A. Crazy Rows

https://code.google.com/codejam/contest/204113/dashboard 题目大意: 给你一个矩阵,让你转化为下三角矩阵,每次只能交换相邻的行,求最小的交换次数。 思路: 一开始觉得记录每一行最后一个1的位置,然后相邻交换排序可以直接冒泡法(甚至可以nlogn的合并排序),结果交上去错的。 想了想因为每一行最后一个1已经满足j<=...

weixin_30929011的博客 216

Crazy Rows————GCJ 2009 Round2A(简单贪心)

题目:给定一个由0和1组成的矩阵,只允许交换相邻的两行。要把矩阵化为下三角矩阵,即主对角线上方的元素都为0,问至少需要交换几次。(1 input:3            001            100            010 output:2(1和2先交换,再交换2和3) 思路:先考虑最后形成的下三角矩阵的情形,肯定是第一行第一个数后面的都是0,第二行第二个数后面

say_c_box的博客 1071

GCJ 2009 Problem A. Crazy Rows【位运算要加LL】

教训:自己预处理位运算的一些东西,1之类的要加上 LL,否则会出问题 #include #include #include #include #include #include #include #include using namespace std; #define LL long long const LL maxn = 100; LL n; LL er[1000

ACMer不要报考高校代码为13627的移通学院! 474
上一篇: 最大的划分
下一篇: [Code Jam] Millionaire
type_q
博客等级 码龄14年 13粉丝 52原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值