zoukankan      html  css  js  c++  java
  • CodeForces 287B Pipeline (水题)

    B. Pipeline
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ultimate Thule, Vova wants the city to have exactly n pipes, each such pipe should be connected to the water supply. A pipe can be connected to the water supply if there's water flowing out of it. Initially Vova has only one pipe with flowing water. Besides, Vova has several splitters.

    A splitter is a construction that consists of one input (it can be connected to a water pipe) and x output pipes. When a splitter is connected to a water pipe, water flows from each output pipe. You can assume that the output pipes are ordinary pipes. For example, you can connect water supply to such pipe if there's water flowing out from it. At most one splitter can be connected to any water pipe.

    The figure shows a 4-output splitter

    Vova has one splitter of each kind: with 2, 3, 4, ..., k outputs. Help Vova use the minimum number of splitters to build the required pipeline or otherwise state that it's impossible.

    Vova needs the pipeline to have exactly n pipes with flowing out water. Note that some of those pipes can be the output pipes of the splitters.

    Input

    The first line contains two space-separated integers n and k (1 ≤ n ≤ 1018, 2 ≤ k ≤ 109).

    Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.

    Output

    Print a single integer — the minimum number of splitters needed to build the pipeline. If it is impossible to build a pipeline with the given splitters, print -1.

    Sample test(s)
    input
    4 3
    output
    2
    input
    5 5
    output
    1
    input
    8 4
    output
    -1


    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <queue>
    #include <map>
    #include <utility>
    #include <stack>
    #define REP(i,n) for(i=0;i<(n);++i)
    #define FOR(i,n) for(i=1;i<=(n);++i)
    using namespace std;
    typedef long long ll;
    const int INF = 1<<30;
    const double eps = 1e-20;
    const int N = 1000;
    
    void show(int *arr,int l,int r)
    {
        for(int i=l;i<r;++i)
        {
            printf("%d ",arr[i]);
            if(i%10==0) cout<<endl;
        }
    }
    
    long long n,k;
    
    ll get(double x)
    {
        double aa = floor(x+0.5);
        if(fabs(x-aa)<eps) return (ll)aa;
        return (ll)ceil(x);
    }
    
    void run()
    {
        if(n==1)
        {
            cout<<"0"<<endl;
            return;
        }
        if(n==0)
        {
            cout<<"0"<<endl;
            return;
        }
        if((2+k)*(k-1)/2-(k-2)<n)
        {
            cout<<"-1"<<endl;
            return;
        }
        if(n<=k)
        {
            cout<<"1"<<endl;
            return;
        }
        else if(n<=k+k-2)
        {
            cout<<"2"<<endl;
            return;
        }
        double a,b,c;
        a=1.0;
        b=-2*k+1;
        c=2*n-2;
        double de=sqrt(b*b-4*a*c);
    //    double ans1 = (-b+de)/(2*a);
        double ans2 = (-b-de)/(2*a);
    //    cout<<ans2<<endl;
        ll ans = get(ans2);
    //    cout<<ans1<<' '<<ans2<<endl;
        cout<<ans<<endl;
    }
    
    int main()
    {
    //    double x;
    //    while(cin>>x) cout<<get(x)<<endl;
        while(cin>>n>>k)
            run();
        return 0;
    }
  • 相关阅读:
    PTA 乙级 1041 考试座位号 (15分) C++
    markdown test
    PTA 乙级 1040 有几个PAT (25分) C/C++
    PTA 乙级 1039 到底买不买 (20分) Python
    升级DLL plugin 到AutoDllPlugin
    使用插件适配移动端布局
    关于webpack,你想知道的都在这;
    HTML2Canvas使用总结
    ajax自己封装
    linux 基础知识练习之一---安装与连接
  • 原文地址:https://www.cnblogs.com/someblue/p/4018976.html
Copyright © 2011-2022 走看看