T1 T640726 落落的去的围栏
二分题哇
思路:
涂色 : 从 到 涂成一种颜色,从 到 涂成另一种颜色。
可以枚举 。
-
左边颜色必须满足 的条件才可以,二分出有多少 的颜色 。
-
右边颜色必须满足 才可以,二分出有多少 的颜色 。
即 。
解释一下为什么是 。
举个例子 , 在记录方案数时:
如果 , , 那么颜色分别就是 和 , 所以方案数需要两变量相乘。
最后输出 减去重复的数字即可。
思路实现 ( 部分代码 ) :
for(ll i=1;i<n;i++){
ll x=lower_bound(a+1,a+m+1,i)-a;
ll y=lower_bound(a+1,a+m+1,n-i)-a;
x=m-x+1;
y=m-y+1;
ans+=x*y-min(x,y);
}由于需要二分,所以要对数组进行排序 !!!
赛时代码是骗分的就不讲了。
Code:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=2e5+10;
ll t,n,m,a[N],ans;
int main(){
ios::sync_with_stdio(0);
cin>>t;
while(t--){
cin>>n>>m;
ans=0;
for(ll i=1;i<=m;i++)cin>>a[i];
sort(a+1,a+m+1);
for(ll i=1;i<n;i++){
ll x=lower_bound(a+1,a+m+1,i)-a;
ll y=lower_bound(a+1,a+m+1,n-i)-a;
x=m-x+1;
y=m-y+1;
ans+=x*y-min(x,y);
}
cout<<ans<<'\n';
}
return 0;
}