Bitmap(模拟)

Bitmap is a compression technology for redundant data compression. The ratio of Bitmap Compression is the size of compressed data divides by the size of original data. The following is an example, the left one is an original array, the middle one is a bitmap array and the right one is a compact table. The original array is original data.The bitmap array and the compact table is compressed data. The way of Bitmap Compression is showed in the figure. Each element of a bitmap array occupies one bit. The data type of elements in compact table is the same as in original array.

Assuming the type of elements in original array is char(which occupies 1Byte=8bit each), there are eighteen elements in total. So the size of original array is 1Byte * 18 = 18Byte. After compression the size of bitmap array and compact table are 18 bit and 6Byte. The compression ratio can be calculated as: (18bit + 6Byte) / (18Byte) = 11/24 = 0.458333. Some people may wonder that the Bitmap Compression is not always efficient in some situation. So does fengzlzl. Now given the original array, can you help fengzlzl to calculate compression ratio using Bitmap Compression?

input

The first line is an integer T which stands for the number of test cases. For each test case the first line is an integer N which stands for the number of elements in the original array, a blank space, and a string which stands for the data type of elements in original array. The string can only be "char" or "int". The size and range of these two types are the same as signed 8-bit integer or signed 32-bit integer in C/C++, Java. There are N space-seperated integers of the original array in the second line. The i-th integer stands for the element whose subscript is i-1.

Constraints

1 <= N <= 1000

output

For each test case output a float number stands for the compression radio, rounded to 6 digits after decimal point.

sample input

2
18 char
0 0 0 0 1 1 1 0 0 0 0 2 0 0 0 3 3 3
4 int
-1 -1 -1 -1

sample output

0.458333
0.281250

题意大概就是按照他的要求计算压缩比,主要是对于这道题目的理解,如果理解题意的话,还是比较好解决的,直接贴代码吧。


#include<cstdio> 
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<map>
using namespace std;
int main(){
	int t;
	int m,n,i,j;
	int sum,a[1001];
	string cmd;
	cin>>t;
	while(t--)
	{
		sum=1;
		cin>>n>>cmd;
		for(i=0;i<n;i++)
		{
			cin>>a[i];
			if(i>0&&a[i]!=a[i-1])
			sum++;
		}
		if(cmd=="char")
		m=sizeof(char);
		else 
		m=sizeof(int);
		double x=(sum*8.0*m+n)/(n*8*m);
		printf("%.6lf\n",x); 
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值