[POI 2012]Vouchers

21 篇文章 0 订阅
8 篇文章 0 订阅

题目链接

http://main.edu.pl/en/archive/oi/19/bon

题目大意

考虑正整数集合,现在有n组人依次来取数,假设第i组来了x人,他们每个取的数一定是x的倍数,并且是还剩下的最小的x个。
正整数中有m个数被标成了幸运数,问有哪些人取到了幸运数。

思路

不要把这个题想复杂了,实际上这个题非常水。。。
记录 last[x]+x= 从小到大第一个没有被用过的 x 的倍数。我们每次开始取x的倍数时,从 last[x]+x 开始取,注意每次都要不断更新 last[x] !若当前的 x 超出了所有的幸运数中的最大值,显然再也找不到x的倍数的幸运数了,直接跳出。这样做的话,每次取 x 的倍数的操作的复杂度是log2maxn,maxn是所有幸运数中的最大值。

代码非常好写,但是处处有坑,此题如果 last[x] 在某些时候忘记更新的话就会很容易TLE。

代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>

#define MAXN 2100000

using namespace std;

typedef long long int LL;

bool isLucky[MAXN],used[MAXN];
int last[MAXN],m,n;
LL stack[MAXN],top=0;

int main()
{
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        int x;
        scanf("%d",&x);
        isLucky[x]=true;
    }
    LL tot=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        LL x;
        scanf("%d",&x);
        LL tmp=x;
        for(int t=last[x]+x,j=1;j<=x;j++)
        {
            if(t>1100000) break;
            tot++,tmp--;
            while(used[t]) t+=x;
            used[t]=true;
            last[x]=t;
            if(isLucky[t])
                stack[++top]=tot;
        }
        tot+=tmp;
    }
    printf("%lld\n",top);
    for(int i=1;i<=top;i++) printf("%lld\n",stack[i]);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值