码迷,mamicode.com
首页 > 其他好文 > 详细

SGU 108. Self-numbers 2 离线+位优化

时间:2014-09-26 20:31:58      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:io   for   sp   c   html   amp   r   size   c++   

可以像筛选法那样标记 但是内存最多只能开1024的int数组 我用了位优化 用一个int 32 位标记32个数字 接下来就离线 排个序 算出第k大的哪个数

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

int a[10000000/32];

struct node
{
	int x, y, id;
}b[50010];
int get(int x)
{
	int ans = x;
	while(x)
	{
		ans += x%10;
		x /= 10;
	}
	return ans;
}
void dfs(int x)
{
	int tmp = x + get(x);
	if(tmp > 10000000 || a[tmp])
		return;
	a[tmp] = 1;
	dfs(tmp);
}

bool cmp1(node a, node b)
{
	return a.x < b.x;
}
bool cmp2(node a, node b)
{
	return a.id < b.id;
}

int main()
{
	//for(int i = 1; i <= 10000; i++)
	//	p[i] = get(i);
	int n, k;
	while(scanf("%d %d", &n, &k) != EOF)
	{
		for(int i = 0; i < k; i++)
		{
			scanf("%d", &b[i].x);
			b[i].id = i;
		}
		sort(b, b+k, cmp1);
		memset(a, 0, sizeof(a));
		int c = 0, j = 0;
		for(int i = 1; i <= n; i++)
		{
			if(!(a[i/32]&(1<<(i%32))))
			{
				c++;
				while(j < k && b[j].x == c)
				{
					b[j].y = i;
					j++;
				}
			}
			int x = get(i);
			if(x <= n)
				a[x/32] |= (1<<(x%32));
		}
		printf("%d\n", c);
		sort(b, b+k, cmp2);
		for(int i = 0; i < k; i++)
		{
			if(i)
				printf(" ");
			printf("%d", b[i].y);
		}
		puts("");
	}
	return 0;
}


SGU 108. Self-numbers 2 离线+位优化

标签:io   for   sp   c   html   amp   r   size   c++   

原文地址:http://blog.csdn.net/u011686226/article/details/39579433

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!