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. 【Henu ACM Round #13 C】 Ebony and Ivory

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先求出c-bx的所有可能 ->存在map里面 然后枚举y看看ay在不在map里面 在的话就有解. 这样复杂度是\(O(N*lo ...

  2. 洛谷 P1914 小书童——密码

    P1914 小书童——密码 题目背景 某蒟蒻迷上了“小书童”,有一天登陆时忘记密码了(他没绑定邮箱or手机),于是便把问题抛给了神犇你. 题目描述 蒟蒻虽然忘记密码,但他还记得密码是由一串字母组成.且 ...

  3. CentOS6.4安装Docker

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

  4. 带你彻彻底底弄懂Scroller

    Scroller的使用 这是一个滑动帮助类.并不能够使View真正的滑动,而是依据时间的流逝.获取插值器中的数据.传递给我们.让我们去配合scrollTo/scrollBy去让view产生缓慢滑动,产 ...

  5. 第二天,导出文件sql,查询,视图view,聚合函数,反模式,字符串处理函数

    //把数据库导出到脚本文件mysqldump -uroot -p1234 --databases abc > d:/a/abc.sql CREATE TABLE stud( id INT PRI ...

  6. AIX 适配器

    1. 查看所有适配卡 lsdev -CHc adapter     2. 物理网卡适配卡 查看到物理网卡的个数与类型 lsdev -Cc adapter|grep ent   查看物理网卡具体插槽位( ...

  7. C/C++(文件操作二)

    二进制读写才是本质 二进制的读写对文件标记不敏感. eg: 对图片进行加密与解密: 用命令的形式去执行: //xx.exe -c src dest 加密 //xx.exe -d src dest 解密 ...

  8. Model、ModelMap、ModelAndView的作用及区别

    Model.ModelMap.ModelAndView的作用及区别 对于MVC框架,控制器controller执行业务逻辑 用于产生模型数据Model 视图view用来渲染模型数据 Model和Mod ...

  9. .Net 开源控件 NPlot使用小结

    NPlot是一款非常难得的.Net平台下的图表控件,能做各种曲线图,柱状图,饼图,散点图,股票图等,而且它免费又开源,使用起来也非常符合程序员的习惯.授权方式为BSD许可证. 下载链接: http:/ ...

  10. iOS Dev (51)加急审核

    https://developer.apple.com/appstore/contact/? topic=expedite