小媛在努力
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
2
-
描述
-
在多媒体数据处理中,数据压缩算法尤为重要。小媛上完课后就想自己发明一个数据压缩算法。她想呀想,终于想到一个方法。在多媒体数据中有很多数据都是重复的,所以她想把连续相同的数据用数据出现的次数和数据本身表示。例如:1 1 1 2 3 3 3 3 3 压缩后及为3 1 1 2 5 3(表示3个1,1个2和5个3)。有想法后小媛就希望把它用代码实现了。但是大家都知道小媛现在整天都忙着苦B的复习考研,连电脑都摸不到。所以她希望作为ACMer的你帮她写一下。
-
输入
-
输入包含多组数据,第一行一个数字T代表输入样例数。
每组样例开始一个数M < 10^7表示这组数据中数字的个数,接下来M个数表示要被压缩的数字(数字都不超过int表示的范围)。
输出
- 每组测试数据输出一行数字对,如上面描述的一样。两个数字之间用一个空格隔开。 样例输入
-
1 9 1 1 1 2 3 3 3 3 3
样例输出
-
3 1 1 2 5 3
-
输入包含多组数据,第一行一个数字T代表输入样例数。
一般方法AC:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int main(){
int n;
scanf("%d",&n);
while(n--){
int m,x,t;
scanf("%d",&m);
scanf("%d",&x);
t=x;
int ans=1;
for(int i=1;i<m;i++){
scanf("%d",&x);
if(t==x)
ans++;
else{
printf("%d %d ",ans,t);
t=x;
ans=1;
}
}
printf("%d %d ",ans,t);
printf("\n");
}
return 0;
}
感觉可以用map做,测试数据也对,但是过不了,求大神指点
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
int main (){
map<int,int>s;
map<int,int>::iterator it;
int t;
scanf("%d",&t);
while(t--){
int n,x;
scanf("%d",&n);
while(n--){
scanf("%d",&x);
s[x]++;
}
it=s.begin();
cout<<it->second<<" "<<it->first;
it++;
for(;it!=s.end();it++){
cout<<" "<<it->second<<" "<<it->first;
}
cout<<endl;
}
return 0;
}