跳到正文

闲话

7.31总结

Super_Diu约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年7月31日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 2 分钟

版本

  1. v1当前

T1 T640726 落落的去的围栏

二分题哇

思路:

涂色 : 从 11 到 kk 涂成一种颜色,从 k+1k+1 到 nn 涂成另一种颜色。

可以枚举 kk 。

  • 左边颜色必须满足 >=k>=k 的条件才可以,二分出有多少 >=k>=k 的颜色 xx 。

  • 右边颜色必须满足 >=n−k>=n-k 才可以,二分出有多少 >=n−k>=n-k 的颜色 yy 。

即 ans+=x×y−min(x,y)ans += x \times y- min(x,y) 。


解释一下为什么是 ans+=x×y−min(x,y)ans += x \times y- min(x,y) 。

举个例子 , 在记录方案数时:

如果 x=3x=3 , y=3y=3 , 那么颜色分别就是 11 22 33 和 33 44 55 , 所以方案数需要两变量相乘。

最后输出 x×yx \times y 减去重复的数字即可。

思路实现 ( 部分代码 ) :

C++
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:

C++
#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; 
}
十年OI一场空,不开long long见祖宗\huge \text{十年OI一场空,不开long long见祖宗}
原文