zoukankan      html  css  js  c++  java
  • Codeforces 1355 C. Count Triangles

    传送门:C - Count Triangles 

    题意:给你四个数A,B,C,D,求有多少个三边为x,y,z (A ≤ ≤ ≤ ≤ ≤ ≤ D)的三角形。

    题解:枚举 x=A~B,然后计算当z的值大于等于C时,y为最小值B可以有多少个三角形,y为最大值C有多少个三角形。想想就可以发现 y=B~C时 其实就是一个差值为1的等差数列。然后要算出 y=B 和 y=C 时 z 比最大值D大了多少,这也是个等差数列,然后两个等差数列做差就好了。这么说可能有点乱,代码看了就明白了。

     1 #include<bits/stdc++.h>
     2 #define ll long long
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     ios::sync_with_stdio(false);
     8     cin.tie(0);
     9     cout.tie(0);
    10     ll a,b,c,d;
    11     cin>>a>>b>>c>>d;
    12     ll ans=0;
    13     for(ll i=a;i<=b;i++){
    14         ll p=i+b-1;
    15         ll q=i+c-1;
    16         ll x=0,y=0,pp=0,qq=0;
    17         if(p>=c) x=p-c+1;
    18         if(q>=c) y=q-c+1;
    19         if(p>d) pp=p-d;
    20         if(q>d) qq=q-d;
    21         ans=ans+(x+y)*abs(y-x+1)/2;
    22         ans=ans-(pp+qq)*abs(qq-pp+1)/2;
    23     }
    24     cout<<ans<<endl;
    25     return 0;
    26 }
  • 相关阅读:
    Labeling Balls
    Following Orders
    Frame Stacking
    Window Pains
    Sort it all out
    Ferry Loading||
    今年暑假不AC
    xcode10 出现 框架 或者 pod 出错
    网络请求 步骤
    swift UIAlertController使用 UIAlertController的宽度 为270
  • 原文地址:https://www.cnblogs.com/lilibuxiangtle/p/12902331.html
Copyright © 2011-2022 走看看