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. CCF模拟 无线网络

    无线网络 时间限制: 1.0s 内存限制: 256.0MB   问题描述 目前在一个很大的平面房间里有 n 个无线路由器,每个无线路由器都固定在某个点上.任何两个无线路由器只要距离不超过 r 就能互相 ...

  2. CODEVS——T 4189 字典

    http://codevs.cn/problem/4189/  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Des ...

  3. POJ 3592 Instantaneous Transference(强连通+DP)

    POJ 3592 Instantaneous Transference 题目链接 题意:一个图.能往右和下走,然后有*能够传送到一个位置.'#'不能走.走过一个点能够获得该点上面的数字值,问最大能获得 ...

  4. 操作系统的 (program)loader(程序加载器)

    在计算机科学中,加载器(也叫程序加载器)属于操作系统的一部分,用于加载程序(programs)和库(libraries).加载器是执行程序和代码必不可少的组件,正是它负责将程序送入内存,为程序的运行提 ...

  5. JCameraView 仿微信拍照Android控件(点击拍照,长按录小视频)

    JCameraView 控件介绍 这是一个模仿微信拍照的Android开源控件,主要的功能有如下: 点击拍照. 前后摄像头的切换. 长按录视频(视频长度为10秒内). 长按录视频的时候,手指上滑可以放 ...

  6. python生成md5, shell生成md5

    echo -n 'aaa'|md5sum|cut -d ' ' -f1 python用hashlib md5=hashlib.md5(mid.upper()).hexdigest().upper()

  7. [ES6] Extends class in ES6 vs ES5 subclass

    ES6 class with extends and super: class Tree { constructor(size = ', leaves = {spring: 'green', summ ...

  8. HDU 1495 很可乐(BFS 倒水问题)

    题意  将体积为s的可乐  利用容积分别为n和m的两个杯子平均分为两份  至少须要倒多少次可乐 能够把容器s,n,m中装的可乐量看成一种状态 容器都是没有刻度的  所以每次倒可乐要么把自己倒完 要么把 ...

  9. oralce的系统用户system的输入口令怎么找回?遇见ORA-28000: the account is locked怎么解锁?

    好几个月前安装的Oracle软件忽然想用就忘记了当初设置的口令了,今天查了下怎么找回. 以一个用户jqz/jqz(曾经建立的一个用户.幸亏还记得)的身份登录后: SQL> connect/as ...

  10. jmeter--FTP测试

    FTP服务主要提供上传和下载功能.有时间需要我们测试服务器上传和下载的性能.在这里我通过JMeter做一个FTP测试计划的例子. 当然,JMeter官方网站的用户手册也有例子,但由于版本较早,我也算是 ...