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;
}
}
}