[Hdu 4841] roundtable questions | Joseph problems STL-vector

The original title

Problem Description:
The classic Joseph problems, there are personal 2n, where n n a good man bad man, so that after deleting n people, and the rest are all good people. m is the number of each number.
n <= 32767

Solution:
First, consider the range of n, violence certainly does not work, so think of tree line ......
it turns out, thinking segment tree can be completely solved by vector, although vector.erase () complexity is linear, not a constant, but why can over it ......

About vector.erase ():
C ++ Reference of the complexity of writing is "Linear on the number of elements erased (destructions) plus the number of elements after the last element deleted (moving).", So it is linear ......

#include<cstdio>
#include<vector>
using namespace std;
int n,m,pos;
vector <int> v;

int main()
{
    while (~scanf("%d%d",&n,&m))
    {
        v.clear();
        for (int i=0;i<2*n;i++) v.push_back(i);
        pos=0;
        for (int i=0;i<n;i++)
        {
            pos=(pos+m-1)%(v.size());
            v.erase(v.begin()+pos);
        }
        for (int i=0,j=0;i<2*n;i++)
        {
            if (i && !(i%50)) putchar('\n');
            if (j<v.size() && i==v[j])
            {
                j++;
                putchar('G');
            }
            else putchar('B');
        }
        putchar('\n');
        putchar('\n');
    }
    return 0;
}

Still can not help but lament the STL powerful? ?

Guess you like

Origin www.cnblogs.com/mrha/p/11955082.html