Crazy Rows————GCJ 2009 Round2A(简单贪心)
来源:互联网 发布:linux查看内存插槽 编辑:程序博客网 时间:2024/06/07 13:38
题目:给定一个由0和1组成的矩阵,只允许交换相邻的两行。要把矩阵化为下三角矩阵,即主对角线上方的元素都为0,问至少需要交换几次。(1<=N<=40)
input:3
001
100
010
output:2(1和2先交换,再交换2和3)
思路:先考虑最后形成的下三角矩阵的情形,肯定是第一行第一个数后面的都是0,第二行第二个数后面的都是0,依次类推。
那么哪几行可以被交换到第一行?
只要考虑每行最后一个1出现的位置。并找到离第一行最近的一个。交换过来。接下来的以此类推。
为什么可行?在纸上模拟一下就知道,能交换到第一行的肯定能放到第二行,第三行,反之则不一定。如果有两行同时满足第一行和第二行的要求,先把最靠近的换到第一行,这样至少不会比另一种策略用的步数多。
代码
#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <cmath>#include <queue>#include <string>#include <map>#include <iostream>using namespace std;#define INF 1e9+7#define MAXN 100010int n;char a[50];int b[50];bool used[50];int main(){ int n; scanf("%d",&n); getchar(); for(int i=0;i<n;i++) { b[i]=-1; used[i]=false; scanf("%s",a); for(int j=n-1;j>=0;j--) { if(a[j]=='1') { b[i]=j; break; } } } int s=0; for(int i=0;i<n;i++) { int ans=-1; for(int j=i;j<n;j++) { if(b[j]<=i){ ans=j; break; } } for(int j=ans;j>i;j--) { swap(b[j],b[j-1]); s++; } } printf("%d\n",s);}
0 0
- Crazy Rows————GCJ 2009 Round2A(简单贪心)
- GCJ Crazy Rows 水题-贪心
- Code Jam 2009 Round2 Problem A. Crazy Rows —— 贪心
- GCJ 2009 Round2 A (Crazy Rows)
- GCJ--Crazy Rows (2009 Round2 A)
- GCJ 2009 Round2 Problem A. Crazy Rows
- GCJ 2009 Round2 A Crazy Rows
- GCJ Crazy Rows
- GCJ 2009 Round 2 Problem A. Crazy Rows
- GCJ Round 2 2009 Problem A. Crazy Rows
- 每天水一水 Crazy Rows (2009 Round2 A) GCJ
- GCJ 2009 Problem A. Crazy Rows【位运算要加LL】
- 挑战2.7.2 Round 2 2009 A. Crazy Rows 贪心
- crazy Rows 2009 Round2 A
- Crazy Rows
- Crazy Rows (GoogleCode Jam 2009 Round2 A)
- [Code Jam] Crazy Rows
- poj1455——Crazy tea party
- 安卓四种点击最常用点击事件之一
- Retrofit初探——Header问题补充
- Stack(栈)的使用
- JAVA BIO例子
- java语言开发环境配置 jdk 配置中.; 什么意思
- Crazy Rows————GCJ 2009 Round2A(简单贪心)
- 77. Combinations
- qnx之中断控制
- 自定义view
- Caffe 入门- caffe数据准备,格式转换
- leetcode难度及面试频率
- 大话设计之模式总结——创建型模式
- UI系列网站
- 浅谈binder机制