1038. 二哥的约瑟夫

http://acm.sjtu.edu.cn/OnlineJudge/problem/1038
好久没写过博客了,搞了这么久的算法,觉得自己还是不会灵活运用,这题用树状数组+二分;
如果想到了,并且思路清晰的话,那就比较简单,可是我一开始根本就不会往这个方面就想;
后来还是问了队长,队长直接告诉了我想法,然后拍出来了,哎,自己怎么这么弱啊!!

这题思路;
这题先用树状数组来标记,然后二分来查找,
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
const int maxn=10000+10;
int a[maxn],c[maxn];
int n;
int lowbit(int x)
{
    return x&(-x);
}
void genxin(int x,int s)
{
    while(x<=10000)
    {
        c[x]+=s;
        x+=lowbit(x);
    }
}


int sum(int x)
{
    int ans=0;
    while(x>0)
    {
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1; i<n; i++)scanf("%d",&a[i]);
        for(int i=1; i<=n; i++)
        {
            genxin(i,1);
        }
        int cnt;
        int len=n,now=0;
        for(int i=1; i<=n; i++)
        {
            now=(now-1+a[i])%len;
            int l,r;
            l=1,r=n;
            while(l<=r)
            {
                int mid=(l+r)>>1;
                if(sum(mid)>=now+1)
                {
                   cnt=mid;
                   r=mid-1;
                }
                else
                {
                    l=mid+1;
                }
            }
            if(i==n)
            {
                cout<<cnt<<endl;
                return 0;
            }
            genxin(cnt,-1);
            --len;
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值