• 【codeforces 572C】Lengthening Sticks


    time limit per test1 second
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    You are given three sticks with positive integer lengths of a, b, and c centimeters. You can increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most l centimeters. In particular, it is allowed not to increase the length of any stick.

    Determine the number of ways to increase the lengths of some sticks so that you can form from them a non-degenerate (that is, having a positive area) triangle. Two ways are considered different, if the length of some stick is increased by different number of centimeters in them.

    Input
    The single line contains 4 integers a, b, c, l (1 ≤ a, b, c ≤ 3·105, 0 ≤ l ≤ 3·105).

    Output
    Print a single integer — the number of ways to increase the sizes of the sticks by the total of at most l centimeters, so that you can make a non-degenerate triangle from it.

    Examples
    input
    1 1 1 2
    output
    4
    input
    1 2 3 1
    output
    2
    input
    10 2 1 7
    output
    0
    Note
    In the first sample test you can either not increase any stick or increase any two sticks by 1 centimeter.

    In the second sample test you can increase either the first or the second stick by one centimeter. Note that the triangle made from the initial sticks is degenerate and thus, doesn’t meet the conditions.

    【题目链接】:http://codeforces.com/contest/572/problem/C

    【题解】

    首先先枚举用了l当中的多少i (全都用掉了)(i∈[0..l]);
    假设a,b,c各增长了x,y,z
    则x+y+z=i
    x,y,z为非负数
    再全都加1
    x+1+y+1+z+1=i+3
    隔板法可以算出来满足要求的x,y,z共有C(i+2,2)个;
    累加i=1,2…l的情况即可;
    这样我们就算出了包括合法和不合法的三条边的所有情况;
    设为total;
    接下来单纯算不合法的边;
    不合法->两边之和小于等于第3边
    则我们枚举最长的那条边为a,b,c三种情况
    假设他们用掉了l中的i
    则变成a+i或b+i或c+i;
    然后剩下的两条边分配l-i;
    依然用隔板法分配就好;
    因为每条边增量不同就算作不同方案;
    所以不会有重复计算的;

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    //const int MAXN = x;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    LL a,b,c,l,total = 0;
    
    LL solv(LL a,LL b,LL c,LL l)
    {
        if (b+c>a)
            return 1LL*0;
        //a>=b+c
        LL ma = min(a-b-c,l);
        return 1LL*(ma+2)*(ma+1)/2;
        // y + z + no used == ma;
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(a);rei(b);rei(c);rei(l);
        rep1(i,0,l)
            total += (1LL*(i+2)*(i+1))/2;
        //a+x b+y c+z
        // x+1 + y+1 + z+1 == i+3;
        rep1(i,0,l)
        {
            total -= solv(a+i,b,c,l-i);
            total -= solv(b+i,a,c,l-i);
            total -= solv(c+i,a,b,l-i);
        }
        printf("%I64d
    ",total);
        return 0;
    }
  • 相关阅读:
    2020杭电HDU-6756多校第一场Finding a MEX(图的分块)
    2020杭电HDU-6768多校第二场Lead of Wisdom(暴力DFS)
    牛客-Matrix(二维Hash-矩阵匹配)
    牛客-白兔的字符串(Hash+二分)
    2020杭电HDU-6768多校第二场The Oculus(假斐波那契数列真Hash)
    2020杭电HDU-6767多校第二场New Equipments(三分+费用流)
    使用 xmllint 验证 odoo xml文件
    odoo 的时差 坑
    请问如何突破”所选文件超出了文件的最大值设定:25.00 Mb“限制
    仓库打包作业超出分拣单数量时,发警报邮件
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626777.html
Copyright © 2020-2023  润新知