GCJ 2009 Round 2 Problem A. Crazy Rows
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的更多相关文章
- 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 ...
- GCJ——Crazy Rows (2009 Round 2 A)
题意: 给定一个N*N的矩阵,由0,1组成,只允许交换相邻的两行,把矩阵转化为下三角矩阵(对角线上方全是0),最少需要多少次交换?(保证可以转化为下三角矩阵) Large: N<=40 解析: ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 【Henu ACM Round #13 C】 Ebony and Ivory
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先求出c-bx的所有可能 ->存在map里面 然后枚举y看看ay在不在map里面 在的话就有解. 这样复杂度是\(O(N*lo ...
- [Python] Generates permutations
>>> import itertools >>> for p in itertools.permutations('ABCD'): ... print(p) ('A ...
- 71.用express框架,出现 express.Router is not a function
Express版本太久
- Tachyon的配置详解
Tachyon的配置 Tachyon环境变量 Tachyon通用配置 TachyonMaster配置 TachyonWorker配置 用户配置 1 Tachyon的配置 这里以0.5.0版本为例,介绍 ...
- string StartsWith 方法 Https
public ActionResult Index() { string url = "Https://www.baodu.com"; ...
- HDU 4699 Editor 维护栈
维护两个栈,分别存光标前和光标后的数 再维护前缀和的栈 和 前缀和最大值的栈 注意一下左移,右移,删除到顶了就不操作了 5个操作 I x : 光标处插入x -----> s1.push(x) ...
- JOISC 2017 Day1 T3 烟花棒
JOISC 2017 Day1 T3 烟花棒 题意: 数轴上有\(N\)人在放烟花,一开始只有第\(K\)个人的烟花是点燃的,烟花燃烧的时间为\(T\)秒,求让所有人的烟花都可以点燃的速度的最小值 ...
- hash_set和hash_map
1.hash_set集合容器 hash_set利用链式哈希表,进行数据的插入.删除和搜索.与set容器同样,不同意插入反复键值的元素.SGIC++哈希表是一个链式的结构,由表头和一系列单链组成.表头是 ...
- 谈谈Spine动画在产品中的应用
笔者介绍:姜雪伟,IT公司技术合伙人.IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D ...
- Spring有用功能--Profile、WebService、缓存、消息、ORM
本篇介绍一些Spring与其它框架结合的有用功能,包含:Apache CXF WebService框架.Redis缓存.RabbitMQ消息.MyBatis框架. 另外对于Profile,也是Spri ...