HDU 2830 dp最大完全矩阵Matrix Swapping
2015-12-15 21:58
351 查看
Matrix Swapping II
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1519 Accepted Submission(s): 1019
[align=left]Problem Description[/align]
Given an N * M matrix with each entry equal to 0 or 1. We can find some rectangles in the matrix whose entries are all 1, and we define the maximum area of such rectangle as this matrix’s goodness.
We can swap any two columns any times, and we are to make the goodness of the matrix as large as possible.
[align=left]Input[/align]
There are several test cases in the input. The first line of each test case contains two integers N and M (1 ≤ N,M ≤ 1000). Then N lines follow, each contains M numbers (0 or 1), indicating the N * M matrix
[align=left]Output[/align]
Output one line for each test case, indicating the maximum possible goodness.
[align=left]Sample Input[/align]
3 4 1011 1001 0001 3 4 1010 1001 0001
[align=left]Sample Output[/align]
4 2 Note: Huge Input, scanf() is recommended.
题意:
可以任意的交换两列,让求最大矩阵的面积(不含0,含1)
最大完全子矩阵,以第i行为底,可以构成的最大矩阵,因为该题可以任意移动列,所以只要大于等于height[i]的都可以移动到一起,求出 height>=height[i]的个数即可
,这里用hash+滚动,先求出height[i]出现的次数,然后逆序扫一遍 hash[i]+=hash[i+1];
#include<bits/stdc++.h> using namespace std; int H[1010][1010]; int T[1010]; char s[1010][1010]; int main() { int n,m; while(~scanf("%d%d",&n,&m)) { for(int i=1;i<=n;i++) scanf("%s",s[i]); memset(H,0,sizeof(H)); for(int i=n;i>=1;i--) { for(int j=1;j<=m;j++) { if(s[i][j-1]=='1') H[i][j]=H[i+1][j]+1; } } int Max=-1; memset(T,0,sizeof(T)); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(H[i][j]!=0) T[H[i][j]]++; //hash统计对应各个高的底 } for(int j=n;j>=1;j--) //hash统计对应出现的各个高,逆推得到底的大小(高大于等于该点的边的数目) T[j]+=T[j+1]; for(int j=1;j<=m;j++) Max=max(Max,H[i][j]*T[H[i][j]]); //求最大的面积 for(int j=1;j<=n;j++) //大小为n T[j]=0; //清空防止累积到下一行 } printf("%d\n",Max); } return 0; }
方法二:
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; bool cmp(int a,int b) { return a > b; } int main() { int R,C,num[1005],a[1005]; while(~scanf("%d%d",&R,&C)) { getchar(); int ans = 0; memset(num,0,sizeof(num)); for(int i=1;i<=R;i++) { for(int j=1;j<=C;j++) { char c = getchar(); if(c == '1') ++num[j]; else num[j] = 0; a[j] = num[j]; } sort(a+1,a+1+C,cmp); //排序 for(int j=1;j<=C && a[j];j++) if(a[j]*j > ans) ans = a[j]*j; getchar(); } printf("%d\n",ans); } return 0; }
相关文章推荐
- android gradle 配置
- 谈谈你对Application类的理解
- android 如何设置窗体特性getWindow().setFlags
- Unity3D 提取游戏资源的三个工具支持 Unity5
- iOS---自动处理键盘事件的第三方库:IQKeyboardManager
- 人脸及脸部关键点检测控件
- iOS-浅谈runtime运行时机制02-runtime简单使用
- iOS-浅谈runtime运行时机制01-类与对象的内部结构
- Android Context 是什么?
- swift学习日记(七)下标 继承
- iOS的图表显示的实现
- android中的ViewTreeObserver
- 【Android 学习】之二维码扫描开发(闪光灯功能)
- Android开发之ListView-ArrayAdapter的使用
- iOS减小导航栏button响应范围
- Android Activity LaunchMode
- Swift 柯里化(currying)和反柯里化(uncurrying)
- ios下fixed回复框bug的解决方案
- 微信:JSSDK开发
- iOS切图的尺寸