不等式解 x+y >z , x+z > y, y+z>x && x+y+z = len;
暴力枚举x ,解出 (len - 2*x)/2< y < len/2
但实际计算时候,上界根据len的奇偶性判断 为 最大(len - 1)/2(取改式的整数值),而下界直接求出整数+1即可。
<pre name="code" class="html">#include <cstdio> #include <cstring> #include <cctype> #include <algorithm> #include <iostream> #include <cmath> using namespace std; typedef long long LL; #define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1|1 const int inf = 1e9; int get(int x,int len){ if(2*x >= len) return 0; int l = (len - 2*x)/2 + 1; int r = (len-1)/2; if(l<=r) return r-l+1; return 0; } int n,m; int main() { while(scanf("%d %d",&n,&m)==2){ int Max = 0,Min = inf; for(int i=1;i<=m;i++){ int x; scanf("%d",&x); Max = max(Max,x); Min = min(Min,x); } if(Min == 1 || Max == n){ LL res = 0; if(Min == 1 && Max == n) res = 0; else { int len = max(Min - 1, n - Max); for(int i=1;2*i<len;i++){ res += get(i,len); } } cout<<res<<endl; } else { int pre = Min - 1, suf = n - Max; int x = min(pre,suf),y=max(pre,suf); LL res = get(x,x+y); if(!res){ LL an1=0,an2=0; for(int i=1;2*i<x;i++) an1+=get(i,x); for(int i=1;2*i<y;i++) an2+=get(i,y); res = max(an1,an2); } cout<<res<<endl; } } return 0; }