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

来源:互联网 发布:航天税控发票软件 编辑:程序博客网 时间:2024/05/22 07:42

可以像筛选法那样标记 但是内存最多只能开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;}


0 0
原创粉丝点击