Luo Gu P4571 BZOJ 2257 [JSOI2009] bottles and fuel

bzoj topic Link

Above there is the hint to select the second and third bottles bottles

Time limit 10000 ms

Memory limit 131072 kB

Linux OS

Source Jsoi2009

Make complaints

The story is this: I had to write this tree section title, then thought for a long time did not want to come out at the same time how the update interval maintenance gcd on scratch paper, then looked, found the problem, say the entire problem solution problem Italy is obviously interval gcd, but staring at the face and did not see where the problem has gcd, but think of this question -. So I found the question ,, finally know how to pour oil.

  • [X] BZOJ 2257 and the fuel bottles
  • [] BZOJ 5028 z small gas station
  • [] CodeChef DGCD Dynamic GCD // oj this topic actually represent with letters instead of numbers

Garden blog actually do not support markdown-do

Problem-solving ideas

The title should be coupled with a word: \ (k \ leqslant the n-\) . Similar way down Decreases fuel, can not be reduced is reduced Martian pay the least fuel. I do not understand here is
due to the small data range, \ (the n-\) maximum only 1k, each number up to \ (10 ^ 9 \) , it is possible to find a factor violence, complexity is \ (O (n \ sqrt {\ max {a_i}}) \) . Also need to count the number of occurrences of all factors, directly map like, coupled to a complexity \ (\ log n-\ Times \ G SUM (a_i) \) , where \ (g (a_i) \) represents \ ( a_i \) the total number of factors.

Source

#include<map>
#include<cstdio>

int n,k;
std::map<int,int> m;//离散化因数
std::map<int,int>::iterator it;
int main()
{
    scanf("%d%d",&n,&k);
    while(n--)
    {
        int a;
        scanf("%d",&a);
        for(int j=1;j*j<=a;j++)//枚举因子
        {
            if(a%j==0)
            {
                m[j]++;
                if(a!=j*j) m[a/j]++;
            }
        }
    }
    for(it=m.end(),it--;it!=m.begin();it--)//一开始居然把这个循环放到了上面那个while里,WA了两发
    {
        if(it->second>=k)
        {
            printf("%d\n",it->first);
            return 0;
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/wawcac-blog/p/11315717.html