Matrix Swapping II hdoj

Matrix Swapping II

Time Limit : 9000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 8
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

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.

Input

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

Output

Output one line for each test case, indicating the maximum possible goodness.

Sample Input

3 4
1011
1001
0001
3 4
1010
1001
0001

Sample Output

4
2

Note: Huge Input, scanf() is recommended.

Source

2009 Multi-University Training Contest 2 - Host by TJU


#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 1005;
int n, m,ans;
char mx[maxn][maxn];
int dp[maxn][maxn];
int s[maxn];
int val(int a, int b) {
	int res = 0;
	while (mx[a][b]==1) {
		a--;
		res++;
	}
	return res;
}
int mmax(int a, int b) {
	if (a > b) return a;
	return b;
}
int main()
{
	std::ios::sync_with_stdio(false);
	while (cin >> n >> m) {
		memset(mx, 0, sizeof(mx));
		memset(dp, 0, sizeof(dp));
		memset(s, 0, sizeof(s));
		ans = 0;

		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++) {
				cin >> mx[i][j];
			}
		for (int i = 1; i <= n; i++) {		
			int k = 0;
			for (int j = 1; j <= m; j++) {
				if (mx[i][j] == '1')
					dp[i][j] = dp[i - 1][j]+1;
				s[k++] = dp[i][j];
			}
			sort(s, s + k);
			for (int u = 0; u < k; u++)
				ans = mmax(ans, s[u] * (k - u));
		}
		cout << ans << endl;
	}


	return 0;
}


在系统运维过程中,如果观察到频繁的交换(swapping)行为,这通常表明系统的物理内存(RAM)不足以满足当前运行的应用程序需求,导致操作系统将部分内存数据转移到磁盘上的交换空间(swap space),从而缓解内存压力。然而,这种交换行为会显著影响系统性能,因为磁盘的读写速度远低于内存。 ### 原因分析 1. **物理内存不足** 当系统中运行的应用程序消耗的内存总量超过可用的物理内存时,操作系统会开始使用交换空间来释放内存页,这会导致频繁的内存与磁盘之间的数据交换[^1]。 2. **内存泄漏或资源未释放** 某些应用程序可能存在内存泄漏问题,即程序在运行过程中不断分配内存而未能正确释放,导致可用内存逐渐减少,最终触发交换行为。 3. **缓存和缓冲区占用过高** Linux 系统会利用空闲内存作为文件系统缓存(如 page cache 和 slab),以提高 I/O 性能。但在内存紧张时,系统可能无法及时释放这些缓存,从而导致交换行为增加。 4. **交换阈值设置不合理** Linux 系统通过 `swappiness` 参数控制内核将内存页移出到交换空间的倾向,默认值为 60。如果该值设置过高,即使系统仍有可用内存,也可能触发不必要的交换行为。 5. **突发性内存需求** 某些任务或进程在短时间内需要大量内存,例如批处理作业、大数据分析任务等,可能导致瞬时内存压力增大,从而引发交换。 ### 优化方法 1. **增加物理内存** 最直接的解决方案是增加服务器的物理内存(RAM),以减少对交换空间的依赖。这是解决频繁交换问题的根本方法。 2. **优化应用程序内存使用** 分析应用程序的内存使用情况,识别并修复内存泄漏问题,合理管理内存分配和释放。使用工具如 `valgrind`、`gperftools` 等可以帮助检测内存泄漏。 3. **调整 `swappiness` 参数** 通过修改 `/proc/sys/vm/swappiness` 文件或在 `sysctl.conf` 中设置 `vm.swappiness` 值来降低系统对交换空间的依赖。建议值为 10 到 20 之间,具体取决于系统负载特性。 ```bash sysctl -w vm.swappiness=10 ``` 4. **监控内存与交换使用情况** 使用 `top`、`htop`、`free`、`vmstat`、`sar` 等工具监控系统的内存和交换使用情况,及时发现异常行为。 ```bash free -h vmstat 1 ``` 5. **限制进程内存使用** 使用 `cgroups` 或 `systemd` 的内存限制功能,为特定服务或进程设置内存上限,防止个别进程占用过多内存。 ```ini # 示例:在 systemd 服务单元文件中限制内存 [Service] MemoryLimit=2G ``` 6. **使用 ZRAM 或 ZSWAP(内存压缩)** 启用 ZRAM 或 ZSWAP 可以在内存不足时将部分数据压缩后存储在内存中,而不是写入磁盘,从而降低 I/O 延迟。 ```bash modprobe zram echo $((16 * 1024 * 1024 * 1024)) > /sys/block/zram0/disksize mkswap /dev/zram0 swapon /dev/zram0 ``` 7. **避免不必要的缓存占用** 在内存紧张时,系统会自动释放部分缓存,但也可以通过手动清理缓存来释放内存: ```bash echo 1 > /proc/sys/vm/drop_caches # 释放 page cache echo 2 > /proc/sys/vm/drop_caches # 释放 dentries 和 inodes echo 3 > /proc/sys/vm/drop_caches # 释放所有缓存 ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值