https://code.google.com/codejam/contest/204113/dashboard

题目大意:

给你一个矩阵,让你转化为下三角矩阵,每次只能交换相邻的行,求最小的交换次数。

思路:

一开始觉得记录每一行最后一个1的位置,然后相邻交换排序可以直接冒泡法(甚至可以nlogn的合并排序),结果交上去错的。

想了想因为每一行最后一个1已经满足j<=i了,所以。。(设行i列j)

那么就只好贪心啦,每次选最近的要交换的。

#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=50;
char matrix[MAXN][MAXN];
int a[MAXN];
int main()
{
freopen("e:\\A-large-practice.in","r",stdin);
freopen("e:\\out.out","w",stdout);
int T;
scanf("%d",&T);
for(int kase=1;kase<=T;kase++)
{
int n;
scanf("%d",&n); for(int i=1;i<=n;i++)
scanf("%s",matrix[i]+1); for(int i=1;i<=n;i++)
{
a[i]=-1;
for(int j=1;j<=n;j++)
{
if(matrix[i][j]=='1')
a[i]=j; //记录每行最后一个1的位置
}
} int ans=0;
for(int i=1;i<=n;i++)
{
if(a[i] <= i) continue;
int j;
for(j=i+1;j<=n;j++)
{
if(a[j] <=i)
break;
}
j--;
for(;j>=i;j--)
{
swap(a[j],a[j+1]);
ans++;
}
} printf("Case #%d: %d\n",kase,ans);
} return 0;
}

GCJ 2009 Round 2 Problem A. Crazy Rows的更多相关文章

  1. Google Code Jam 2010 Round 1C Problem A. Rope Intranet

    Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...

  2. GCJ——Crazy Rows (2009 Round 2 A)

    题意: 给定一个N*N的矩阵,由0,1组成,只允许交换相邻的两行,把矩阵转化为下三角矩阵(对角线上方全是0),最少需要多少次交换?(保证可以转化为下三角矩阵) Large: N<=40 解析: ...

  3. 2009 Round2 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 ...

  4. Google Code Jam 2010 Round 1B Problem A. File Fix-it

    https://code.google.com/codejam/contest/635101/dashboard#s=p0   Problem On Unix computers, data is s ...

  5. 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 ...

  6. dp - Google Code jam Qualification Round 2015 --- Problem B. Infinite House of Pancakes

    Problem B. Infinite House of Pancakes Problem's Link:   https://code.google.com/codejam/contest/6224 ...

  7. Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation

    Problem A. Standing Ovation Problem's Link:   https://code.google.com/codejam/contest/6224486/dashbo ...

  8. Google Code Jam 2010 Round 1A Problem A. Rotate

    https://code.google.com/codejam/contest/544101/dashboard#s=p0     Problem In the exciting game of Jo ...

  9. Google Code Jam 2010 Round 1B Problem B. Picking Up Chicks

    https://code.google.com/codejam/contest/635101/dashboard#s=p1   Problem A flock of chickens are runn ...

随机推荐

  1. 03014_properties配置文件

    1.使用properties配置文件 (1)开发中获得连接的4个参数(驱动.URL.用户名.密码)通常都存在配置文件中,方便后期维护,程序如果需要更换数据库,只需要修改配置文件即可: (2)通常情况下 ...

  2. CCF模拟题 最优配餐

    最优配餐 时间限制: 1.0s 内存限制: 256.0MB   问题描述 栋栋最近开了一家餐饮连锁店,提供外卖服务.随着连锁店越来越多,怎么合理的给客户送餐成为了一个急需解决的问题. 栋栋的连锁店所在 ...

  3. CentOS6.4安装Docker

    首先,须要安装EPEL仓库 # wget http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpm # rpm -ivh e ...

  4. log4j+slf4j迁移到log4j2+slf4j (Servlet3.0)

    近期对系统中的旧项目实现log升级,选择了log4j2来取代log4j.作为最新一代的log实现.log4j2好在那里能够直接看log4j2性能章节. 这里写写怎样从log4j升级到log4j2. 1 ...

  5. json和XML

    发请求(url) 1.client  ---------------->服务端                发送数据(Json/xml)                      < - ...

  6. Google开源新的 RISC-V IP核: “BottleRocket”(https://cnrv.io)

    BottleRocket是RISCV RV32IMC的实现. Google在2017年11月29日在Github上非官方开源了BottleRocket的RTL代码,同时表明这并不是一个官方支持的Goo ...

  7. ElasticSearch指南

    ElasticSearch快速指南 ElasticSearch是基于Apache Lucene的分布式搜索引擎, 提供面向文档的搜索服务. 安装ElasticSearch 文档 创建文档 访问文档 更 ...

  8. 2.CURL命令

    转自:https://blog.csdn.net/ligang2585116/article/details/46548617 curl是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在 ...

  9. Kinect 开发 —— 图片浏览

    总体思路 首先运用WPF编写一个简单的支持多点触控的图片浏览程序,这方面您可以参看MSDN上的这篇文章,上面有代码,可能需要FQ才能下载.中文的话,您可以参考Gnie同学关于在WPF上面多点触屏(Mu ...

  10. HDU 4007 Dave(离散化)

    Dave Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submis ...