【NOIP2007提高组】统计数字

本题排序输出即可。

#include<cstdio>
#include<algorithm>
using namespace std;
int n,a[200010],tot=1;

inline int read()
{
	int x=0; char c=getchar();
	while (c<'0' || c>'9') c=getchar();
	while (c>='0' && c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x;
}

void qsort(int l,int r)
{
	int i=l,j=r,mid=a[l+r>>1];
	while (i<j)
	{
		while (a[i]<mid) i++;
		while (a[j]>mid) j--;
		if (i<=j) swap(a[i++],a[j--]);
	}
	if (i<r) qsort(i,r);
	if (l<j) qsort(l,j);
}

int main()
{
	freopen("count.in","r",stdin);
	freopen("count.out","w",stdout);
	n=read();
	for (int i=1;i<=n;i++) a[i]=read();
	qsort(1,n);a[++n]=-1;
	for (int i=2;i<=n;i++)
		if (a[i]!=a[i-1]) printf("%d %d\n",a[i-1],tot),tot=1;
		else tot++;
	return 0;
}

本来想打成这样的:(可惜XC不给。。。)

#include<cstdio>
#include<algorithm>
using namespace std;
int n,a[200010],tot=1;

inline int read()
{
	int x=0; char c=getchar();
	while (c<'0' || c>'9') c=getchar();
	while (c>='0' && c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x;
}

int main()
{
	freopen("count.in","r",stdin);
	freopen("count.out","w",stdout);
	n=read();
	for (int i=1;i<=n;i++) a[i]=read();
	sort(a+1,a+n+1);a[++n]=-1;
	for (int i=2;i<=n;i++)
		if (a[i]!=a[i-1]) printf("%d %d\n",a[i-1],tot),tot=1;
		else tot++;
	return 0;
}

总之,本题乃签到题也。

posted @ 2018-12-30 12:06  jz929  阅读(406)  评论(0编辑  收藏  举报