zoukankan      html  css  js  c++  java
  • spoj TBATTLE 质因数分解+二分

    题目链接:点击传送

    TBATTLE - Thor vs Frost Giants

    Thor is caught up in a fierce battle with Loki's army. This army consists of frost giants that have magical powers with them. Their strength levels gets multiplied when they are together. Giants are not highly skilled in the arts of combat, but their sheer size and strength make them formidable opponents even for the Asgardian gods. Thor is no exception. They recover very fast from physical injury but their recovery slows down when they are exposed to extreme heat. 
    Thor's hammer can generate heat only in multiples of heat quantum N. Frost giants get killed only when their combined strength level is exactly equal to the heat level of the hammer. Thor is interested in killing a continuous stretch of frost enemies with a throw of his hammer with a preference to kill closer enemies first.
    Continuous stretch is defined as a set of consecutive elements.
    Help Thor to determine the minimum stretch of frost giants that could be killed in a throw. In case of multiple minimal stretches, output the indices of that stretch that has lowest starting index. If there is no such continuous stretch possible then print -1.

    Input

    The first line will contain N, the number of Frost Giants in Loki's army and the Heat quantum.
    The second line will contain N integers (a_0, a_2....., a_n-1) - the strength of each frost giant. 
    Minimum stretch of the army should be 1.

    • 1 ≤ N ≤ 100000
    • 1 ≤ a_i ≤ 100000

    Output

    Output the range of the minimum stretch of frost giants that could be killed in a throw. In case of multiple minimal stretches, output the indices of that stretch that has lowest starting index.
    If there is no such continuous stretch possible then print -1.

    Example

    Input:
    3
    1 2 9
    Output: 
    2 2

    Input:
    5
    2 3 4 8 9 Output:
    -1

    Input: 10 2 4 3 5 17 4 7 5 2 15
    Output:
    7 8

    Explanation

    Input #1:
    Thor can only kill the stretch [2,2] as this is the minimum length range with strength, multiple of 3.

    Input #2:
    There is no stretch of frost giants that have combined strength as a multiple of 5.

    Input #3:
    There are many stretches of frost giants that have strength as multiple of 10. But the minimal stretch with the least indices is from [7,8]. Minimum size stretches are [7, 8] and [8, 9]. Out of them we select [7,8].

     
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    #define bug(x)  cout<<"bug"<<x<<endl;
    const int N=1e5+10,M=1e6+10,inf=2147483647;
    const ll INF=1e18+10,mod=1e9+7;
    ///   数组大小
    vector<int>p;
    int n;
    int c[20];
    void init(int n)
    {
        memset(c,0,sizeof(c));
        int si=0;
        for(int i=2;i<=n;i++)
        {
            if(n%i==0)p.push_back(i),si++;
            while(n%i==0)
            {
                c[si-1]++;
                n/=i;
            }
        }
    }
    int sum[N][20];
    int check(int l,int r)
    {
        for(int j=0;j<p.size();j++)
        {
            if(sum[r][j]-sum[l-1][j]<c[j])
                return 0;
        }
        return 1;
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            memset(sum,0,sizeof(sum));
            p.clear();
            init(n);
            for(int i=1;i<=n;i++)
            {
                int x;
                scanf("%d",&x);
                int num=x;
                for(int j=0;j<p.size();j++)
                {
                    sum[i][j]=sum[i-1][j];
                    while(num%p[j]==0)
                    {
                        num/=p[j];
                        sum[i][j]++;
                    }
                }
            }
            int s=-1,e=1e9;
            for(int i=1;i<=n;i++)
            {
                int st=i,en=n,ans=-1;
                while(st<=en)
                {
                    int mid=(st+en)>>1;
                    if(check(i,mid))
                    {
                        ans=mid;
                        en=mid-1;
                    }
                    else
                        st=mid+1;
                }
                if(ans!=-1)
                {
                    if(ans-i<e-s)
                        s=i,e=ans;
                }
            }
            if(s==-1)
                printf("-1
    ");
            else
                printf("%d %d
    ",s-1,e-1);
        }
        return 0;
    }
  • 相关阅读:
    学会读懂 MySql 的慢查询日志
    Azure Mobile App
    ZOJ 3810 A Volcanic Island (2014年牡丹江赛区网络赛B题)
    学习日记之观察者模式
    Linux C高级编程——网络编程基础(1)
    Atitit.ati&#160;&#160;str&#160;&#160;字符串增强api
    ListView 自己定义BaseAdapter实现单选打勾(无漏洞)
    TintTo和TintBy
    cc.Sprite
    手机网页一行中文字数上限
  • 原文地址:https://www.cnblogs.com/jhz033/p/6659790.html
Copyright © 2011-2022 走看看